




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
【移動應(yīng)用開發(fā)技術(shù)】Android中如何實現(xiàn)自定義ImageView添加文字設(shè)置按下效果
首先上效果圖,看看是否是你需要的效果圖/upload/information/20200623/125/126743.gif下面開始擼代碼MyImageTextView.javapublic
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);
}
}下面是屬性文件<?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"
form
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度珠寶企業(yè)社會責任與環(huán)保合作合同
- 二零二五年度汽車贈與及二手車置換增值服務(wù)合同
- 二零二五年度放棄祖屋繼承權(quán)的明確合同
- 2025年度石材幕墻安裝與維護管理合同協(xié)議
- 二零二五年度水資源保護融資合同
- 二零二五年度土地租賃合同糾紛處理指南
- 2025年度貨物損失賠償協(xié)議書:跨境電商供應(yīng)鏈風險分擔合同
- 二零二五年度師徒互助職業(yè)技能提升協(xié)議
- 二零二五年度足浴店轉(zhuǎn)讓與市場推廣合作框架協(xié)議
- 2025年度涂料行業(yè)綠色生產(chǎn)推廣合同
- 2025年天翼云解決方案架構(gòu)師認證考試指導題庫-上(單選題)
- 行為規(guī)范教育中學校長在國旗下講話:嚴格要求自己規(guī)范自己的行為
- 2024年12月廣東廣州市港務(wù)局直屬事業(yè)單位引進緊缺專業(yè)人才8人筆試歷年典型考題(歷年真題考點)解題思路附帶答案詳解
- 七下綜合世界真奇妙-共享“地球村”
- DBJ50-T-100-2022 建筑邊坡工程施工質(zhì)量驗收標準
- 2025年寧夏工商職業(yè)技術(shù)學院高職單招職業(yè)適應(yīng)性測試近5年??及鎱⒖碱}庫含答案解析
- 2025年信陽職業(yè)技術(shù)學院高職單招職業(yè)技能測試近5年??及鎱⒖碱}庫含答案解析
- DB11-T 1004-2023 房屋建筑使用安全檢查評定技術(shù)規(guī)程
- 《藝術(shù)與傳播》課件
- 烹飪安全知識培訓課件
- 2024年廣東職業(yè)技術(shù)學院高職單招語文歷年參考題庫含答案解析
評論
0/150
提交評論