缩放图像保存在后台绘制的长宽比 [英] Scale image keeping its aspect ratio in background drawable

查看:181
本文介绍了缩放图像保存在后台绘制的长宽比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用&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:

  1. 您剪切/编程的规模与位图 <一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, 布尔过滤器) ,并设置一些查看的背景。

  1. You cut/scale the bitmap programmatically with Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) and set it as some View's background.

您使用的ImageView ,而不是背景把它作为第一个布局的元素,并指定<一href="http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType"><$c$c>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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆