没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Android自定义自定义View多种效果解析多种效果解析
主要为大家详细介绍了Android自定义View多种效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
最近项目里涉及到自定义View的东西还是挺多的,所以打算在自定义View上多花点时间,也顺便分享给大家。
先总结下自定义View的步骤:
1、自定义View的属性
2、在View的构造方法中获得我们自定义的属性
[3、重写onMeasure]
4、重写onDraw
1.首先在我们的res/values/目录下建立一个attrs.xml文件,然后在里面声明我们我们需要的自定义属性
我们定义了矩形的颜色,矩形的高度,矩形的宽度3个属性,format是指该属性的取值类型:
一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;
不太明白的可以google一下
然后在布局中声明我们的自定义View
需要引入xmls:app="http://schemas.android.com/apk/res-auto"这样就会自动查找我们的自定义属性,也可以采用另外一种引入方
式,xmlns:app="http://schemas.android.com/apk/res/com.example.administrator.demoone.customeview.CustomeRectView"
我们的命名空间,后面的包路径指的是项目的package
2.在View的构造方法里获取我们的自定义属性
public class CustomeRectView extends View {
private Paint mPiant;//定义画笔对象
private int rectColor;//矩形的颜色
private int rectHeight;//矩形的高度
private int rectWidth;//矩形的宽度
public CustomeRectView(Context context) {
this(context, null);
}
public CustomeRectView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomeRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
/**
* 获取我们自定义的属性
资源评论
weixin_38745925
- 粉丝: 28
- 资源: 890
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功