【5年Android从零复盘系列之二】Android中使用xml代码绘制Drawable资源图 详解
发布日期:2021-06-29 18:17:30
浏览次数:2
分类:技术文章
本文共 6359 字,大约阅读时间需要 21 分钟。
1.Drawable
Android中,所有能显示出来的都抽象定义为一个父类Drawable[可绘制的图形],
包括但不限于图片、色块、画板、背景等。1.1 Drawable资源
- drawable文件放在res目录的各个drawable目录下;
- \res\drawable一般放的是描述性的xml文件;
- 图片文件一般放在具体分辨率的mipmap-xdpi或drawable-xdpi目录下;
1.2 Drawable多状态图形
StateListDrawable可以根据不同的手势状态变化图形,对应xml中的selector
引用的 shape_btn_blue_radius5.xml
引用的 shape_btn_gray_radius5.xml
1.3 多状态非引用
2.shape :Android中最常用的xml绘图
2.1 先举例
2.2 详解
3.selector扩展
3.1 文字颜色选择器,要使用android:color=“ARGB|RGB颜色值”
3.2 使用 animated-rotate
3.3 java代码动态创建selector
public static void createSelector(Context context, View view, @DrawableRes int... resIds) { StateListDrawable stateListDrawable = new StateListDrawable(); Drawable drawablePre = ContextCompat.getDrawable(context, resIds[0]); Drawable drawableDef = ContextCompat.getDrawable(context, resIds[1]); stateListDrawable.addState(new int[]{ android.R.attr.state_pressed, android.R.attr.state_enabled}, drawablePre); stateListDrawable.addState(new int[0], drawableDef); stateListDrawable.setState(new int[0]); stateListDrawable.setEnterFadeDuration(200); stateListDrawable.setExitFadeDuration(200); view.setBackground(stateListDrawable); }
3.4 java代码动态创建selector辅助类
public class SelectorHelper { //常用状态数组 public static final int[] STATE_DEFAULT = new int[0]; public static final int[] STATE_PRESSED = new int[]{ android.R.attr.state_pressed, android.R.attr.state_enabled}; public static final int[] STATE_SELECTED = new int[]{ android.R.attr.state_selected}; public static final int[] STATE_CHECKED = new int[]{ android.R.attr.state_checked}; public static final int[] STATE_UNCHECKED = new int[]{ -android.R.attr.state_checked}; public static final int[] STATE_DISABLED = new int[]{ -android.R.attr.state_enabled}; public static final int[] STATE_FOCUSED = new int[]{ android.R.attr.state_focused}; /** * 动态创建一个简单的选择器 * @param context * @param view * @param resId */ public static void createSelector(Context context, View view, @DrawableRes int... resId) { StateListDrawable stateListDrawable = new StateListDrawable(); Drawable drawablePre = ContextCompat.getDrawable(context, resId[0]); Drawable drawableDef = ContextCompat.getDrawable(context, resId[1]); stateListDrawable.addState(STATE_PRESSED, drawablePre); stateListDrawable.addState(new int[0], drawableDef); stateListDrawable.setState(new int[0]); stateListDrawable.setEnterFadeDuration(200); stateListDrawable.setExitFadeDuration(200); view.setBackground(stateListDrawable); } /** * 通过对图片的着色创建选择器 * @param context * @param view * @param isVector * @param resId */ public static void createSelector(Context context, View view, boolean isVector, @DrawableRes int resId) { StateListDrawable stateListDrawable = new StateListDrawable(); Drawable drawableSrc; if (isVector) { drawableSrc = VectorDrawableCompat.create(context.getResources(), resId, context.getTheme()); } else { drawableSrc = ContextCompat.getDrawable(context, resId); } stateListDrawable.addState(STATE_PRESSED, drawableSrc); stateListDrawable.addState(new int[0], drawableSrc); stateListDrawable.setState(new int[0]); Drawable wrapDrawable = DrawableCompat.wrap(stateListDrawable); ColorStateList colorStateList = new ColorStateList(new int[][]{ STATE_PRESSED, new int[0] }, new int[]{ 0XFFFF0000, 0xFF00FF00}); DrawableCompat.setTintList(wrapDrawable, colorStateList); view.setBackground(stateListDrawable); } /** * 文字颜色选择器 * @param v */ public static void createSelector(TextView v) { ColorStateList colorStateList = new ColorStateList(new int[][]{ STATE_PRESSED, new int[0] }, new int[]{ Color.CYAN, Color.BLACK}); v.setTextColor(colorStateList); } /** * * @param context 上下文 * @param view View * @param isVector 是否为矢量图 * @param resId 图片数组 */ public static void createSelector(Context context, View view, boolean isVector, @DrawableRes int... resId) { StateListDrawable stateListDrawable = new StateListDrawable(); Drawable drawableDis; Drawable drawablePre; Drawable drawableSel; Drawable drawableDef; if (isVector) { drawableDis = VectorDrawableCompat.create(context.getResources(), resId[0], context.getTheme()); drawablePre = VectorDrawableCompat.create(context.getResources(), resId[1], context.getTheme()); drawableSel = VectorDrawableCompat.create(context.getResources(), resId[2], context.getTheme()); drawableDef = VectorDrawableCompat.create(context.getResources(), resId[3], context.getTheme()); } else { drawableDis = ContextCompat.getDrawable(context, resId[0]); drawablePre = ContextCompat.getDrawable(context, resId[1]); drawableSel = ContextCompat.getDrawable(context, resId[2]); drawableDef = ContextCompat.getDrawable(context, resId[3]); } stateListDrawable.addState(STATE_DISABLED, drawableDis); stateListDrawable.addState(STATE_PRESSED, drawablePre); stateListDrawable.addState(STATE_SELECTED, drawableSel); stateListDrawable.addState(STATE_DEFAULT, drawableDef); stateListDrawable.setState(STATE_DEFAULT); stateListDrawable.setEnterFadeDuration(200); stateListDrawable.setExitFadeDuration(200); view.setBackground(stateListDrawable); }}
转载地址:https://cupster.blog.csdn.net/article/details/111933730 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月28日 14时16分45秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
必须了解的mysql三大日志-binlog、redo log和undo log
2019-04-29
局部敏感哈希Locality Sensitive Hashing归总
2019-04-30
图像检索中为什么仍用BOW和LSH
2019-04-30
图˙谱˙马尔可夫过程˙聚类结构----by林达华
2019-04-30
深度学习读书笔记之AE(自动编码AutoEncoder)
2019-04-30
深度学习读书笔记之RBM
2019-04-30
深度学习word2vec笔记之基础篇
2019-04-30
法国INRIA Data Sets & Images 数据集和图像库
2019-04-30
训练自己haar-like特征分类器并识别物体(1)
2019-04-30
iOS容易造成循环引用的三种场景,就在你我身边!
2019-04-30
有一个会做饭的女友是一种怎样的体验?
2019-04-30
Storm入门之第一章
2019-04-30
java提高篇之数组(1):认识JAVA数组
2019-04-30
java提高篇之数组(2)
2019-04-30
浅析数据一致性
2019-04-30
Java 多维数组遍历
2019-04-30
深入分析Java中的length和length()
2019-04-30
数据库连接池浅析
2019-04-30