菜鸟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
  1. @Override  
  2.     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
原创粉丝点击