Android自定義ImageView實(shí)現(xiàn)在圖片上添加圖層效果
首先我們先看下效果圖

實(shí)現(xiàn)思路
這是兩張前后對(duì)比圖,右邊第二張圖里面的已搶光標(biāo)簽圖片當(dāng)已經(jīng)沒有商品的時(shí)候就會(huì)顯示了,在每個(gè)圖片的中心位置,第一想法是在ImageView的外層再套一層RelativeLayout
實(shí)現(xiàn)方法
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <SelectableRoundedImageView android:id="@+id/imageView" style="@style/margin_distance" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/youxuan_bg_shape_normol" android:contentDescription="@string/app_name" android:padding="1dp" android:scaleType="centerCrop" /> <ImageView android:id="@+id/iv_empty_pic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout>
這樣當(dāng)然是可以的,然而如果XML布局本身就很復(fù)雜,用這樣的寫法又給View Tree加了一層,不夠優(yōu)雅,下面介紹另一種實(shí)現(xiàn)方式:自定義View
public class CenterImage extends ImageView {
private Paint paint;
private boolean isCenterImgShow;
private Bitmap bitmap;
public void setCenterImgShow(boolean centerImgShow) {
isCenterImgShow = centerImgShow;
if (isCenterImgShow) {
bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
invalidate();
}
}
public CenterImage(Context context) {
super(context);
init();
}
public CenterImage(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CenterImage(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (isCenterImgShow && bitmap != null) {
canvas.drawBitmap(bitmap, getMeasuredWidth() / 2 - bitmap.getWidth() / 2, getMeasuredHeight() / 2 - bitmap.getHeight() / 2, paint);
}
}
}
XML中:
<com.henanjianye.soon.communityo2o.view.CenterImage android:id="@+id/goodsImage" android:layout_width="match_parent" android:layout_height="100dp" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:contentDescription="@string/app_name" android:scaleType="centerCrop" android:src="@mipmap/yijia_default_bg" />
代碼中拿到CenterImage的對(duì)象:
CenterImage mGoodsImg =(CenterImage)findViewById(R.id.GoodsImage); mGoodsImg.setCenterImgShow(true);
當(dāng)setCenterImgShow()里的invalidate()方法被調(diào)用后,CenterImage的onDraw()方法會(huì)重新被調(diào)用并重新繪制,這樣就可以愉快地在ImageView的上面新加一個(gè)圖層。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
- Android使用控件ImageView加載圖片的方法
- Android 自定義圓形頭像CircleImageView支持加載網(wǎng)絡(luò)圖片的實(shí)現(xiàn)代碼
- 詳解Android中Glide與CircleImageView加載圓形圖片的問題
- android imageview圖片居中技巧應(yīng)用
- Android實(shí)現(xiàn)ImageView圖片雙擊放大及縮小
- Android中ImageView使用網(wǎng)絡(luò)圖片資源的方法
- Android開發(fā)之imageView圖片按比例縮放的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)ImageView陰影和圖層效果
- Android實(shí)現(xiàn)ImageView圖片縮放和拖動(dòng)
- Android開發(fā)實(shí)現(xiàn)ImageView加載攝像頭拍攝的大圖功能
相關(guān)文章
Android 5.0 實(shí)現(xiàn)水波擴(kuò)散效果
這篇文章主要為大家詳細(xì)介紹了Android 5.0 實(shí)現(xiàn)水波擴(kuò)散效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android MenuItem 自定義長按事件的實(shí)現(xiàn)
這篇文章主要介紹了Android MenuItem 自定義長按事件的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
Android 逆向?qū)W習(xí)詳解及實(shí)例
本文主要介紹Android 逆向?qū)W習(xí),這里整理逆向?qū)W習(xí)的思路及學(xué)習(xí)要點(diǎn),并附示例代碼,幫助大家學(xué)習(xí)理解,有需要的小伙伴可以參考下2016-09-09
Flutter實(shí)現(xiàn)密碼強(qiáng)度校驗(yàn)結(jié)果的示例詳解
我們經(jīng)常在一些網(wǎng)站上看到這樣的密碼強(qiáng)度指示,使用三段線,分別用不同的顏色來表示弱密碼、中等強(qiáng)度密碼和強(qiáng)密碼,本篇我們就用?Flutter?來實(shí)現(xiàn)這樣一個(gè)密碼強(qiáng)度校驗(yàn)示例,希望對(duì)大家有所幫助2023-08-08
Android 中TabLayout自定義選擇背景滑塊的實(shí)例代碼
TabLayout是Android 的Material Design包中的一個(gè)控件,可以和V4包中的ViewPager搭配產(chǎn)生一個(gè)聯(lián)動(dòng)的效果。接下來通過本文給大家分享TabLayout自定義選擇背景滑塊的實(shí)例代碼,感興趣的朋友一起學(xué)習(xí)吧2016-10-10
Popupwindow 的簡單實(shí)用案例(顯示在控件下方)
下面小編就為大家?guī)硪黄狿opupwindow 的簡單實(shí)用案例(顯示在控件下方)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Android實(shí)戰(zhàn)打飛機(jī)游戲之子彈生成與碰撞以及爆炸效果(5)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)打飛機(jī)游戲之子彈生成與碰撞以及爆炸效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07

