ImageView设置边框的两种方式
发布日期:2021-06-30 11:11:19 浏览次数:2 分类:技术文章

本文共 1455 字,大约阅读时间需要 4 分钟。

MainActivity如下:

package cc.testimageviewbounds;import android.os.Bundle;import android.app.Activity;/** * Demo描述: * 给ImageView添加边框的两种实现方式 *  * 方式一: * 利用自定义的shape-->即此处的imageviewboundshape.xml * 且为ImageView设置background,即代码: * android:background="@drawable/imageviewboundshape" *  * 方式二: * 自定义ImageView * */public class MainActivity extends Activity {	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);	}}

ImageViewSubClass如下:

package cc.testimageviewbounds;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.util.AttributeSet;import android.widget.ImageView;public class ImageViewSubClass extends ImageView {	public ImageViewSubClass(Context context, AttributeSet attrs, int defStyle) {		super(context, attrs, defStyle);	}	public ImageViewSubClass(Context context, AttributeSet attrs) {		super(context, attrs);	}	public ImageViewSubClass(Context context) {		super(context);	}		@Override	protected void onDraw(Canvas canvas) {		super.onDraw(canvas);				//获取控件需要重新绘制的区域		Rect rect=canvas.getClipBounds();		rect.bottom--;		rect.right--;		Paint paint=new Paint();		paint.setColor(Color.RED);		paint.setStyle(Paint.Style.STROKE);		paint.setStrokeWidth(3);		canvas.drawRect(rect, paint);	}   }

main.xml如下:

imageviewboundshape.xml如下:

 

转载地址:https://it9527.blog.csdn.net/article/details/10526277 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Version 28 (intended for Android Pie and below) is the last version of the legacy support library
下一篇:Maven实用教程详解

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月08日 05时21分03秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

企业邮箱如何申请注册,邮箱申请如何免费注册? 2019-04-30
微信企业邮箱,手机邮箱格式地址怎么写? 2019-04-30
公司如何申请企业邮箱,公司邮箱怎么申请,公司企业邮箱哪个好? 2019-04-30
电子邮箱账号怎么申请,怎样申请邮箱账号呢 2019-04-30
邮箱怎么发邮件,邮件发信量多少,职场新人怎么发汇报邮件呢? 2019-04-30
maven 多层次pom 新引入包,编译成功,还是没有将包引入到本地 2019-04-30
leetCode2 两数相加 2019-04-30
【工具使用】使用pip与conda安装、更新与卸载Pytorch和torchvision 2019-04-30
【深度学习笔记】batchsize, time step(iteration), epoch 区别与联系 2019-04-30
【解决错误】ModuleNotFoundError No module named matplotlib 2019-04-30
【工具使用】Google免费云环境Colaboratory使用 2019-04-30
【深度学习笔记】卷积层,全连接层,池化层的相关输出参数计算 2019-04-30
【NLP学习笔记】文本分类概述 2019-04-30
【深度学习笔记】文本分类 2019-04-30
【转载】炼丹实验室:深度学习网络调参技巧 2019-04-30
【论文阅读笔记】Graph Convolutional Networks for Text Classification 2019-04-30
【论文阅读笔记】文本分类论文汇总 2019-04-30
【论文阅读笔记】Convolutional Neural Networks for Sentence Classification 2019-04-30
【NLP学习笔记】One-hot encoding:独热编码 2019-04-30
【工具使用】CSDN编辑器markdown字体、颜色与字号的设置 2019-04-30