自定义属性步骤
来源:互联网 发布:百度云搜索cms程序 编辑:程序博客网 时间:2024/06/05 18:54
1、values目录下,创建attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RoundProgress">
<attr name="roundColor" format="color"/>
<attr name="roundProgressColor" format="color"/>
<attr name="roundWidth" format="dimension"></attr>
<attr name="textColor" format="color"/>
<attr name="textSize" format="dimension"/>
</declare-styleable>
</resources>
2、在布局文件中使用时,添加命名空间,AS可以直接alt+enter自动生成
然后就可以在布局中使用了
xmlns:app="http://schemas.android.com/apk/res-auto"
<cn.wade.RoundProgress
android:id="@+id/p_progresss"
android:layout_width="120dp"
android:layout_height="120dp"
app:roundColor="@android:color/darker_gray"
app:roundProgressColor="@android:color/holo_red_dark"
app:roundWidth="10dp"
app:textColor="#18b4ed"
app:textSize="20sp">
</cn.wade.RoundProgress>
3、在自定义view的三个参数的构造方法中使用
public RoundProgress(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgress);
//圆环的颜色
roundColor = mTypedArray.getColor(R.styleable.RoundProgress_roundColor, Color.RED);
//圆环进度的颜色
roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgress_roundProgressColor, Color.GREEN);
//中间进度百分比文字字符串的颜色
textColor = mTypedArray.getColor(R.styleable.RoundProgress_textColor, Color.GREEN);
//中间进度百分比的字符串的字体大小
textSize = mTypedArray.getDimension(R.styleable.RoundProgress_textSize, 15);
//圆环的宽度
roundWidth = mTypedArray.getDimension(R.styleable.RoundProgress_roundWidth, 5);
mTypedArray.recycle();
}
用context.obtainStyledAttributes(attrs, R.styleable.RoundProgress);来获取自定义的属性数组,然后根据情况获取具体的属性,如定义的时color就通过getColor()获取
4、附上自定义的一个圆弧进度圈代码
public class RoundProgress extends View {
private Paint paint = new Paint();
private int roundColor;
private int roundProgressColor;
private int textColor;
private float textSize;
private float roundWidth;
private int max = 100;
private int progress = 50;
public RoundProgress(Context context) {
this(context, null);
}
public RoundProgress(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RoundProgress(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgress);
//圆环的颜色
roundColor = mTypedArray.getColor(R.styleable.RoundProgress_roundColor, Color.RED);
//圆环进度的颜色
roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgress_roundProgressColor, Color.GREEN);
//中间进度百分比文字字符串的颜色
textColor = mTypedArray.getColor(R.styleable.RoundProgress_textColor, Color.GREEN);
//中间进度百分比的字符串的字体大小
textSize = mTypedArray.getDimension(R.styleable.RoundProgress_textSize, 15);
//圆环的宽度
roundWidth = mTypedArray.getDimension(R.styleable.RoundProgress_roundWidth, 5);
mTypedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
//第一步:绘制一个最外层的圆
paint.setColor(roundColor);
paint.setStrokeWidth(roundWidth);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
int center = getWidth() / 2;
int radius = (int) (center - roundWidth / 2);
canvas.drawCircle(center, center, radius, paint);
//第二步:绘制正中间的文本
float textWidth = paint.measureText(progress + "%");
paint.setColor(textColor);
paint.setTextSize(textSize);
paint.setStrokeWidth(0);
canvas.drawText(progress + "%", center - textWidth / 2, center + textSize / 2, paint);
//第三步:
/**
* 参数解释:
* oval:绘制弧形圈所包含的矩形范围轮廓
* 0:开始的角度
* 360 * progress / max:扫描过的角度
* false:是否包含圆心
* paint:绘制弧形时候的画笔
*/
RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius);
paint.setColor(roundProgressColor);
paint.setStrokeWidth(roundWidth);
paint.setStyle(Paint.Style.STROKE);
canvas.drawArc(oval, 0, 360 * progress / max, false, paint);
}
public void setProgress(int progress){
this.progress = progress;
if(progress>100){
this.progress = 100;
}
postInvalidate();
}
}
- 自定义属性步骤
- 自定义属性步骤
- android 自定义控件属性步骤
- 自定义属性TypedArray的使用步骤
- Android自定义属性的使用步骤
- Android中自定义属性基本步骤
- View自定义属性步骤与分析
- Android实现自定义View的自定义属性的一般步骤
- Android-自定义view里实现自定义属性步骤
- Android自定义View的基本步骤和使用自定义属性
- Android wiki:06.自定义组合控件属性的步骤
- 自定义组合控件动态,静态设置属性的步骤
- Android中自定义控件属性的操作步骤
- 创建具有属性值的MOSS2007自定义字段实例(包括步骤与源代码)
- 自定义属性
- 自定义属性
- 自定义属性
- 自定义属性
- There is no Action mapped for namespace [/] and action name [hello] associated with context path [/d
- websocket即时通信
- Linux下安装MariaDB10
- mysql下载安装配置详细教程
- Focusable 属性和IsTabStop 属性之间的关系
- 自定义属性步骤
- PAT 乙级 1011. A+B和C (15)
- 2017.2.5 Java有感6
- bzoj 2440 二分+莫比乌斯函数和容斥原理
- Subsets II
- 【一年半以后复习】从头学习刘汝佳
- wxpython,窗口,控件,事件
- 在Ubuntu服务器上搭建LAMP环境.
- gulp源码解析(二)—— vinyl-fs_0