亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android中自定義ImageView添加文字設(shè)置按下效果詳解

 更新時(shí)間:2017年08月23日 10:31:18   作者:芯_空  
這篇文章主要給大家介紹了關(guān)于Android中自定義ImageView添加文字設(shè)置按下效果的相關(guān)資料,實(shí)現(xiàn)后的效果非常利用用戶的體驗(yàn),文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)下吧。

前言

我們?cè)?a target="_blank" href="http://chabaoo.cn/article/121873.htm">上一篇文章教大家使用ImageView+TextView的組合自定義控件...可能在開(kāi)發(fā)中你還需要其他功能,例如:按下效果,可以在代碼中改變字體顏色,更換圖片等等...

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


效果圖

下面開(kāi)始擼代碼

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);//設(shè)置垂直排序
  this.setGravity(Gravity.CENTER);//設(shè)置居中
  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);//獲取屬性名稱
   //根據(jù)屬性獲取資源ID
   switch (attrName) {
    //顯示的圖片
    case "image":
     imageId = attrs.getAttributeResourceValue(i, 0);
     break;
    //按下時(shí)顯示的圖片
    case "pressImage":
     pressImageId = attrs.getAttributeResourceValue(i, 0);
     break;
    //顯示的文字
    case "text":
     textId = attrs.getAttributeResourceValue(i, 0);
     break;
    //設(shè)置文字顏色
    case "textColor":
     textColorId = attrs.getAttributeResourceValue(i, 0);
     break;
    //設(shè)置文字距離上面圖片的距離
    case "textTop":
     textTopId = attrs.getAttributeResourceValue(i, 0);
     break;
    //按下時(shí)顯示的文字顏色
    case "pressTextColor":
     pressTextColorId = attrs.getAttributeResourceValue(i, 0);
     break;

   }
  }
  init();

 }

 /**
  * 初始化狀態(tài)
  */
 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;
   //移動(dòng)
   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);
 }

 /**
  * 設(shè)置默認(rèn)的圖片
  *
  * @param resourceID 圖片id
  */
 public void setImgResourceDefault(int resourceID) {
  imageId = resourceID;
  setImgResource(resourceID);
 }

 /**
  * 設(shè)置按下的圖片
  *
  * @param resourceID 圖片id
  */
 public void setImgResourcePress(int resourceID) {
  pressImageId = resourceID;
 }


 /**
  * 設(shè)置顯示的圖片
  *
  * @param resourceID 圖片ID
  */
 private void setImgResource(int resourceID) {
  if (resourceID == 0) {
   this.mImageView.setImageResource(0);
  } else {
   this.mImageView.setImageResource(resourceID);
  }
 }


 /**
  * 設(shè)置顯示的文字
  *
  * @param text
  */
 public void setText(int text) {
  this.mTextView.setText(text);
 }

 /**
  * 設(shè)置字體顏色(默認(rèn)為黑色)
  *
  * @param color
  */
 private void setTextColor(int color) {
  if (color == 0) {
   this.mTextView.setTextColor(Color.BLACK);
  } else {
   this.mTextView.setTextColor(getResources().getColor(color));
  }
 }

 /**
  * 設(shè)置默認(rèn)的顏色
  *
  * @param color 顏色I(xiàn)D
  */
 public void setTextDefaultColor(int color) {
  textColorId = color;
  setTextColor(color);
 }

 /**
  * 設(shè)置按下的顏色
  *
  * @param color 顏色I(xiàn)D
  */
 public void setTextPressColor(int color) {
  pressImageId = color;
 }

 /**
  * 設(shè)置字體大小
  *
  * @param size
  */
 public void setTextSize(float size) {
  this.mTextView.setTextSize(size);
 }


 /**
  * 設(shè)置文字與上面的距離
  * @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>

屬性文件存放位置如下圖


文件位置

下面我們來(lái)看看具體的調(diào)用方法


布局調(diào)用

當(dāng)然我們也可以在Activity中進(jìn)行再次設(shè)置, 例如:


在java中設(shè)置

這些都是在自定義View中的set方法...也可以根據(jù)具體的業(yè)務(wù)增刪set方法.

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • 詳解android shape的使用總結(jié)

    詳解android shape的使用總結(jié)

    在Android程序開(kāi)發(fā)中,我們經(jīng)常會(huì)去用到Shape這個(gè)東西去定義各種各樣的形狀,本篇文章主要介紹了android shape的使用,有興趣的可以一起了解一下。
    2016-12-12
  • Android實(shí)現(xiàn)折線走勢(shì)圖

    Android實(shí)現(xiàn)折線走勢(shì)圖

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)折線走勢(shì)圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Android繪制跟隨手指移動(dòng)的小球

    Android繪制跟隨手指移動(dòng)的小球

    這篇文章主要為大家詳細(xì)介紹了Android繪制跟隨手指移動(dòng)的小球,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • 很贊的引導(dǎo)界面效果Android控件ImageSwitcher實(shí)現(xiàn)

    很贊的引導(dǎo)界面效果Android控件ImageSwitcher實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Android控件ImageSwitcher如何實(shí)現(xiàn)很贊的引導(dǎo)界面的具體代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android RecyclerView添加搜索過(guò)濾器的示例代碼

    Android RecyclerView添加搜索過(guò)濾器的示例代碼

    本篇文章主要介紹了Android RecyclerView添加搜索過(guò)濾器的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Android設(shè)置全屏代碼分享

    Android設(shè)置全屏代碼分享

    本文是安卓代碼分享的第一篇,給大家分享了一段簡(jiǎn)單的設(shè)置安卓全屏的代碼,后續(xù)還會(huì)為大家分享一些。
    2014-10-10
  • Android判斷軟鍵盤(pán)的狀態(tài)和隱藏軟鍵盤(pán)的簡(jiǎn)單實(shí)例

    Android判斷軟鍵盤(pán)的狀態(tài)和隱藏軟鍵盤(pán)的簡(jiǎn)單實(shí)例

    下面小編就為大家?guī)?lái)一篇Android判斷軟鍵盤(pán)的狀態(tài)和隱藏軟鍵盤(pán)的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-10-10
  • RecyclerView焦點(diǎn)跳轉(zhuǎn)BUG優(yōu)化的方法

    RecyclerView焦點(diǎn)跳轉(zhuǎn)BUG優(yōu)化的方法

    這篇文章主要介紹了RecyclerView焦點(diǎn)跳轉(zhuǎn)BUG優(yōu)化的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • 自定義ListView實(shí)現(xiàn)拖拽ListItem項(xiàng)交換位置(附源碼)

    自定義ListView實(shí)現(xiàn)拖拽ListItem項(xiàng)交換位置(附源碼)

    本文要實(shí)現(xiàn)的是拖拽ListView的Item項(xiàng),在布局方面還是用基于布局泵LayoutInflater來(lái)從不同的Layout模板拿到不同的布局然后將view返回,感興趣的朋友可以了解下哈
    2013-06-06
  • 詳解Android的網(wǎng)絡(luò)數(shù)據(jù)存儲(chǔ)

    詳解Android的網(wǎng)絡(luò)數(shù)據(jù)存儲(chǔ)

    LeanCloud是一種簡(jiǎn)單高效的數(shù)據(jù)和文件存儲(chǔ)服務(wù),本文主要介紹了利用LeanCloud來(lái)進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)的存儲(chǔ)的實(shí)現(xiàn)方法。具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧
    2016-12-12

最新評(píng)論