通过学习《android开发艺术探索》和《android群英传》,还有一些著名博客的学习,写一些自学的过程和小列子。
直接上图:
- 把自定义的view进行分析,首先中间有一个文字,内容是一个随机数,当进行点击事件时,里面的数字会发生改变,而且外围的弧线的弧度随着数字而重绘invalidate(),不过因为数字的精度不够,所以还有待改进。
public class DrawArc extends View{
int x=100;
String text="36";
public DrawArc(Context context) {
super(context);
}
public DrawArc(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawArc(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width=getWidth();
int height=getHeight();
//绘制实心圆s
Paint paint2=new Paint();
paint2.setStyle(Paint.Style.FILL);//FILL设置画笔为实心,画出的圆是实心圆,STROKE则是一个圆圈
paint2.setColor(Color.YELLOW);
canvas.drawCircle(width/2,width/2,200,paint2);
//绘制文字
Paint paint3=new Paint();
paint3.setStyle(Paint.Style.STROKE);
paint3.setColor(Color.BLACK);
paint3.setTextSize(150);
Paint paint5=new Paint();
paint5.setStyle(Paint.Style.STROKE);
paint5.setColor(Color.BLACK);
paint5.setTextSize(50);
canvas.drawText(text,(width/2-115),width/2+50,paint3);
canvas.drawText("%",(width/2+50),width/2+50,paint5);
//绘制圆弧
Paint paint=new Paint();
paint.setColor(Color.YELLOW);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(90);
Paint paint4=new Paint();
paint4.setColor(Color.GREEN);
paint4.setStyle(Paint.Style.STROKE);
paint4.setStrokeWidth(90);
RectF rectF2=new RectF(300,300,width-300,width-300);//设置圆弧所在的外接圆的矩形
canvas.drawArc(rectF2, //圆弧所在的椭圆对象
0, //圆弧的起始角度
x, //圆弧的角度
false, //是否显示半径连接线,true表示显示圆弧与圆心半径连线,false表示不显示
paint4
);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
x= (int) (Math.random()*1000);
x=x%360;
System.out.println(x);
int y=x/36*10;
text=y+"";
invalidate();
}
return super.onTouchEvent(event);
}}
各位看官如果有意见或者更好的方法欢迎留言评论。。。。