如何使背景圖像適合視圖,但在使用<bitmap />
作爲背景可繪製xml時保持其縱橫比? 沒有<bitmap>
的android:gravity
值給出所需的效果。縮放圖像保持背景縱橫比可繪製
回答
只能在xml文件中實現操縱背景屬性是不可能的。有兩個選項:
你砍/分的位圖編程方式與
Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
並將其設置爲一些View
的背景。您使用
ImageView
而不是背景把它作爲第一個佈局的元素,併爲它指定android:scaleType
屬性:<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/backgrnd" android:scaleType="centerCrop" /> ... rest layout components here ... </RelativeLayout>
ScaleType的'centerCrop'選項對此非常有用。 – 2012-03-27 14:52:10
所有可能的'android:scaleType'值都不會提供所需的效果。僅當我沒有指定'scaleType'時,圖像才符合視圖的縱橫比。 – tilex 2012-03-28 07:25:18
謝謝,centerInside適合我。 – pengguang001 2016-08-25 01:10:43
另一種方法是創建您的正規圖像patch 9 images並讓它伸展按照您希望的方式縮放。
您可以通過在角落中放置9個補丁點來顯示內容的中心位置,以明顯保留您的比例(假設圖像的最外邊緣是可重複/透明的)。
希望你明白了。
android:scaleType="centerCrop"
這裏是可用的規模類型的完整列表: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
使用a.ch描述的方法,除了我用這對我需要什麼工作要好得多這個規模類型爲我工作的偉大
,在這裏您可以看到每種類型的一些示例:http://etcodehome.blogspot.de/2011/05/android-imageview-scaletype-samples.html – 2013-09-27 12:09:46
有一個簡單的方法來從繪製這樣做:
your_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@color/bg_color"/>
<item>
<bitmap
android:gravity="center|bottom|clip_vertical"
android:src="@drawable/your_image" />
</item>
</layer-list>
唯一的缺點是,如果沒有足夠的空間,您的圖像將不會完全顯示,但會被剪切,我無法找到一種方式直接從drawable中執行此操作。但是從測試中我發現它工作得很好,而且它不會剪切那麼多圖像。你可以玩更多的重力選項。
另一種方式將是剛剛創建的佈局,在那裏你會使用ImageView
並設置scaleType
到fitCenter
。
希望這些信息可以幫助你實現你想要的。
嘗試使用InsetDrawable(對我很好)。
只要給你這個可繪製的,並且從四邊中的任何一邊你想要的插入(或填充)。
InsetDrawable insetDrawable = new InsetDrawable(drawable, insetLeft, insetTop, insetRight, insetBottom);
它是專門用於設置大小小於視圖的背景可繪製。
在這裏看到: https://developer.android.com/reference/android/graphics/drawable/InsetDrawable.html
老問題,但沒有其他的答案爲我工作。然而,這種XML代碼所做的:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="@drawable/background_image"/>
</RelativeLayout>
如果您的位圖的寬度大於高,使用android:gravity="fill_vertical"
。否則,請使用android:gravity="fill_horitzonal"
。這與在ImageView
上使用android:scaleType="centerCrop"
具有相似的效果。
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="fill_vertical"
android:src="@drawable/image" />
如果您支持多個方向,您可以創建在drawable-port
文件夾和一個位圖的XML文件,另一個在drawable-land
文件夾中。
我想在我的自定義Drawable類中做類似的事情。 這裏是重要的部分:
public class CustomBackgroundDrawable extends Drawable
{
private Rect mTempRect = new Rect();
private Paint mBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
...
public void draw(@NonNull Canvas canvas)
{
Rect bounds = getBounds();
if (mBitmap != null) {
if (mScaleType == ScaleType.SCALE_FILL) {
//bitmap scales to fill the whole bounds area (bitmap can be cropped)
if (bounds.height() > 0 && bounds.height() > 0) {
float scale = Math.min(mBitmap.getWidth()/(float)bounds.width(), mBitmap.getHeight()/(float)bounds.height());
float bitmapVisibleWidth = scale * bounds.width();
float bitmapVisibleHeight = scale * bounds.height();
mTempRect.set((int)(mBitmap.getWidth()-bitmapVisibleWidth)/2, 0, (int)(bitmapVisibleWidth+mBitmap.getWidth())/2, (int)bitmapVisibleHeight);
canvas.drawBitmap(mBitmap, mTempRect, bounds, mBitmapPaint);
}
} else if (mScaleType == ScaleType.SCALE_FIT) {
//bitmap scales to fit in bounds area
if (bounds.height() > 0 && bounds.height() > 0) {
float scale = Math.min((float)bounds.width()/mBitmap.getWidth(), (float)bounds.height()/mBitmap.getHeight());
float bitmapScaledWidth = scale * mBitmap.getWidth();
float bitmapScaledHeight = scale * mBitmap.getHeight();
int centerPadding = (int)(bounds.width()-bitmapScaledWidth)/2;
mTempRect.set(bounds.left + centerPadding, bounds.top, bounds.right - centerPadding, bounds.top+(int)bitmapScaledHeight);
canvas.drawBitmap(mBitmap, null, mTempRect, mBitmapPaint);
}
}
}
}
通過這種方法,你可以靈活地應用,你需要
- 1. 背景圖像縮放,同時保持縱橫比
- 2. 縮放位圖並保持縱橫比
- 3. SVG剪裁的背景縮放 - 如何保持縱橫比?
- 4. CSS - 拉伸背景圖像 - 不保持縱橫比
- 5. 保持背景圖像的縱橫比(android)
- 6. 使用PHP和保持縱橫比的縮放圖像
- 7. Flexbox圖像縮放到高度並保持縱橫比
- 8. 在保持縱橫比的同時在中心繪製圖像
- 9. 如何以橫向適應縱向圖像在縱橫比保持縱橫比
- 10. CSS - 保持圖像縱橫比flexbox
- 11. 調整圖像保持縱橫比
- 12. 如何保持圖像縮略圖的縱橫比
- 13. 繪圖縱橫比
- 14. 縮放比例按鈕保持背景圖像
- 15. 保持背景圖像在流體內部縱橫比的問題SVG
- 16. 縮放圖像,保持縱橫比不被裁剪 - android應用程序
- 17. 通過使用CSS保持縱橫比縮放中心裁剪圖像
- 18. 保持縱橫比在OpenGL
- 19. 如何在保持縱橫比的同時縮放SVG矩形?
- 20. 可繪製圖像縮放
- 21. 如何保持縱橫比在它被設置爲一個的ImageView的背景的可繪製XML動畫
- 22. 保持設備相機點擊圖像的縱橫比(風景/人像)
- 23. 如何將位圖壓縮爲較小尺寸的圖像(保持縱橫比)
- 24. 如何在保持縱橫比的同時儘可能大地製作圖像
- 25. writeablebitmap縱橫比保存圖像
- 26. 保留圖像縱橫比在UICollectionViewCell
- 27. ImageMagick在不縮放圖像的情況下更改縱橫比
- 28. 關於窗口大小和縱橫比縮放圖像
- 29. 縮放圖像,但保持其比例
- 30. 縮放圖像並保持其比例
你是否已經嘗試過[ScaleType]任何規模的邏輯(http://developer.android.com/reference /android/widget/ImageView.ScaleType.html)? – 2012-03-27 14:03:46
嘗試android:scaleType =「matrix」 – Pavandroid 2012-03-27 14:52:24
ScaleType用於ImageView。位圖不支持它。 – OldSchool4664 2016-12-01 01:28:16