定义:图片缩放,基于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>
<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);