java Android:如何在保持寬高比的同時將圖像拉伸到屏幕寬度?
最後,我手動生成了尺寸,這很好用:
DisplayMetrics dm = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * mainImage.getHeight() / mainImage.getWidth(); //mainImage is the Bitmap I'm drawing
addView(mainImageView,new LinearLayout.LayoutParams(
width, height));
我想下載一張圖片(尺寸未知,但總是大致為正方形),並將其顯示為水平填充屏幕,並在任何屏幕尺寸下垂直延伸以保持圖像的高寬比。 這是我的(非工作)代碼。 它水平地延伸圖像,但不是垂直的,所以它被壓扁了......
ImageView mainImageView = new ImageView(context);
mainImageView.setImageBitmap(mainImage); //downloaded from server
mainImageView.setScaleType(ScaleType.FIT_XY);
//mainImageView.setAdjustViewBounds(true);
//with this line enabled, just scales image down
addView(mainImageView,new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
將adjustViewBounds設置為true並使用LinearLayout視圖組對我而言效果很好。 無需子類或要求設備度量標準:
//NOTE: "this" is a subclass of LinearLayout
ImageView splashImageView = new ImageView(context);
splashImageView.setImageResource(R.drawable.splash);
splashImageView.setAdjustViewBounds(true);
addView(splashImageView);
在某些情況下,這種神奇的配方巧妙地解決了這個問題。
對於任何人都為來自另一個平台而苦苦掙扎的人來說, “適合尺寸和形狀”選項在Android中處理得非常好,但很難找到。
您通常需要這種組合:
- 寬度匹配父,
- 身高包裹內容,
- adjustViewBounds打開(原文如此)
- 規模合適的中心
- cropToPadding OFF(原文如此)
然後它是自動和驚人的。
如果你是一個iOS開發人員,那麼在表格視圖中簡單地完成“完全動態的單元格高度”是非常棒的。err,我的意思是ListView。 請享用。
<com.parse.ParseImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/post_image"
android:src="@drawable/icon_192"
android:layout_margin="0dp"
android:cropToPadding="false"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:background="#eff2eb"/>
我設法只使用這個XML代碼來實現這一點。 可能是這種情況,日食並沒有渲染高度來顯示它擴大到適合; 然而,當你真的在設備上運行它時,它會正確渲染並提供所需的結果。 (至少對我來說)
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/whatever" />
</FrameLayout>
一個非常簡單的解決方案就是使用RelativeLayout
提供的功能。
以下是使用標準Android Views
實現的xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
>
<Button
android:text="button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<ImageView
android:src="@drawable/cat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:layout_above="@id/button_container"/>
</RelativeLayout>
</ScrollView>
訣竅是你設置ImageView
來填充屏幕,但它必須高於其他佈局。 這樣你就可以實現你需要的一切。
您將ScaleType設置為ScaleType.FIT_XY。 根據javadocs ,這將拉伸圖像以適應整個區域,必要時改變縱橫比。 這將解釋你所看到的行為。
為了得到你想要的行為... FIT_CENTER,FIT_START或FIT_END是接近的,但是如果圖像比它高,它將不會開始填充寬度。 你可以看看這些是如何實現的,你應該能夠弄清楚如何根據你的目的進行調整。
看看你的問題有一個更容易的解決方案:
ImageView imageView;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
imageView =(ImageView)findViewById(R.id.your_imageView);
Bitmap imageBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.your_image);
Point screenSize = new Point();
getWindowManager().getDefaultDisplay().getSize(screenSize);
Bitmap temp = Bitmap.createBitmap(screenSize.x, screenSize.x, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(temp);
canvas.drawBitmap(imageBitmap,null, new Rect(0,0,screenSize.x,screenSize.x), null);
imageView.setImageBitmap(temp);
}
對我來說,android:scaleType =“centerCrop”沒有解決我的問題。 它實際上擴大了圖像的方式。 所以我嘗試使用android:scaleType =“fitXY”,它工作得很好。