自定義view學(xué)習(xí)(第一章)
1诡必、自定義刻度尺控件
在我們想要獲取用戶的身高體重等信息時(shí),直接讓他們輸入顯然不夠友好偶然看到一款A(yù)pp用了類似刻度尺的界面讓用戶選擇搔扁,覺得很贊爸舒。所有決定實(shí)現(xiàn)下。
實(shí)現(xiàn)的最終效果如下圖所示:
7c0d4213-1242-496b-9898-0d04d14f3097.gif
2稿蹲、使用方式
2.1 在gradle添加依賴
compile 'com.zkk.view:ZkkRulerView:1.0.0'
2.2 在xml中設(shè)置
<com.zkk.view.rulerview.RulerView
android:id="@+id/ruler_height"
android:layout_width="fill_parent"
android:layout_height="58.0dip"
android:layout_marginTop="24.0dip"
app:alphaEnable="true"
app:lineColor="@color/gray"
app:lineMaxHeight="40dp"
app:lineMidHeight="30dp"
app:lineMinHeight="20dp"
app:lineSpaceWidth="10dp"
app:lineWidth="2dip"
app:textColor="@color/black"
app:minValue="80.0"
app:maxValue="250.0"
app:perValue="1"
app:selectorValue="165.0"
/>
2.3 在activity中只需調(diào)用一個(gè)方法和一個(gè)數(shù)值的回調(diào)
ruler_height=(RulerView)findViewById(R.id.ruler_height);
ruler_weight.setOnValueChangeListener(new RulerView.OnValueChangeListener() {
@Override
public void onValueChange(float value) {
tv_register_info_weight_value.setText(value+"");
}
});
/**
*
* @param selectorValue 未選擇時(shí) 默認(rèn)的值 滑動(dòng)后表示當(dāng)前中間指針正在指著的值
* @param minValue 最大數(shù)值
* @param maxValue 最小的數(shù)值
* @param per 最小單位 如 1:表示 每2條刻度差為1. 0.1:表示 每2條刻度差為0.1 在demo中 身高mPerValue為1 體重mPerValue 為0.1
*/
ruler_weight.setValue(165, 80, 250, 1);
2扭勉、實(shí)現(xiàn)講解
首先先無恥的將github地址拋出:
https://github.com/panacena/RuleView
<TextView
android:id="@+id/tv_register_info_height_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="11.0dip"
android:includeFontPadding="false"
android:maxHeight="24.0sp"
android:text="165"
android:textColor="#cc222222"
android:textSize="24.0sp"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.zkk.view.RulerView
android:id="@+id/ruler_height"
android:layout_width="fill_parent"
android:layout_height="58.0dip"
android:layout_marginTop="24.0dip"/>
<ImageView
android:layout_width="14.0dip"
android:layout_height="46.0dip"
android:layout_centerHorizontal="true"
android:layout_marginTop="11.0dip"
android:scaleType="fitXY"
android:src="@drawable/user_info_ruler_height"/>
</RelativeLayout>
自定義了一個(gè) RulerView。代碼就不一一寫出苛聘,在源碼中有詳細(xì)的解釋涂炎。
private int mMinVelocity;
private Scroller mScroller; //Scroller是一個(gè)專門用于處理滾動(dòng)效果的工具類 用mScroller記錄/計(jì)算View滾動(dòng)的位置,再重寫View的computeScroll()设哗,完成實(shí)際的滾動(dòng)
private VelocityTracker mVelocityTracker; //主要用跟蹤觸摸屏事件(flinging事件和其他gestures手勢事件)的速率唱捣。
private int mWidth;
private int mHeight;
private float mSelectorValue = 50.0f; // 未選擇時(shí) 默認(rèn)的值 滑動(dòng)后表示當(dāng)前中間指針正在指著 的值
private float mMaxValue = 200; // 最大數(shù)值
private float mMinValue = 100.0f; //最小的數(shù)值
private float mPerValue = 1; //最小單位 如 1:表示 每2條刻度差為1. 0.1:表示 每2條刻度差為0.1
// 在demo中 身高mPerValue為1 體重mPerValue 為0.1
private float mLineSpaceWidth = 5; // 尺子刻度2條線之間的距離
private float mLineWidth = 4; // 尺子刻度的寬度
private float mLineMaxHeight = 420; // 尺子刻度分為3中不同的高度。 mLineMaxHeight表示最長的那根(也就是 10的倍數(shù)時(shí)的高度)
private float mLineMidHeight = 30; // mLineMidHeight 表示中間的高度(也就是 5 15 25 等時(shí)的高度)
private float mLineMinHeight = 17; // mLineMinHeight 表示最短的那個(gè)高度(也就是 1 2 3 4 等時(shí)的高度)
private float mTextMarginTop = 10; //o
private float mTextSize =30; //尺子刻度下方數(shù)字 textsize
private boolean mAlphaEnable = false; // 尺子 最左邊 最后邊是否需要透明 (透明效果更好點(diǎn))
private float mTextHeight; //尺子刻度下方數(shù)字 的高度
private Paint mTextPaint; // 尺子刻度下方數(shù)字( 也就是每隔10個(gè)出現(xiàn)的數(shù)值) paint
private Paint mLinePaint; // 尺子刻度 paint
private int mTotalLine; //共有多少條 刻度
private int mMaxOffset; //所有刻度 共有多長
private float mOffset; // 默認(rèn)狀態(tài)下网梢,mSelectorValue所在的位置 位于尺子總刻度的位置
private int mLastX, mMove;
private OnValueChangeListener mListener; // 滑動(dòng)后數(shù)值回調(diào)
github地址:https://github.com/panacena/RuleView