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


猜你喜欢
- 提示:java.util.zipoutputstream
- Authentication使用SpringSecurity可以在任何地方注入Authentication进而获取到当前登录的用户信息,可谓
- Class:EcanRMB.cs using System; using System.Collections.Gen
- Spring Boot中的那些Conditionalspring boot中为我们提供了丰富的Conditional来让我们得以非常方便的在
- 一、背景在通过Runnable接口创建线程时,启动线程需要借助Thread类,这里就涉及到了静态代理模式。二、实例以歌手演出为例,在演出的这
- Session具有以下特点: (1)Session中的数据保存在服务器端; (2)Session中可以保存任意类型的数据; (2)Sessi
- 本文较为详细的分析了Java中对象的销毁方法。分享给大家供大家参考。具体分析如下:Java中的基本数据类型变量和对象的名称引用变量如定义在方
- using Microsoft.Win32 ; 1.读取指定名称的注册表的值 &nbs
- 开发中对版本进行检查并更新的需求基本是所有应用必须有的功能,可是在实际开发中有些朋友就容易忽略一些细节。版本更新的基本流程:一般是将本地版本
- 该配置基于IDEA2020.1版本,如后续有版本更新或者配置变更,再更新idea64.exe.vmoptions配置为提供IDEA启动速度和
- 在java数组中,查找数组元素是比较基础的操作了,arrays类的binarySearch就是专门实现指定元素的。同时它也属于我们常说的二分
- Android setButtonDrawable()的兼容问题解决办法setButtonDrawable()的兼容问题API1
- 恩 主要大家可以看下思路吧 图形界面里 除了图标和音乐两个资源 别的都是代码。 时间没有用timer组件 是自创的T
- //方法一//须添加对System.Web的引用//using System.Web.Security;/// <summary>
- 欣赏一下我们清爽的界面吧~如果是只用activity来制作这样的东西简直是太小儿科了,此处我们当然用的是service首先我们先上servi
- 本文实例讲述了WinForm绘制圆角的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.Co
- Java中 获取指定字符串在另一个字符串中出现的次数,供大家参考,具体内容如下/** * @param args */ public s
- 我就废话不多说了,大家还是直接看代码吧~public class GatewayContext { public static final
- 序言在flutter开发中,我们使用 bloc 框架,基于状态变更进行响应式开发。本篇文章,小轰将 bloc 核心业务块进行拆解简化,聊一聊
- 前言最近在学习spring,抽空会将学习的知识总结下面,本文我们会接触spring 4的新功能:@Conditional注解。在之前的spr