Android中自定义ImageView添加文字设置按下效果详解
作者:芯_空 发布时间:2021-10-08 08:13:14
标签:android,自定义imageview,文字
前言
我们在上一篇文章教大家使用ImageView+TextView的组合自定义控件...可能在开发中你还需要其他功能,例如:按下效果,可以在代码中改变字体颜色,更换图片等等...
首先上效果图,看看是否是你需要的
下面开始撸代码
MyImageTextView.java
public class MyImageTextView extends LinearLayout {
private ImageView mImageView = null;
private TextView mTextView = null;
private int imageId, pressImageId;
private int textId, textColorId, textTopId, pressTextColorId;
public MyImageTextView(Context context) {
this(context, null);
}
public MyImageTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyImageTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setOrientation(LinearLayout.VERTICAL);//设置垂直排序
this.setGravity(Gravity.CENTER);//设置居中
if (mImageView == null) {
mImageView = new ImageView(context);
}
if (mTextView == null) {
mTextView = new TextView(context);
}
if (attrs == null)
return;
int count = attrs.getAttributeCount();
for (int i = 0; i < count; i++) {
String attrName = attrs.getAttributeName(i);//获取属性名称
//根据属性获取资源ID
switch (attrName) {
//显示的图片
case "image":
imageId = attrs.getAttributeResourceValue(i, 0);
break;
//按下时显示的图片
case "pressImage":
pressImageId = attrs.getAttributeResourceValue(i, 0);
break;
//显示的文字
case "text":
textId = attrs.getAttributeResourceValue(i, 0);
break;
//设置文字颜色
case "textColor":
textColorId = attrs.getAttributeResourceValue(i, 0);
break;
//设置文字距离上面图片的距离
case "textTop":
textTopId = attrs.getAttributeResourceValue(i, 0);
break;
//按下时显示的文字颜色
case "pressTextColor":
pressTextColorId = attrs.getAttributeResourceValue(i, 0);
break;
}
}
init();
}
/**
* 初始化状态
*/
private void init() {
this.setText(textId);
mTextView.setGravity(Gravity.CENTER);//字体居中
this.setTextColor(textColorId);
this.setTextPaddingTop(textTopId);
this.setImgResource(imageId);
addView(mImageView);//将图片加入
addView(mTextView);//将文字加入
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
//按下
case MotionEvent.ACTION_DOWN:
if (pressImageId != 0)
this.setImgResource(pressImageId);
if (pressTextColorId != 0)
this.setTextColor(pressTextColorId);
break;
//移动
case MotionEvent.ACTION_MOVE:
break;
//抬起
case MotionEvent.ACTION_UP:
if (imageId != 0)
this.setImgResource(imageId);
if (textColorId != 0)
this.setTextColor(textColorId);
break;
}
return super.onTouchEvent(event);
}
/**
* 设置默认的图片
*
* @param resourceID 图片id
*/
public void setImgResourceDefault(int resourceID) {
imageId = resourceID;
setImgResource(resourceID);
}
/**
* 设置按下的图片
*
* @param resourceID 图片id
*/
public void setImgResourcePress(int resourceID) {
pressImageId = resourceID;
}
/**
* 设置显示的图片
*
* @param resourceID 图片ID
*/
private void setImgResource(int resourceID) {
if (resourceID == 0) {
this.mImageView.setImageResource(0);
} else {
this.mImageView.setImageResource(resourceID);
}
}
/**
* 设置显示的文字
*
* @param text
*/
public void setText(int text) {
this.mTextView.setText(text);
}
/**
* 设置字体颜色(默认为黑色)
*
* @param color
*/
private void setTextColor(int color) {
if (color == 0) {
this.mTextView.setTextColor(Color.BLACK);
} else {
this.mTextView.setTextColor(getResources().getColor(color));
}
}
/**
* 设置默认的颜色
*
* @param color 颜色ID
*/
public void setTextDefaultColor(int color) {
textColorId = color;
setTextColor(color);
}
/**
* 设置按下的颜色
*
* @param color 颜色ID
*/
public void setTextPressColor(int color) {
pressImageId = color;
}
/**
* 设置字体大小
*
* @param size
*/
public void setTextSize(float size) {
this.mTextView.setTextSize(size);
}
/**
* 设置文字与上面的距离
* @param top
*/
public void setTextPaddingTop(int top) {
if (top != 0)
this.mTextView.setPadding(0, getResources().getDimensionPixelOffset(top), 0, 0);
}
}
下面是属性文件
image_text.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="imageText">
<attr name="image" format="integer" />
<attr name="pressImage" format="integer" />
<attr name="text" format="integer" />
<attr name="textColor" format="integer" />
<attr name="pressTextColor" format="integer" />
<attr name="textTop" format="integer" />
</declare-styleable>
</resources>
属性文件存放位置如下图
文件位置
下面我们来看看具体的调用方法
当然我们也可以在Activity中进行再次设置, 例如:
这些都是在自定义View中的set方法...也可以根据具体的业务增删set方法.
来源:http://www.jianshu.com/p/4b3d599945ef
0
投稿
猜你喜欢
- 归并排序里运用到算法里很重要的一个思想——分治法:将原问题分解为几个规模较小但类似于原问题的子问题——《算法导论》。在每一层递归中都有3个步
- 一、RequestMapping注解RequestMapping注解的作用是建立请求URL和处理方法之间的对应关系RequestMappin
- 概述AOP(Aspect Orient Programming),我们一般称为面向方面(切面)编程,作为面向对象的一种补充,用于处理系统中分
- Java是一门面向对象的语言,即一切皆是对象!那么为何数据类型中还分为:基本类型和对象?Java中有8种基本数据类型boolean、byte
- 第一次是接触Lambda表达式是在TypeScript中(JavaScript的超集中),当时是为了让TypeScript的this方法外而
- SpringBoot运行Test时报错运行Test时的报错信息:SpringBoot Unable to find a @SpringBoo
- 1.现象描述原来项目在Android studio 2.3一切正常,升级3.0之后报如下错误:Error:Cannot choose bet
- 1.什么是thread当我们提及多线程的时候会想到thread和threadpool,这都是异步操作,threadpool其实就是threa
- 先看实例 代码如下 classip { privatestaticlongiptolong(stringstrip) //将127.0.0.
- 初看 cgaolei 翻译的 Java技巧之双括弧初始化 一文,走马观花,只知用法,未细看后面的解释。蔚为惊艳,心里想 Java 竟然有这么
- 准备:wildfly/tomcat或者其他服务器你的数据库的Driver,(此处用的mysql-connecter-java-5.1.39-
- 前言在一些项目中,经常会遇到需要把当前线程中的上下文传递到其他线程中的情况,比如某项目包含国际化操作,在业务请求进来时需要把对应的国家代码存
- 背景在最近的项目中,有一个需求是对一个很大的数据库进行查询,数据量大概在几千万条。但同时对查询速度的要求也比较高。这个数据库之前在没有使用P
- 本篇博客我们继续的来聊SpringMVC的东西,下方我们将会聊到js、css这些静态文件的加载配置,以及服务器推送的两种实现方式。当然我们在
- 简介本文介绍InheritableThreadLocal的用法。ThreadLocal可以将数据绑定当前线程,如果希望当前线程的Thread
- 工厂方法模式定义: Define an interface for creating an object, but let subclass
- 在项目中,经常会遇到页面分割,最常见的系统或网站的主界面。主页面分为,上面系统简介、下面作者简介、左边系统功能菜单、右边则是菜单真正展示的界
- 本文实例分析了C#实现的24点游戏。分享给大家供大家参考。具体如下:1. 24点游戏规则及算法规则:给出4个自然数,找出能够求出24的四则运
- 首先给大家来讲一个我们遇到的一个奇怪的问题:1.我的一个springboot项目,用mvn install打包成jar,换一台有jdk的机器
- 在HotSpot虚拟机中,对象在内存中存储的布局可以分为三块区域:对象头(Header)、实例数据(Instance Data)和对齐填充(