android 画笔 高度,Android画笔 --- 仪表

本文档展示了如何使用Android自定义View创建一个仪表盘组件,该组件在用户触摸时每次增加5%的进度,并通过代码详细解释了背景、进度和文字的绘制过程。此外,还提供了主函数中触摸事件的处理逻辑,用于更新仪表盘的进度。

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

效果展示:

初始界面:

a8503fab5c39

初始界面

中间界面(点击后进度每次增加5%):

a8503fab5c39

点击后进度每次增加5%

最终界面:

a8503fab5c39

最终界面

代码:

MeterView类:

注意:浮点类型存在误差,<= 1 实际可能有偏差,最好可能不能达到100%或者超出100%,需要更改数字的精度。

package swu.twj.a13_drawview;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Rect;

import android.graphics.RectF;

import android.text.TextPaint;

import android.util.AttributeSet;

import android.view.View;

/**

* 仪表盘

*/

public class MeterView extends View {

private Paint bgPaint;//背景画笔

private Paint progressPaint;//进度画笔

private Paint textPaint;//文字画笔

private float progress;//进度

public MeterView(Context context) {

super(context);

}

public MeterView(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

private void init(){

//背景画笔

bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

bgPaint.setColor(Color.BLACK);

bgPaint.setStyle(Paint.Style.STROKE);

bgPaint.setStrokeWidth(40);

bgPaint.setStrokeCap(Paint.Cap.ROUND);//设置两端的类型

progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

progressPaint.setColor(Color.MAGENTA);

progressPaint.setStyle(Paint.Style.STROKE);

progressPaint.setStrokeWidth(40);

progressPaint.setStrokeCap(Paint.Cap.ROUND);//设置两端的类型

textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);

textPaint.setColor(Color.BLACK);

textPaint.setTextSize(100);

}

/**

* 固定不变的 就不要放在onDraw方法里面

* 这个方法可能会被调用多次

* @param canvas

*/

@Override

protected void onDraw(Canvas canvas) {

//画初始圆

//确定矩形区域

RectF frame = new RectF(50,100,getWidth()-50,getWidth()-50);

//画一个弧

canvas.drawArc(frame,120,300,false,bgPaint);

//计算进度对应的角度

int angle = (int)(progress*300);

canvas.drawArc(frame,120,angle,false,progressPaint);

//文本内容

String text = (int)(progress*100)+"%";

//计算文字的宽度

int width = (int)textPaint.measureText(text);

//计算文字的矩阵 FontMetrics

Paint.FontMetricsInt fo = textPaint.getFontMetricsInt();

//文字的高度

int height = fo.bottom - fo.top;

//计算向下移动的距离 ascent/2

int space = -fo.ascent/2;

//画文字

canvas.drawText(text,getWidth()/2-width/2,getWidth()/2+space,textPaint);

}

public float getProgress() {

return progress;

}

public void setProgress(float progress) {

this.progress = progress;

//刷新 重绘

if (progress <= 1.0001) {

invalidate();

}

}

}

主函数:

创建触摸事件,每次触摸都使进度增加5%

package swu.twj.a13_drawview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.MotionEvent;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

MeterView mv = findViewById(R.id.meter);

if (event.getAction() == MotionEvent.ACTION_DOWN){

//更改进度值:在原有的进度上+0.05

mv.setProgress((float)(mv.getProgress() + 0.05));

}

return true;

}

}

xml文件配置:

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=".MainActivity">

android:id="@+id/meter"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值