Android自定义ImageView 在图片上添加一个图层

先看下效果图:

这是两张前后对比图,右边第二张图里面的已抢光标签图片当已经没有商品的时候就会显示了,在每个图片的中心位置,第一想法是在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:src="@mipmap/yijia_default_bg" />
复制代码

代码中拿到CenterImage的对象:

CenterImage mGoodsImg =(CenterImage)findViewById(R.id.GoodsImage);
mGoodsImg.setCenterImgShow(true);
复制代码

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

转载于:https://juejin.im/post/5a33e7f2f265da430703547b

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值