当前位置: 移动技术网 > IT编程>移动开发>Android > Android中自定义ImageView添加文字设置按下效果详解

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

chengrendian,肖恩雅思胜经,河北省最低工资标准

前言

我们在教大家使用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中进行再次设置, 例如:


在java中设置

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网