亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Android自定義ImageView:在圖片上添加一個圖層

標簽:
Android

先看下效果图:

01.jpg

这是两张前后对比图,右边第二张图里面的已抢光标签图片当已经没有商品的时候就会显示了,在每个图片的中心位置,第一想法是在ImageView的外层再套一层RelativeLayout

<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>

这样当然是可以的,然而如果XML布局本身就很复杂,用这样的写法又给View Tree加了一层,不够优雅,下面介绍另一种实现方式:自定义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:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@mipmap/yijia_default_bg" />

代码中拿到CenterImage的对象:

CenterImage mGoodsImg =(CenterImage)findViewById(R.id.GoodsImage);
mGoodsImg.setCenterImgShow(true);

当setCenterImgShow()里的invalidate()方法被调用后,CenterImage的onDraw()方法会重新被调用并重新绘制,这样就可以愉快地在ImageView的上面新加一个图层。

             

github地址:https://github.com/crazyqiang



作者:_小马快跑_
链接:https://www.jianshu.com/p/a3d9101b3c23


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消