Android自定义仪表盘

本文介绍了在Android项目中创建一个仪表盘效果的实践过程。最初尝试使用自定义View,但遇到指针圆心控制困难导致精度问题。为解决这个问题,改用固定指针并添加旋转动画的方法,通过在RelativeLayout中放置ImageView和TextView,实现了动态旋转的效果,完美解决了指针定位问题。提供了项目源码链接。

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

前几天项目中用到一个上阵指数大盘指数仪表图效果如下:这里写图片描述
一开始觉得用自定义View来写这个界面,而且仪表盘的图片也给好了,本来想这会非常简单,结果因为指针也是一张图片,指针的圆心不好控制,在给数值的时候总是会有略微的偏差看,尤其是在转着转着的时候就看出来,这是因为指针本身有宽度的问题,代码如下:
public class DashboardCustom extends View {

private Paint paint;
private Paint textPaint;

int speed = 0;
float radius;
private Bitmap back;
private Bitmap center;
private Bitmap arrow;

public DashboardCustom(Context context) {
    this(context, null, 0);
}

public DashboardCustom(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public DashboardCustom(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    /**
     * 获得我们所定义的自定义样式属性
     */
    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DashboardCustom, defStyleAttr, 0);

    speed = a.getInt(R.styleable.DashboardCustom_speed, 0);

    paint = new Paint();
    textPaint = new Paint();

    radius = a.getDimension(R.styleable.DashboardCustom_radius, 200);
    back = BitmapFactory.decodeResource(getResources(), R.drawable.dash);
    arrow = BitmapFactory.decodeResourc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值