package com.example.hui;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setContentView(new CustomView(this));
}
class CustomView extends View{
Paint paint;
//private ArrayList<PointF> graphics = new ArrayList<PointF>();
// PointF point;
public CustomView(Context context) {
super(context);
paint = new Paint(); //设置一个笔刷大小是3的黄色的画笔
paint.setColor(Color.RED);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(3);
}
//在这里我们将测试canvas提供的绘制图形方法
@Override
protected void onDraw(Canvas canvas) {
//绘制黑色背景
Paint b = new Paint();
b.setColor(Color.BLACK);
RectF rect = new RectF(0, 0, 900, 900);
canvas.drawRect(rect, b);
//画大圆圈
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
canvas.translate(canvas.getWidth()/2, 200); //将位置移动画纸的坐标点:150,150
canvas.drawCircle(0, 0, 100, paint); //画圆圈
//使用path绘制路径文字
canvas.save();
canvas.translate(-75, -75);
Path path = new Path();
path.addArc(new RectF(0,0,150,150), -180, 180);
Paint citePaint = new Paint(paint);
citePaint.setTextSize(14);
citePaint.setStrokeWidth(1); //设置画笔的宽度
canvas.drawTextOnPath("ninngsnhenngcnai@2015080558s", path, 28, 0, citePaint);
canvas.restore();
//画刻度
Paint tmpPaint = new Paint(paint); //小刻度画笔对象
tmpPaint.setStrokeWidth(1); //设置画笔的宽度
float y=100;
int count = 60; //总刻度数
for(int i=0 ; i <count ; i++){
if(i%5 == 0){
canvas.drawLine(0f, y, 0, y+12f, paint);
canvas.drawText(String.valueOf(i/5+1), -4f, y+25f, tmpPaint);
}else{
canvas.drawLine(0f, y, 0f, y +5f, tmpPaint);
}
canvas.rotate(360/count,0f,0f); //旋转画纸
}
//绘制指针
tmpPaint.setColor(Color.GRAY); //设置画笔颜色
tmpPaint.setStrokeWidth(4); //设置画笔的宽度
canvas.drawCircle(0, 0, 7, tmpPaint); //画圆
tmpPaint.setStyle(Style.FILL);
tmpPaint.setColor(Color.YELLOW);
canvas.drawCircle(0, 0, 5, tmpPaint);
canvas.drawLine(0, 10, 0, -65, paint);
//画圆弧
// canvas.drawCircle(0, 0, 200, paint); //画圆圈
RectF oval=new RectF();
oval.set(80, 80, 200, 200);
canvas.drawArc(oval, 100, 100, true, paint);//true 连接半径线, false 不连接
// //画线
// canvas.drawLines(new float[]{
// 10,10, // 线的起始坐标
// 20,20, // 线的终点坐标
//
// 30,30, //....
// 40,40,
//
// 50,50,
// 60,60,
//
// 70,70,
// 80,80,
//
// 120,120,
// 150,150
// }, tmpPaint);
super.onDraw(canvas);
}
}
}
android canvas 画闹钟 圆弧
最新推荐文章于 2023-03-07 15:28:57 发布