饼图/圆柱体
public class MyView3 extends View implements Runnable{
Paint paint = new Paint();
Paint paint1 = new Paint();
Paint paint2 = new Paint();
Paint paint3 = new Paint();
Paint paint4 = new Paint();
int width;
int height;
RectF rectF = new RectF();
int a;
int b;
int c;
public MyView3(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.YELLOW);
paint.setAntiAlias(true);
paint1.setStrokeWidth(10);
paint1.setStyle(Paint.Style.FILL);
paint1.setColor(Color.GRAY);
paint1.setAntiAlias(true);
paint2.setAntiAlias(true);
paint2.setStyle(Paint.Style.FILL);
paint2.setStrokeWidth(2);
paint2.setColor(Color.RED);
paint3.setAntiAlias(true);
paint3.setStyle(Paint.Style.FILL);
paint3.setStrokeWidth(2);
paint3.setColor(Color.BLUE);
paint4.setAntiAlias(true);
paint4.setStyle(Paint.Style.FILL);
paint4.setStrokeWidth(2);
paint4.setColor(Color.YELLOW);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
this.width=w;
this.height=h;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//柱状图
canvas.drawRect(170,200,230,400,paint1);
canvas.drawCircle(200,200,30,paint);
canvas.drawCircle(200,400,30,paint);
canvas.drawRect(270,200,330,400,paint1);
canvas.drawCircle(300,200,30,paint);
canvas.drawCircle(300,400,30,paint);
//饼图
canvas.translate(width/2,height/2);
int r = (Math.min(width, height) / 4);
rectF.set(-r,-r,r,r);
canvas.drawArc(rectF,a,130,true,paint2);
canvas.drawArc(rectF,b,120,true,paint3);
canvas.drawArc(rectF,c,120,true,paint4);
}
@Override
public void run() {
while (true){
a+=1;
b=a+130;
c=b+120;
postInvalidate();
try {
Thread.sleep(5);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyView3 myView3 = findViewById(R.id.myview3);
new Thread(myView3).start();
}
}
效果
时钟
public class MyView4 extends View implements Runnable{
int width;
int height;
Paint paint = new Paint();
Paint paint1 = new Paint();
Paint paint2 = new Paint();
Paint paint3 = new Paint();
Paint paint4 = new Paint();
RectF rectF = new RectF();
RectF rectF1 = new RectF();
RectF rectF2 = new RectF();
int x=270;//时
int y=270;//分
int z=270;//秒
public MyView4(Context context,AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
paint.setColor(Color.GRAY);
paint.setStrokeWidth(2);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint1.setColor(Color.BLUE);
paint1.setStrokeWidth(10);
paint1.setAntiAlias(true);
paint1.setStyle(Paint.Style.FILL);
paint2.setColor(Color.RED);
paint2.setStrokeWidth(6);
paint2.setTextSize(20);
paint2.setAntiAlias(true);
paint2.setStyle(Paint.Style.FILL);
paint3.setColor(Color.GRAY);
paint3.setStrokeWidth(6);
paint3.setAntiAlias(true);
paint3.setStyle(Paint.Style.FILL);
paint4.setColor(Color.BLACK);
paint4.setStrokeWidth(10);
paint4.setAntiAlias(true);
paint4.setStyle(Paint.Style.FILL);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width=w;
height=h;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(width/2,height/2,300,paint);
canvas.drawCircle(width/2,height/2,10,paint1);
canvas.drawLine(width/2,height/2-300,width/2,height/2-270,paint4);
canvas.drawLine(width/2,height/2+300,width/2,height/2+270,paint4);
canvas.drawLine(width/2-300,height/2,width/2-270,height/2,paint4);
canvas.drawLine(width/2+300,height/2,width/2+270,height/2,paint4);
canvas.drawText("12",width/2-10,height/2-250,paint2);
canvas.drawText("6",width/2+10,height/2+250,paint2);
canvas.drawText("9",width/2-250,height/2,paint2);
canvas.drawText("3",width/2+250,height/2,paint2);
canvas.translate(width/2,height/2);
int r = Math.min(width, height+20) / 4;//时
int r1 = Math.min(width, height+20) / 5;//分
int r2 = Math.min(width, height) / 6;//秒
rectF.set(-r,-r,r,r);
rectF1.set(-r1,-r1,r1,r1);
rectF2.set(-r2,-r2,r2,r2);
canvas.drawArc(rectF,x,0.5f,true,paint3);
canvas.drawArc(rectF1,y,0.5f,true,paint2);
canvas.drawArc(rectF2,z,0.5f,true,paint3);
}
@Override
public void run() {
while (true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
x+=6;
if(x==360){
x=0;
}
postInvalidate();
if (x==270){
y+=6;
if (y==360){
y=0;
}
if (y==270){
z+=30;
if (z==360){
z=0;
}
}
}
}
}
}
Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyView4 myView = findViewById(R.id.myview3);
new Thread(myView).start();
}
}
效果