最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

我們在上一篇文章教大家使用ImageView+TextView的組合自定義控件...可能在開發(fā)中你還需要其他功能,例如:按下效果,可以在代碼中改變字體顏色,更換圖片等等...

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


效果圖

下面開始擼代碼

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;
    //按下時顯示的圖片
    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;
    //按下時顯示的文字顏色
    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;
   //移動
   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è)置默認的圖片
  *
  * @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è)置字體顏色(默認為黑色)
  *
  * @param color
  */
 private void setTextColor(int color) {
  if (color == 0) {
   this.mTextView.setTextColor(Color.BLACK);
  } else {
   this.mTextView.setTextColor(getResources().getColor(color));
  }
 }

 /**
  * 設(shè)置默認的顏色
  *
  * @param color 顏色ID
  */
 public void setTextDefaultColor(int color) {
  textColorId = color;
  setTextColor(color);
 }

 /**
  * 設(shè)置按下的顏色
  *
  * @param color 顏色ID
  */
 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>

屬性文件存放位置如下圖


文件位置

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


布局調(diào)用

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


在java中設(shè)置

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

總結(jié)

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

相關(guān)文章

  • Android?完整購物商城界面的實現(xiàn)案例

    Android?完整購物商城界面的實現(xiàn)案例

    這篇文章為大家?guī)硪粋€Android?完整購物商城的界面具體的實現(xiàn),案例中含有商品列表的顯示,為商城最重要的功能之一,感興趣的朋友來看看吧
    2022-03-03
  • Android自定義日歷滑動控件

    Android自定義日歷滑動控件

    這篇文章主要為大家詳細介紹了Android自定義日歷滑動控件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android實現(xiàn)加載圈

    Android實現(xiàn)加載圈

    這篇文章主要為大家詳細介紹了Android實現(xiàn)加載圈,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Android仿淘寶商品拖動查看詳情及標(biāo)題欄漸變功能

    Android仿淘寶商品拖動查看詳情及標(biāo)題欄漸變功能

    這篇文章主要介紹了Android仿淘寶商品拖動查看詳情及標(biāo)題欄漸變功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android中Bitmap用法實例分析

    Android中Bitmap用法實例分析

    這篇文章主要介紹了Android中Bitmap用法,結(jié)合實例形式分析了Android操作圖片的載入、屬性設(shè)置、旋轉(zhuǎn)等相關(guān)技巧,需要的朋友可以參考下
    2016-02-02
  • Android之Viewpager+Fragment實現(xiàn)懶加載示例

    Android之Viewpager+Fragment實現(xiàn)懶加載示例

    本篇文章主要介紹了Android之Viewpager+Fragment實現(xiàn)懶加載示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Flutter有狀態(tài)組件使用詳解

    Flutter有狀態(tài)組件使用詳解

    這篇文章主要為大家詳細介紹了Flutter有狀態(tài)組件的使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Flutter持久化存儲之?dāng)?shù)據(jù)庫存儲(sqflite)詳解

    Flutter持久化存儲之?dāng)?shù)據(jù)庫存儲(sqflite)詳解

    這篇文章主要給大家介紹了關(guān)于Flutter持久化存儲之?dāng)?shù)據(jù)庫存儲的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Android界面數(shù)據(jù)懶加載實現(xiàn)代碼

    Android界面數(shù)據(jù)懶加載實現(xiàn)代碼

    這篇文章主要為大家分享了Android界面數(shù)據(jù)懶加載實現(xiàn)代碼,告訴大家怎樣實現(xiàn)界面即Fragment的懶加載,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android 系統(tǒng)net和wap接入點的區(qū)別

    Android 系統(tǒng)net和wap接入點的區(qū)別

    這篇文章主要介紹了Android 系統(tǒng)net和wap接入點的區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2016-09-09

最新評論

贵阳市| 宝兴县| 罗山县| 福鼎市| 台中市| 乐昌市| 行唐县| 和政县| 永城市| 富平县| 佛坪县| 灵山县| 平泉县| 兰考县| 都江堰市| 青阳县| 商洛市| 饶河县| 平舆县| 那坡县| 格尔木市| 宜丰县| 湘乡市| 大邑县| 贵阳市| 陆良县| 祁阳县| 定西市| 印江| 金平| 皮山县| 健康| 葵青区| 旬阳县| 内丘县| 三原县| 紫金县| 新余市| 响水县| 丹凤县| 阜南县|