Drawable (可绘制图像)
本文由 Luzhuo 编写,转发请保留该信息.
原文: https://blog.youkuaiyun.com/Rozol/article/details/87100169
Drawable是可绘制图像, 它并不是某一具体类型的图像.
从图像资源创建
图片资源是指放在res/drawable
下的图片, 现在一般都把图片放在res/mipmap
下了.
文件的类型支持PNG、JPG、GIF,一般只使用PNG格式的图片, 其他图片格式不推荐.
1.使用代码的方式
AppCompatImageView i = new AppCompatImageView(this);
i.setImageResource(R.mipmap.my_image);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayoutCompat mLinearLayout = new LinearLayoutCompat(this);
mLinearLayout.addView(i);
setContentView(mLinearLayout);
2.使用xml的方式
setContentView(R.layout.activity_png);
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/my_image"/>
</android.support.v7.widget.LinearLayoutCompat>
BitmapDrawable 图片
就是对图片进行描述, 从而进行一些简单的处理, 如平铺。
案例代码
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/v0"
android:tileModeX="repeat"
android:tileModeY="repeat"
android:antialias="true"
android:dither="true" >
</bitmap>
使用:设置背景即可
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/my_bitmap" />
其效果
详细参数
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@[package:]drawable/drawable_resource"
// 抗锯齿
android:antialias=["true" | "false"]
// 位图抖动 (当位图的像素配置与屏幕不同时(如ARGB8888位图和RGB565屏幕), 启动抖动)
android:dither=["true" | "false"]
// 位图过滤 (当位图收缩或拉伸使其平滑)
android:filter=["true" | "false"]
// 放置位置
android:gravity=["top" | "bottom" | "left" | "right" | "center" | "center_vertical" | "center_horizontal"
"fill_vertical" | "fill_horizontal" |
"fill" | "clip_vertical" | "clip_horizontal"]
// mipmap 提示
android:mipMap=["true" | "false"]
// 平铺模式: disabled:不平铺(默认) / clamp:复制边缘颜色 / repeat:水平和垂直重复 / mirror:水平和垂直, 交替处以使相邻图像相接
android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
TransitionDrawable
TransitionDrawable 是可转换的绘制对象, 支持两个item进行向前或向后转换, 实现两个Drawable之间过渡切换效果.
创建的xml文件放在res/drawable
下.
transition.startTransition(1000); // 向后转换
transition.resetTransition(); // 向前转换
从xml创建
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/image_expand">
<item android:drawable="@mipmap/image_collapse">
</transition>
标签支持的属性有
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >
<item
<!-- 可绘制对象资源 -->
android:drawable="@drawable/drawable_resource"
<!-- 唯一资源ID -->
android:id="@+id/resource_name"
<!-- 上下左右 偏移 -->
android:top="dimension"
android:bottom="dimension"
android:left="dimension"
android:right="dimension" />
</transition>
案例代码
Resources res = this.getResources();
TransitionDrawable transition = (TransitionDrawable) ResourcesCompat.getDrawable(res, R.drawable.my_transition, null);
ImageView image = findViewById(R.id.iv_stransition);
image.setImageDrawable(transition);
transition.startTransition(1000);
<ImageView
android:id="@+id/iv_stransition"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
效果
ShapeDrawable
ShapeDrawable主要用来绘制一些简单的形状、背景. 这个也是平时比较常用的.
创建的xml文件放在res/drawable
下.
可以定义的形状有: rectangle:矩形(默认) oval:椭圆 line:水平线(<stroke>定义线宽) ring:环形
rectangle:矩形(默认)特有属性: