菜鸟Android自定义View之旅——基础篇
来源:互联网 发布:龙虎榜数据查询 编辑:程序博客网 时间:2024/06/18 12:35
一、自定义view的步骤
- 1.自定义属性
- 2.在View类的构造函数中获得自定的属性
- 3.[重写onMeasure方法]:如果不需要支持wrap content属性,则不需重写,这样按照match parent处理
- 4.重写onDraw方法
1.自定义属性
- 在res/values/ 下建立一个attrs.xml , 在里面定义我们的属性和声明我们的整个样式
新建一个 declare-styleable name是自定义view的类名
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomTitleView"> <attr name="titleText" format="string" /> <attr name="titleTextColor" format="color" /> <attr name="titleTextSize" format="dimension" /> </declare-styleable> </resources>
我们定义了字体,字体颜色,字体大小3个属性,format是值该属性的取值类型:
一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;
- 然后在布局中声明我们的自定义View
首先需要添加xmlns:
Android studio 中当你在使用自定义的属性的时候可以给你默认添加xmlns,也可手动添加如下:
xmlns:app="http://schemas.android.com/apk/res-auto"
如果使用的是Eclipse(估计也没几个人用了,赶紧换吧)
手动添加 xmlns:custom="http://schemas.android.com/apk/res/com.example.customview01我们的命名空间,后面的包路径指的是项目的package
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.customview01.view.CustomTitleView android:layout_width="200dp" android:layout_height="100dp" app:titleText="3712" app:titleTextColor="#ff0000" app:titleTextSize="40sp" /> </RelativeLayout>2、在View的构造方法中,获得我们的自定义的样式
public CustomTitleView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); /** * 获得我们所定义的自定义样式属性 */ TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyle, 0); mTitleText = a.getString( R.styleable.CustomTitleView_titleText); // 默认颜色设置为黑色 mTitleTextColor = a.getColor(R.styleable.CustomTitleView_titleTextColor, Color.BLACK); // 默认设置为16sp,TypeValue也可以把sp转化为px mTitleTextSize = a.getDimensionPixelSize( R.styleable.CustomTitleView_titleTextSize, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); a.recycle(); }
3.onMeasure请参照我的另一篇博客:
http://blog.csdn.net/davidluo001/article/details/50129605
如果需要支持wrapContent 属性,则需要重写,否则不需要。
4.onDraw这个方法就是进行一些绘制的操作,具体可以查看API
- @Override
- protected void onDraw(Canvas canvas)
这里需要说明一点,cavas.draw***方法通常需要对canvas 传入一个Pain对象
这个对象不要再onDraw里面实例化,因为onDraw会频繁的调用,这样就造成了频繁的创建对象
正确的做法是:需要在构造函数中创建paint对象,然后在onDraw中使用的时候只需要改变属性即可。
这就是看到这个警告的原因:
Avoid object allocations during draw/layout operations (preallocate and reuse instead) less... (Ctrl+F1)
You should avoid allocating objects during a drawing or layout operation. These are called frequently, so a smooth UI can be interrupted by garbage collection pauses caused by the object allocations.
The way this is generally handled is to allocate the needed objects up front and to reuse them for each drawing operation.
Some methods allocate memory on your behalf (such as Bitmap.create
), and these should be handled in the same way.
1 0
- 菜鸟Android自定义View之旅——基础篇
- Android 自定义View绘图篇之基础
- Android 自定义View动画篇之基础
- Android自定义View之旅(基础篇1:坐标系)
- android基础之自定义view
- 笔记—自定义View之绘制基础
- android——自定义view基础
- Android自定义View基础——坐标系
- android自定义View基础之------android坐标系
- 自定义View之基础篇
- 自定义View基础之——初识View
- Android自定义View基础之自定义View分类及须知
- Android自定义View基础篇(三)之SwitchButton开关
- Android自定义View基础之MeasureSpec详解
- Android自定义View基础之onMeasure详解
- Android自定义View基础之onLayout详解
- Android自定义View基础之onDraw详解
- Android自定义View之绘制基础
- Redis配置文件详解
- Git的配置和设置
- 即时通讯整理(一)openfire
- ecshop 手机号码 邮箱 登录
- 【Developer Log】ProGuard扰码可执行JAR包
- 菜鸟Android自定义View之旅——基础篇
- Android中的Bitmap和Drawable
- 如何成为一名优秀的全栈工程师
- Activity的启动流程
- linux下maven的安装
- iOS 打包上传蒲公英和AppStore出问题
- 前端给挖坑
- mysql数据库的基本操作
- Redis中统计各种数据大小的方法