安卓学习笔记五
2015-12-17 02:47
615 查看
1自定义View
步骤:
1)自定义view的属性
2)在构造方法获得我们自定义的属性
3)重写onMeasure()方法 //非必需
4)重写onDraw()方法
在res/values下新建attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="titleText" format="string" />
<attr name="titleTextColor" format="color" />
<attr name="titleTextSize" format="dimension" />
<declare-styleable name="CustomView">
<attr name="titleText" />
<attr name="titleTextColor" />
<attr name="titleTextSize" />
</declare-styleable>
</resources>
上述我们定义了字内容,颜色,大小三个属性,和属性的取值类型。。
然后在布局文件中自定义我们View
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:tanlin="http://schemas.android.com/apk/res/com.example.customview" //我们应用程序的包名
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.customview.CustomView.CustomView //自定义View的全名或者自定义view包名下的类名
android:id="@+id/customView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tanlin:titleText="1234"
android:layout_centerInParent="true"
tanlin:titleTextColor="#ff0000ff"
tanlin:titleTextSize="40sp" />
</RelativeLayout>
当我们在布局文件中高度和宽度属性非确定值 android:layout_width="100dp" 和 android:layout_height="100dp",那么系统帮我们测量的宽度和高度是match_parent
所以当我们布局设置 wrap_content,我们需要自己测量,重写onMeasure方法,
重写之前先了解MeasureSpec的specMode,一共三种:
EXACTLY:一般是设置了明确的值或者是MATCH_PARENT
AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT
UNSPECIFIED:表示子布局想要多大就多大,很少使用
下面重写onMeasure方法代码:
int width = 0;
int height = 0;
/**
* 设置宽度
*/
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);
switch (specMode) {
case MeasureSpec.EXACTLY:// 明确指定
width = getPaddingLeft() + getPaddingRight() + specSize; //如果布局文件没有padding属性,则 getPaddingLeft()和 getPaddingRight()为0
break;
case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT
width = getPaddingLeft() + getPaddingRight() + mBound.width();
break;
}
/**
* 设置高度
*/
specMode = MeasureSpec.getMode(heightMeasureSpec);
specSize = MeasureSpec.getSize(heightMeasureSpec);
switch (specMode) {
case MeasureSpec.EXACTLY:// 明确指定了
height = getPaddingTop() + getPaddingBottom() + specSize;
break;
case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT
height = getPaddingTop() + getPaddingBottom() + mBound.height();
break;
}
setMeasuredDimension(width, height);
Log.i("tanlin", width + "宽度" + "草尖水露" + "高度" + height);
补充:
canvas.drawArc(rectBlackBg, 180, section * 360, false, mPaint); // 然后绘制进度圆环 ,startAngel为起始角度,为时钟3点钟方向开始;sweepAngel为扫过角度。
LinearGradient shader = new LinearGradient(0, 0, endX, endY, new int[]{startColor, midleColor, endColor},new float[]{0 , 0.5f, 1.0f}, TileMode.MIRROR);
其中参数new int[]{startColor, midleColor, endColor}是参与渐变效果的颜色集合,
其中参数new float[]{0 , 0.5f, 1.0f}是定义每个颜色处于的渐变相对位置,
这个参数可以为null,如果为null表示所有的颜色按顺序均匀的分布
步骤:
1)自定义view的属性
2)在构造方法获得我们自定义的属性
3)重写onMeasure()方法 //非必需
4)重写onDraw()方法
在res/values下新建attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="titleText" format="string" />
<attr name="titleTextColor" format="color" />
<attr name="titleTextSize" format="dimension" />
<declare-styleable name="CustomView">
<attr name="titleText" />
<attr name="titleTextColor" />
<attr name="titleTextSize" />
</declare-styleable>
</resources>
上述我们定义了字内容,颜色,大小三个属性,和属性的取值类型。。
然后在布局文件中自定义我们View
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:tanlin="http://schemas.android.com/apk/res/com.example.customview" //我们应用程序的包名
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.customview.CustomView.CustomView //自定义View的全名或者自定义view包名下的类名
android:id="@+id/customView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tanlin:titleText="1234"
android:layout_centerInParent="true"
tanlin:titleTextColor="#ff0000ff"
tanlin:titleTextSize="40sp" />
</RelativeLayout>
当我们在布局文件中高度和宽度属性非确定值 android:layout_width="100dp" 和 android:layout_height="100dp",那么系统帮我们测量的宽度和高度是match_parent
所以当我们布局设置 wrap_content,我们需要自己测量,重写onMeasure方法,
重写之前先了解MeasureSpec的specMode,一共三种:
EXACTLY:一般是设置了明确的值或者是MATCH_PARENT
AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT
UNSPECIFIED:表示子布局想要多大就多大,很少使用
下面重写onMeasure方法代码:
int width = 0;
int height = 0;
/**
* 设置宽度
*/
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);
switch (specMode) {
case MeasureSpec.EXACTLY:// 明确指定
width = getPaddingLeft() + getPaddingRight() + specSize; //如果布局文件没有padding属性,则 getPaddingLeft()和 getPaddingRight()为0
break;
case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT
width = getPaddingLeft() + getPaddingRight() + mBound.width();
break;
}
/**
* 设置高度
*/
specMode = MeasureSpec.getMode(heightMeasureSpec);
specSize = MeasureSpec.getSize(heightMeasureSpec);
switch (specMode) {
case MeasureSpec.EXACTLY:// 明确指定了
height = getPaddingTop() + getPaddingBottom() + specSize;
break;
case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT
height = getPaddingTop() + getPaddingBottom() + mBound.height();
break;
}
setMeasuredDimension(width, height);
Log.i("tanlin", width + "宽度" + "草尖水露" + "高度" + height);
补充:
canvas.drawArc(rectBlackBg, 180, section * 360, false, mPaint); // 然后绘制进度圆环 ,startAngel为起始角度,为时钟3点钟方向开始;sweepAngel为扫过角度。
LinearGradient shader = new LinearGradient(0, 0, endX, endY, new int[]{startColor, midleColor, endColor},new float[]{0 , 0.5f, 1.0f}, TileMode.MIRROR);
其中参数new int[]{startColor, midleColor, endColor}是参与渐变效果的颜色集合,
其中参数new float[]{0 , 0.5f, 1.0f}是定义每个颜色处于的渐变相对位置,
这个参数可以为null,如果为null表示所有的颜色按顺序均匀的分布
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories