缩放图像保存在后台绘制的长宽比 [英] Scale image keeping its aspect ratio in background drawable
问题描述
我如何使用&LT时的背景图像适合的看法,但保留其纵横比位图/>
作为背景绘制的XML?
没有了<位图>
的机器人:重力
值给出了预期的效果。
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:
-
您剪切/编程的规模与位图 <一href="http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap%28android.graphics.Bitmap,%20int,%20int,%20boolean%29"><$c$c>Bitmap.createScaledBitmap(Bitmap SRC,INT dstWidth,INT dstHeight, 布尔过滤器) ,并设置一些
查看
的背景。
You cut/scale the bitmap programmatically with
Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
and set it as someView
's background.
您使用的ImageView
,而不是背景把它作为第一个布局的元素,并指定<一href="http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType"><$c$c>android:scaleType$c$c>属性是:
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屋!