Android自定义ImageView实现在图片上添加图层效果
作者:_小马快跑_ 时间:2023-01-12 01:03:32
首先我们先看下效果图
实现思路
这是两张前后对比图,右边第二张图里面的已抢光标签图片当已经没有商品的时候就会显示了,在每个图片的中心位置,第一想法是在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的上面新加一个图层。
总结
标签:android,imageview,图层
0
投稿
猜你喜欢
Spring与Struts整合之让Spring管理控制器操作示例
2022-08-22 12:53:46
java的主要特性学习总结
2021-11-16 05:46:19
C# 将透明图片的非透明区域转换成Region的实例代码
2021-10-25 19:28:05
java中两个byte数组实现合并的示例
2021-10-16 20:26:48
Android实现Activity水平和垂直滚动条的方法
2021-07-04 13:06:06
Java线程安全和锁Synchronized知识点详解
2023-01-23 03:59:38
Android Studio使用Kotlin时,修改代码后运行不生效的解决方法
2022-08-05 11:29:04
Java编程中的检查型异常与非检查型异常分析
2023-11-04 13:08:38
Java实现图片拼接
2023-02-28 23:01:27
@RequestBody的使用详解
2023-03-08 04:17:28
C#中在WebClient中使用post发送数据实现方法
2023-05-01 00:03:54
JAVA与SQL 中的null与NULL解析
2023-06-23 11:51:18
使用Enumeration和Iterator遍历集合类详解
2023-01-05 11:57:51
了解Java线程池创建过程
2022-09-29 20:45:49
android实现QQ微信侧滑删除效果
2021-07-11 15:58:11
Java读取txt文件和写入txt文件的简单实例
2022-01-01 05:04:16
Winform实现将网页生成图片的方法
2022-09-06 13:39:31
基于Rxjava实现轮询定时器
2021-12-27 07:22:10
详解C#读写Excel的几种方法
2022-10-23 14:23:28
MyBatis核心源码深度剖析SQL语句执行过程
2022-11-09 01:05:39