代码动态设置图标的大小和位置的工具类


项目中经常需要实现一个界面中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设置一个方向的图标。两个图标的话,右边默认是箭头,无法进行个性化定制。


猜你喜欢

转载自blog.csdn.net/Deaful/article/details/52956362