JAVA + 圆球 + 改变 + 填充 + %分比

本文介绍了一个使用Android自定义View实现动态绘制圆形、弧形及文字的示例。通过继承View并重写onDraw方法,实现了每50毫秒更新一次UI显示进度百分比的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.com.lianxi_01.MainActivity">
<com.example.com.lianxi_01.MyView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


</LinearLayout>

自定义View:

package com.example.com.lianxi_01;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by 李小龙 on 2018/4/12.
 */

public class MyView extends View {

    private Paint paint;
    private int yc=0;//改变度数
    public MyView(Context context) {
        this(context,null);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();     //定义画笔
        paint.setAntiAlias(true);//抗锯齿
        paint.setStyle(Paint.Style.STROKE);//画笔样式 描边

    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            invalidate();//重新绘制的方法
        }
    };
    @Override
    protected void onDraw(Canvas canvas) {
        drawCircle(canvas);//画圆
        drawArc(canvas);   //画弧形
        drawText(canvas);  //文字

        new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(50);
                    yc++;
                    if (yc>=101){

                        return;

                    }
                    postInvalidate();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
    //画圆形方法
    public void drawCircle(Canvas canvas){
        paint.setStrokeWidth(20);  //画笔宽度
        paint.setColor(Color.GRAY);
        canvas.drawCircle(200,190,100,paint);
    }
    //画弧形方法
    public void drawArc(Canvas canvas){
        paint.setStrokeWidth(20);   //画笔宽度
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setColor(Color.RED);  //添加颜色
        RectF rectF = new RectF(100,90,300,290);
        canvas.drawArc(rectF,-90,yc*360/100,false,paint);


    }
    //中心文字方法
    public void drawText(Canvas canvas){
        paint.setStrokeWidth(0);//画笔粗细
        paint.setTextSize(25);  //文字大小
        canvas.drawText(yc+"%",10,200,paint);
    }

}

主布局:

什么也没写。。。。

Java实现一个模型、两个视图和两个控制器的功能软件,即采用MVC模式或者说是观察者模式,本程序通过输入球体半径,显示球体形状,面积体积等 Sphere.java package Model; import java.util.Observable; public class Sphere extends Observable { private double radius;//球体半径 private double area;//球体面积 private double volume;//球体体积 public Sphere() { radius=100d; area=4*Math.PI*Math.pow(radius, 2); volume=4*Math.PI*Math.pow(radius, 3)/3; } public double getRadius() { return radius; } public double getArea() { return area; } public double getVolume() { return volume; } public void setRadius(double radius) { this.radius = radius; this.area = 4*Math.PI*Math.pow(radius, 2); this.volume=4*Math.PI*Math.pow(radius, 3)/3; this.setChanged(); this.notifyObservers(); } } textView.java package View; import java.util.Observer; import java.util.Observable; import java.text.NumberFormat; import javax.swing.*; import Controller.TextController; import Model.Sphere; import java.awt.*; import java.awt.event.*; public class TextView extends JPanel implements Observer { private JLabel radiusLab;//提示用户输入球体半径 private JTextField radiusTextField;//接受用户输入球体半径 //private JLabel radiusRang; private JLabel areaLab;//显示球体面积 private JTextField areaTextField;//显示输入球体半径对应的面积 private JLabel volumeLab;//显示球体体积 private JTextField volumeTextField;//显示输入球体半径对应的体积 public TextView() { try { Init(); } catch(Exception e) { e.printStackTrace(); } } private void Init() throws Exception { radiusLab=new JLabel("球体半径"); radiusLab.setForeground(new Color(0,165,168)); //radiusRang=new JLabel("[0-200]"); radiusTextField = new JTextField(12); radiusTextField.setForeground(new Color(223,100,158)); radiusTextField.setBackground(new Color(210,204,230)); areaLab=new JLabel("球体面积"); areaLab.setForeground(new Color(0,165,168)); areaTextField = new JTextField(12); areaTextField.setBackground(new Color(193,219,219)); areaTextField.setEditable(fal
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值