Android自定义ImageView实现在图片上添加图层效果
作者:_小马快跑_ 发布时间:2023-01-12 01:03:32
标签: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的上面新加一个图层。
总结
0
投稿
猜你喜欢
- 题目一??解法/** * Definition for singly-linked list. * public class ListNod
- 前言在我们公司里,不同的服务之间通过Feign进行远程调用,但是,我们在尝试使调用可重试时遇到了一个小问题,Feign框架本身可以配置的自己
- 介绍在上一篇“SimpleAdapter“章节中,我们看到了把:ListView和Listview内
- 本文实例为大家分享了Unity实现俄罗斯方块第3部分,供大家参考,具体内容如下解决穿透问题逻辑部分1、在物体进行移动的过程中更新格子的信息,
- 问题遇到问题:在前后端分离跨域访问的项目中shiro进行权限拦截失效 (即使有正确权限的访问也会被拦截) 时造成302重定向错误等问题报错:
- 一、首先在主布局中,用帧布局来填充 RecycleView 和 两个模拟发送消息的Button<?xml version="
- 小程序官方流程图如下,官方地址 : https://developers.weixin.qq.com/miniprogram/dev/fra
- 查看代码执行mybatis的sql语句File–>Settings–>Plugins 搜索 MyBatis Log Plugin
- 一、WebSocket简介WebSocket协议通过在客户端和服务端之间提供全双工通信来进行Web和服务器的交互功能。在WebSocket应
- 1 修饰属性或者变量无论属性是基本类型还是引用类型,作用都是变量里面存放的“值”不可变经常和static关键字一起使用,作为常量1 基本类型
- package com;import java.util.Arrays; public class sjf { &nbs
- 本文实例为大家分享了Java实现串口通信的具体代码,供大家参考,具体内容如下1.介绍使用Java实现的串口通信程序,支持十六进制数据的发送与
- 解决办法:1.VCS--->Enable Version Control Integration2.选择要关联的版本工具补充:git
- 很多时候我们需要在Android设备上下载远程服务器上的图片进行显示,今天整理出两种比较好的方法来实现远程图片的下载。 方法一、直
- 本文实例为大家分享了C#遍历文件夹获取指定后缀名文件的具体代码,供大家参考,具体内容如下问题描述:项目需要,要进行某文件夹下所有shp数据的
- 说明:本文记录如何在Idea下,利用Maven管理项目,并整合SSM(Spring + Spring MVC +Mybatis)框架,实现简
- Java中获取整点时间戳在实际的开发过程中,前端给后端传时间的时候,有时候传的是整点数值,比如:timeList=[00,01,02,03,
- 在Flutter中实现整个App变为灰色是非常简单的,只需要在最外层的控件上包裹ColorFiltered,用法如下:@overrideWi
- springboot URL带有斜杠的转义字符百分之2F导致的400错误今天项目上出现一个问题,是前端的GET请求url中带有路径参数,这个
- webclient在调用DownloadData或者DownloadString的时候请求回来的数据出现乱码问题,解决办法如下:1、设置we