自定义View饼图/圆柱体/时钟

自定义View饼图/圆柱体/时钟

饼图/圆柱体

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();
}
}

效果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 可以使用 Apache POI 库来操作 Excel 文件,其中也包括创建饼图和环形图。 以下是一个简单的示例代码,可以创建一个包含饼图的 Excel 文件: ``` import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; public class PieChartExample { public static void main(String[] args) throws Exception { // 创建 Excel 文件 XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Pie Chart"); // 创建数据行 Row row; Cell cell; row = sheet.createRow(0); cell = row.createCell(0); cell.setCellValue("Category"); cell = row.createCell(1); cell.setCellValue("Value"); row = sheet.createRow(1); cell = row.createCell(0); cell.setCellValue("Apples"); cell = row.createCell(1); cell.setCellValue(20); row = sheet.createRow(2); cell = row.createCell(0); cell.setCellValue("Oranges"); cell = row.createCell(1); cell.setCellValue(30); row = sheet.createRow(3); cell = row.createCell(0); cell.setCellValue("Pears"); cell = row.createCell(1); cell.setCellValue(10); row = sheet.createRow(4); cell = row.createCell(0); cell.setCellValue("Bananas"); cell = row.createCell(1); cell.setCellValue(40); // 创建饼图 XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 5, 1, 15, 15); XSSFChart chart = drawing.createChart(anchor); chart.setTitleText("Fruit Sales"); chart.setTitleOverlay(false); XDDFChartLegend legend = chart.getOrAddLegend(); legend.setPosition(org.apache.poi.xddf.usermodel.chart.LegendPosition.TOP_RIGHT); XDDFDataSource<String> categories = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(1, 4, 0, 0)); XDDFNumericalDataSource<Double> values = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 4, 1, 1)); XDDFChartData data = chart.createData(ChartTypes.PIE, null, null); data.setVaryColors(true); data.addSeries(categories, values); chart.plot(data); // 保存文件 FileOutputStream fileOut = new FileOutputStream("PieChartExample.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); } } ``` 这个示例代码创建了一个 Excel 文件,包含一个名为 "Pie Chart" 的工作表,该工作表包含一个简单的数据表格和一个饼图。你可以根据需要修改数据和图表的设置来满足你的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值