android自定义view——温度计

本文详细讲解如何在Android中创建一个自定义View,实现一个功能丰富的温度计组件。从绘制基础到交互逻辑,带你一步步实现个性化温度显示,适配不同设备屏幕,并探讨性能优化技巧。
摘要由CSDN通过智能技术生成

</pre><pre name="code" class="java">public class Themometer extends ImageView {

	private Paint mPaint;
	private int height, width, circle_radius;
	private int COLOR_BROWN = Color.parseColor("#A5937B");
	private int COLOR_YELLOW = Color.parseColor("#F7AF1F");
	private int COLOR_GRAY = Color.parseColor("#C1CDCD");
	private int COLOR_TEXT = Color.parseColor("#49BDCC");
	private float temperature = 0f;
	private float temp_temperature = 0f;

	public void setTemperature(float value) {

		Log.e(this.getClass().getSimpleName(), "setTemperature " + value);

		if (value < 0f) {
			value = 0;
		} else if (value > 41.5f) {
			value = 41.5f;
		}

		if (value <= 34f) {
			value = new BigDecimal(value).setScale(0, BigDecimal.ROUND_HALF_UP).floatValue();
		} else {
			value = new BigDecimal(value).setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
		}
		if (temp_temperature > value) {
			temp_temperature = 0f;
		}
		temperature = value;
		invalidate();
	}

	public Themometer(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public Themometer(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		mPaint = new Paint();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		this.height = getMeasuredHeight();
		this.width = getMeasuredWidth();
		circle_radius = width / 4;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawBG(canvas);
		drawScale(canvas);
		drawFG(canvas);

		if (temp_temperature < temperature && temp_temperature < 41.5) {
			if (temp_temperature < 34) {
				BigDecimal b1 = new BigDecimal(Float.toString(temp_temperature));
				BigDecimal b2 = new BigDecimal(Float.toString(1f));
				temp_temperature = b1.add(b2).floatValue();
			} else {
				BigDecimal b1 = new BigDecimal(Float.toString(temp_temperature));
				BigDecimal b2 = new BigDecimal(Float.toString(0.1f));
				temp_temperature = b1.add(b2).floatValue();
			}
			invalidate();
		}
		// else if(temp_temperature>temperature && temp_temperature>0){
		// if (temp_temperature < 34) {
		// BigDecimal b1 = new BigDecimal(Float.toString(temp_temperature));
		// BigDecimal b2 = new BigDecimal(Float.toString(1f));
		// temp_tem
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值