博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
代码动态设置图标的大小和位置的工具类
阅读量:2085 次
发布时间:2019-04-29

本文共 5561 字,大约阅读时间需要 18 分钟。

项目中经常需要实现一个界面中n个这种item,一开始我的实现方法是:LinearLayout包含,左边ImageView,中间TextView,右边ImageView。

布局优点:ImageView可直接设置大小。

布局缺点:绘制过度,控件数量多,xml代码行数多。

项目开发没那么紧张后,我开始重构代码,优化这个布局,我改用了TextView+drawableLeft+drawableRight的方式,实现了同样的效果。

布局优点:xml代码行数减少,简洁明了。

布局缺点:GPU绘制的值没什么变化,依然绘制过度,图标大小无法在xml中设置,只能代码设置。

我发现图标+文字的布局很常见,而图标的位置在文字的上下左右都有可能。于是我写了个工具类,可以代码动态设置图标的大小和位置。与大家分享,有什么不对和需要改进的地方也希望大家不吝指正。

package com.tecsun.zhaoqing.platform.utils;import android.content.Context;import android.graphics.drawable.Drawable;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.TextView;import com.tecsun.zhaoqing.platform.R;import com.tecsun.zhaoqing.platform.common.Constant;/** * @author Deaful * @description 代码设置图片尺寸和位置 * @date 2017/6/5 15:57 * @version V1.1 */public class DrawableSizeUtils {	//图片默认位置	private static final int POSITION = Constant.POSITION_TOP;	/**	 * 不指定图片位置和尺寸	 */	public static void setDrawableSize(Context mContext, View view, int resid) {		Drawable drawable = mContext.getResources().getDrawable(resid);		// / 这一步必须要做,否则不会显示.		drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());		setDrawable(view, POSITION, drawable);	}	/**	 * 指定图片的位置,不指定图片尺寸     */	public static void setDrawableSize(Context mContext, View view, int resid, int position) {		Drawable drawable = mContext.getResources().getDrawable(resid);		// / 这一步必须要做,否则不会显示.		drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());		setDrawable(view, position, drawable);	}	/**	 * 指定图片尺寸,不指定图片位置	 */	public static void setDrawableSize(Context mContext, View view, int resid,									   int width, int height) {		Drawable drawable = mContext.getResources().getDrawable(resid);		// / 这一步必须要做,否则不会显示.		drawable.setBounds(0, 0, mContext.getResources().getDimensionPixelSize(width), mContext.getResources().getDimensionPixelSize(height));		setDrawable(view, POSITION, drawable);	}	/**	 * 指定图片的位置和尺寸	 */	public static void setDrawableSize(Context mContext, View view, int resid, int position,									   int width, int height) {		Drawable drawable = mContext.getResources().getDrawable(resid);		// / 这一步必须要做,否则不会显示.		drawable.setBounds(0, 0, mContext.getResources().getDimensionPixelSize(width), mContext.getResources().getDimensionPixelSize(height));		setDrawable(view, position, drawable);	}	/**	 * 根据方位设置控件对应位置的图标     */	private static void setDrawable(View view, int position, Drawable drawable) {		switch (position) {			case Constant.POSITION_LEFT:// 左				if(view instanceof Button) {					((Button)view).setCompoundDrawables(drawable, null, null, null);				}				if(view instanceof TextView) {					((TextView)view).setCompoundDrawables(drawable, null, null, null);				}				if(view instanceof CheckBox) {					((CheckBox)view).setCompoundDrawables(drawable, null, null, null);				}				break;			case Constant.POSITION_TOP:// 上				if(view instanceof Button) {					((Button)view).setCompoundDrawables(null, drawable, null, null);				}				if(view instanceof TextView) {					((TextView)view).setCompoundDrawables(null, drawable, null, null);				}				if(view instanceof CheckBox) {					((CheckBox)view).setCompoundDrawables(null, drawable, null, null);				}				break;			case Constant.POSITION_RIGHT:// 右				if(view instanceof Button) {					((Button)view).setCompoundDrawables(null, null, drawable, null);				}				if(view instanceof TextView) {					((TextView)view).setCompoundDrawables(null, null, drawable, null);				}				if(view instanceof CheckBox) {					((CheckBox)view).setCompoundDrawables(null, null, drawable, null);				}				break;			case Constant.POSITION_BOTTOM:// 下				if(view instanceof Button) {					((Button)view).setCompoundDrawables(null, null, null, drawable);				}				if(view instanceof TextView) {					((TextView)view).setCompoundDrawables(null, null, null, drawable);				}				if(view instanceof CheckBox) {					((CheckBox)view).setCompoundDrawables(null, null, null, drawable);				}				break;			default:				break;		}	}	/**	 * 图片+文字+箭头,横向布局,无需指定图片尺寸	 */	public static void setDrawableSizes(Context mContext, View view, int resid) {		Drawable drawableLeft = mContext.getResources().getDrawable(resid);		Drawable drawableRight = mContext.getResources().getDrawable(				R.drawable.icon_arrow_right);		// 这一步必须要做,否则不会显示.		drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());		drawableRight.setBounds(0, 0, drawableRight.getMinimumWidth(),				drawableRight.getMinimumHeight());		setDrawables(view, drawableLeft, drawableRight);	}	/**	 * 图片+文字+箭头,横向布局,需要指定图片尺寸	 */	public static void setDrawableSizes(Context mContext, View view, int resid, int width, int height) {		Drawable drawableLeft = mContext.getResources().getDrawable(resid);		Drawable drawableRight = mContext.getResources().getDrawable(				R.drawable.icon_arrow_right);		// 这一步必须要做,否则不会显示.		drawableLeft.setBounds(0, 0, mContext.getResources().getDimensionPixelSize(width), mContext.getResources().getDimensionPixelSize(height));		drawableRight.setBounds(0, 0, drawableRight.getMinimumWidth(),				drawableRight.getMinimumHeight());		setDrawables(view, drawableLeft, drawableRight);	}	private static void setDrawables(View view, Drawable drawableLeft, Drawable drawableRight) {		if(view instanceof Button) {			((Button)view).setCompoundDrawables(drawableLeft, null, drawableRight, null);		}		if(view instanceof TextView) {			((TextView)view).setCompoundDrawables(drawableLeft, null, drawableRight, null);		}	}}
当然,这个工具类还是有局限的地方,仅适用于给Button(RadioButton也可以)或者TextView设置一个方向的图标。两个图标的话,右边默认是箭头,无法进行个性化定制。

你可能感兴趣的文章
Hadoop学习笔记—3.Hadoop RPC机制的使用
查看>>
Hadoop学习笔记—22.Hadoop2.x环境搭建与配置
查看>>
JTS Geometry关系判断和分析
查看>>
GIS基本概念
查看>>
Java文件操作①——XML文件的读取
查看>>
java学习总结之文件操作--ByteArrayOutputStream的用法
查看>>
Java生成和操作Excel文件
查看>>
Java的三种代理模式
查看>>
java静态代理与动态代理简单分析
查看>>
JTS Geometry关系判断和分析
查看>>
阿里巴巴十年Java架构师分享,会了这个知识点的人都去BAT了
查看>>
idea如何显示git远程与本地的更改对比?
查看>>
SDO_GEOMETRY结构说明
查看>>
oracle 的 SDO_GEOMETRY
查看>>
往oracle中插入geometry的两种方式
查看>>
Oracle Spatial中的Operator操作子 详细说明
查看>>
Oracle Spatial中SDO_Geometry详细说明
查看>>
oracle 聚合函数 LISTAGG ,将多行结果合并成一行
查看>>
Oracle列转行函数 Listagg() 语法详解及应用实例
查看>>
LISTAGG函数的用法
查看>>