Android Drawable Resources系列9:<scale>

定义:图片缩放,基于level来控制其显示。

用法:

<?xml version="1.0" encoding="utf-8"?>
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/drawable_resource"
    android:scaleGravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                          "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                          "center" | "fill" | "clip_vertical" | "clip_horizontal"]
    android:scaleHeight="percentage"
    android:scaleWidth="percentage" />
属性作用
android:drawable图片资源
android:scaleGravity缩放中心,就是向什么方位上缩放
android:scaleHeight值:XX%:12.5%,20%。缩放比例,20%就是缩放20%,剩下80%
android:scaleWidth值:XX%:12.5%,20%。缩放比例,20%就是缩放20%,剩下80%

效果:

1、XML文件中resource_drawable_scale.xml:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@mipmap/reasource_drawable_mn4"
    android:scaleGravity="center_vertical|center_horizontal"
    android:scaleHeight="20%"
    android:scaleWidth="20%">
</scale>

2、layout文件中,原始图片是同一张:
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:src="@mipmap/reasource_drawable_mn4" />

        <ImageView
            android:id="@+id/img_scale"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:src="@drawable/resource_drawable_scale" />

3、Activity中,level必须大于0,否则无法显示:
img_scale = (ImageView) findViewById(R.id.img_scale);
        ScaleDrawable scaleDrawable = (ScaleDrawable) img_scale.getDrawable();
        scaleDrawable.setLevel(1);





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值