Android(安卓)自定义View基本用法
16lz
2021-01-26
Android 自定义View基本用法
看完鸿洋大神的自定义view一二三四, 写一下自己的总结:
format="color" />
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCircleView, defStyle, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.MyCircleView_firstColor:
mFirstColor = a.getColor(attr, Color.GREEN);
break;
case R.styleable.MyCircleView_secondColor:
mSecondColor = a.getColor(attr, Color.RED);
break;
case R.styleable.MyCircleView_firstCircleWidth:
mFirstCircleWidth = a.getInt(attr, 20);
break;
case R.styleable.MyCircleView_secondCircleWidth:
mSecondCircleWidth = a.getInt(attr, 30);
break; case R.styleable.MyCircleView_splitSize:
mSplitSize = a.getInt(attr, 40);
break;
}
}
a.recycle(); mPaint = new Paint();
然后重写onMeasure()方法,计算view的位置, 如果在布局文件中是match_parent或者给出了大小,就不需要重写,如果是wrap_content,则需要重写, 否则这个view会填充整个屏幕。如果重写了这个函数,则必须要setMeasureDimension(width, heigth), 将测量的width和height传到onDraw中使用。
最后是onDraw()方法, 在给定的位置画View。在这里,我定义了两个圆,那么就是要计算两个圆的半径, 中心坐标都是getWidth()/2 , getHeight()/2, 两个在正中心的同心圆。
关键的是半径: 第一个圆半径:getWidth()/2 - mFirstCircleWidth()/2; 第二个圆半径:getWidth()/2 - mFirstCircleWidth - mSplitSize - mSecondCircleWidth()/2;
xmlns:tools="http://schemas.android.com/tools"
xmlns:test="http://schemas.android.com/apk/res/com.example.mycircleview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_width="200dp"
android:layout_height="200dp" android:layout_centerInParent="true" test:firstColor="#D4F668" test:secondColor="#2F9DD2" test:firstCircleWidth="20" test:secondCircleWidth="30" test:splitSize="30" />
看完鸿洋大神的自定义view一二三四, 写一下自己的总结:
一、自定义View的基本步骤:
- 自定义属性:在res/values目录下创建attrs.xml
- 自定义一个View:extends View, 获取自定义属性
- 在布局文件中使用自定义的View
二、自定义属性:
format="color"
三、自定义一个View
首先在构造方法中获取刚才定义的一堆属性TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCircleView, defStyle, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.MyCircleView_firstColor:
mFirstColor = a.getColor(attr, Color.GREEN);
break;
case R.styleable.MyCircleView_secondColor:
mSecondColor = a.getColor(attr, Color.RED);
break;
case R.styleable.MyCircleView_firstCircleWidth:
mFirstCircleWidth = a.getInt(attr, 20);
break;
case R.styleable.MyCircleView_secondCircleWidth:
mSecondCircleWidth = a.getInt(attr, 30);
break; case R.styleable.MyCircleView_splitSize:
mSplitSize = a.getInt(attr, 40);
break;
}
}
a.recycle(); mPaint = new Paint();
然后重写onMeasure()方法,计算view的位置, 如果在布局文件中是match_parent或者给出了大小,就不需要重写,如果是wrap_content,则需要重写, 否则这个view会填充整个屏幕。如果重写了这个函数,则必须要setMeasureDimension(width, heigth), 将测量的width和height传到onDraw中使用。
最后是onDraw()方法, 在给定的位置画View。在这里,我定义了两个圆,那么就是要计算两个圆的半径, 中心坐标都是getWidth()/2 , getHeight()/2, 两个在正中心的同心圆。
关键的是半径: 第一个圆半径:getWidth()/2 - mFirstCircleWidth()/2; 第二个圆半径:getWidth()/2 - mFirstCircleWidth - mSplitSize - mSecondCircleWidth()/2;
三、在xml中使用:
xmlns:test="http://schemas.android.com/apk/res/com.example.mycircleview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="200dp"
更多相关文章
- Android开发实践 Intent 解析
- android:shape妙用
- 【Android】TabLayout 自定义指示器 Indicator 样式
- Android(安卓)System Property
- Android各种信息的显示
- Android(安卓)SDK 22.0.1 引用 android-support-v4 的问题
- Android(安卓)manifest文件中的标签详细介绍
- android的充电图标显示
- Android中TabLayout切换选项背景和修改字体大小