Android中自定义ImageView添加文字设置按下效果详解

作者:芯_空 时间:2021-10-08 08:13:14 

前言

我们在上一篇文章教大家使用ImageView+TextView的组合自定义控件...可能在开发中你还需要其他功能,例如:按下效果,可以在代码中改变字体颜色,更换图片等等...

首先上效果图,看看是否是你需要的

Android中自定义ImageView添加文字设置按下效果详解
效果图

下面开始撸代码

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>

属性文件存放位置如下图

Android中自定义ImageView添加文字设置按下效果详解
文件位置

下面我们来看看具体的调用方法

Android中自定义ImageView添加文字设置按下效果详解
布局调用

当然我们也可以在Activity中进行再次设置, 例如:

Android中自定义ImageView添加文字设置按下效果详解
在java中设置

这些都是在自定义View中的set方法...也可以根据具体的业务增删set方法.

来源:http://www.jianshu.com/p/4b3d599945ef

标签:android,自定义imageview,文字
0
投稿

猜你喜欢

  • Java使用Graphics2D绘制SVG和PNG的方法

    2021-11-13 01:03:51
  • 深入了解Java线程池的原理使用及性能优化

    2023-02-17 22:35:31
  • springboot2.5.6集成RabbitMq实现Topic主题模式(推荐)

    2021-10-03 22:01:22
  • 详解Java线程池队列中的延迟队列DelayQueue

    2023-08-30 01:22:04
  • Android点击按钮返回顶部实现代码

    2023-04-10 19:06:40
  • SpringMVC教程之文件上传与下载详解

    2022-12-21 03:49:09
  • 实例讲解Java 自旋锁

    2021-09-17 09:38:50
  • java的多线程高并发详解

    2022-06-28 10:12:06
  • MyBatis-Plus代码生成器的使用详解

    2022-09-04 23:14:40
  • C#委托与匿名委托详解

    2023-02-24 21:12:41
  • MultipartFile中transferTo(File file)的路径问题及解决

    2023-11-12 00:07:08
  • SpringBoot整合Mybatis简单实现增删改查

    2023-07-05 15:33:44
  • java多线程CyclicBarrier的使用案例,让线程起步走

    2023-05-20 16:22:22
  • Android的异步任务AsyncTask详解

    2021-10-13 02:58:28
  • C#信号量用法简单示例

    2022-09-21 12:04:41
  • Android开发之滑动数值选择器NumberPicker用法示例

    2022-08-04 07:22:36
  • Spring cloud alibaba之Ribbon负载均衡实现方案

    2021-11-08 20:05:23
  • C#解决多IfElse判断语句和Switch语句问题的方法分享

    2023-04-06 18:36:22
  • Java SpringBoot自动装配原理详解

    2022-09-08 01:15:09
  • Java控制台输入数组并逆序输出的方法实例 <font color=red>原创</font>

    2023-07-13 23:32:26
  • asp之家 软件编程 m.aspxhome.com