android自定义View之尺子

最近在做一个时间设置功能,之前都是简单的用一个EditText来实现,并设置输入范围或者输入错误的警告信息,这样的方法虽然简单,但用户使用起来,显得繁琐,而且还动不动的蹦出来俩提示,一点都不友好。
因此这次换个新的设计吧——卡尺选择。

主要介绍一下这个View的主要几个绘制点:
1. 坐标轴:需要绘制X轴(横向卡尺)或Y轴(纵向卡尺)drawLine(Canvas canvas, Paint paint);
2. 刻度:需要绘制坐标轴上的刻度。drawScale(Canvas canvas, Paint paint);绘制刻度时,需要的参数有坐标轴宽度以及刻度之间的间隔;
3. 当前刻度指针:drawScalePointer(Canvas canvas, Paint paint); 需要绘制在当前卡尺显示区域的中点。由于卡尺的这个指针每次卡尺滚动都需要重新绘制,所以每次都需要计算当前显示区域的中点位置,而这个中间位置是相对于卡尺起点位置的坐标;
4. 滑动卡尺支持scroll以及fling方式,且注意滑动的边界处理。

其他的就不多说了,直接上代码吧,代码中都有注释:
首先是卡尺绘制的基类,

package rkhy.com.ecg.view.scale;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.VelocityTracker;
import android.view.View;
import android.widget.Scroller;

import rkhy.com.ecg.R;

/**
 * **************************************************
 *
 * @ 日        期:2018/1/5 15:33
 * @ 作        者:shangming
 * 卡尺基类
 * **************************************************
 */
public abstract class BaseScaleView extends View {
   
    protected int mScaleMin; // 刻度最小值
    protected int mScaleMax; // 刻度最大值
    protected int mScaleHeight; // 刻度高度
    protected int mScaleNumHeight; // 整数刻度高度
    protected int mScaleSpace; // 刻度间隔

    protected int mScaleCount; //相对刻度起点滑动的刻度
    protected int mViewWidth; //宽度
    protected int mViewHeight; //高度
    protected int mScrollPreX;
    protected int mInitMiddleScalePointer; // 初始中间刻度指针
    protected int mScrollViewWidth; // 滚动的View宽度
    protected int mScaleMiddle; // 屏幕中间的刻度偏移量

    protected Scroller mScroller;
    protected OnScaleScrollListener mOnScaleScrollListener;
    protected VelocityTracker mVelocityTracker;

    public BaseScaleView(Context context) {
        super(context);
        init(context, null);
    }

    public BaseScaleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public BaseScaleView(Context context, @Nullable Attri
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TypeArray 是 Android 中的一个特殊的资源类型,用于在 XML 中声明自定义 View 的属性。使用 TypeArray 可以方便地在 XML 布局中指定 View 的属性,而不需要在 Java 代码中进行硬编码。 使用 TypeArray 的步骤如下: 1. 在 res/values/attrs.xml 文件中定义定义 View 的属性。 ```xml <resources> <declare-styleable name="MyCustomView"> <attr name="customAttr1" format="integer" /> <attr name="customAttr2" format="string" /> <attr name="customAttr3" format="boolean" /> </declare-styleable> </resources> ``` 2. 在自定义 View 的构造函数中获取 TypedArray 对象,并从中获取属性值。 ```java public class MyCustomView extends View { private int customAttr1; private String customAttr2; private boolean customAttr3; public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView); customAttr1 = a.getInt(R.styleable.MyCustomView_customAttr1, 0); customAttr2 = a.getString(R.styleable.MyCustomView_customAttr2); customAttr3 = a.getBoolean(R.styleable.MyCustomView_customAttr3, false); a.recycle(); } } ``` 在上面的代码中,`context.obtainStyledAttributes(attrs, R.styleable.MyCustomView)` 用于获取 TypedArray 对象,`R.styleable.MyCustomView` 是在 attrs.xml 文件中定义的自定义属性集合,`a.getInt()`、`a.getString()`、`a.getBoolean()` 用于从 TypedArray 对象中获取属性值,最后需要调用 `a.recycle()` 来回收 TypedArray 对象。 3. 在 XML 布局中使用自定义 View,并设置属性值。 ```xml <com.example.MyCustomView android:layout_width="match_parent" android:layout_height="wrap_content" app:customAttr1="123" app:customAttr2="hello" app:customAttr3="true" /> ``` 在上面的代码中,`app:customAttr1`、`app:customAttr2`、`app:customAttr3` 是在 attrs.xml 文件中定义的自定义属性名,可以在 XML 布局中使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值