缩放图像在可绘制的背景中保持其纵横比 [英] Scale image keeping its aspect ratio in background drawable
本文介绍了缩放图像在可绘制的背景中保持其纵横比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当使用
作为背景可绘制 XML 时,如何使背景图像适合视图但保持其纵横比?
的 android:gravity
值都没有达到预期的效果.
How do I make a background image fit the view but keep its aspect ratio when using <bitmap />
as a background drawable XML?
None of <bitmap>
's android:gravity
values gives the desired effect.
推荐答案
仅在 xml 文件中无法实现操作背景属性.有两种选择:
It is impossible to achieve manipulating background attribute within xml-files only. There are two options:
您可以使用编程方式剪切/缩放位图
位图.createScaledBitmap(位图源,int dstWidth,int dstHeight,boolean filter)
并将其设置为一些View
的背景.
您使用 ImageView
而不是背景将其作为第一个布局的元素并指定 android:scaleType
属性:
You use ImageView
instead of background placing it as the first layout's element and specify android:scaleType
attribute for it:
<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>
这篇关于缩放图像在可绘制的背景中保持其纵横比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文