Android 刻度尺

http://blog.csdn.net/windlake/article/details/7201234

http://www.apkbus.com/android-13035-1-1.html

http://blog.csdn.net/jerboy/article/details/8149453

http://hyongx.iteye.com/blog/1156542

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的Android刻度尺实现代码,可以根据需要进行自定义和修改: ``` public class RulerView extends View { private Paint mLinePaint = new Paint(); private Paint mTextPaint = new Paint(); private int mWidth, mHeight; private int mMaxValue, mMinValue, mDefaultValue; private int mLineSpacing, mSelectedLineSpacing; private int mTextSize; private int mLineColor, mTextColor; private int mSelectedLineColor, mSelectedTextColor; private int mCurrentValue; private OnValueChangeListener mListener; public RulerView(Context context) { this(context, null); } public RulerView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RulerView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RulerView); mMaxValue = a.getInt(R.styleable.RulerView_maxValue, 100); mMinValue = a.getInt(R.styleable.RulerView_minValue, 0); mDefaultValue = a.getInt(R.styleable.RulerView_defaultValue, 50); mLineSpacing = a.getDimensionPixelSize(R.styleable.RulerView_lineSpacing, 20); mSelectedLineSpacing = a.getDimensionPixelSize(R.styleable.RulerView_selectedLineSpacing, 30); mTextSize = a.getDimensionPixelSize(R.styleable.RulerView_textSize, 30); mLineColor = a.getColor(R.styleable.RulerView_lineColor, Color.BLACK); mTextColor = a.getColor(R.styleable.RulerView_textColor, Color.BLACK); mSelectedLineColor = a.getColor(R.styleable.RulerView_selectedLineColor, Color.RED); mSelectedTextColor = a.getColor(R.styleable.RulerView_selectedTextColor, Color.RED); a.recycle(); mLinePaint.setStrokeWidth(2); mLinePaint.setAntiAlias(true); mTextPaint.setTextSize(mTextSize); mTextPaint.setAntiAlias(true); mCurrentValue = mDefaultValue; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = getMeasuredWidth(); mHeight = getMeasuredHeight(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int start = mWidth / 2 - (mCurrentValue - mMinValue) * mLineSpacing; int end = mWidth / 2 + (mMaxValue - mCurrentValue) * mLineSpacing; for (int i = mMinValue; i <= mMaxValue; i++) { int x = mWidth / 2 + (i - mCurrentValue) * mLineSpacing; if (i == mCurrentValue) { mLinePaint.setColor(mSelectedLineColor); mTextPaint.setColor(mSelectedTextColor); canvas.drawLine(x, 0, x, mHeight, mLinePaint); canvas.drawText(String.valueOf(i), x, mHeight - mTextSize, mTextPaint); } else { mLinePaint.setColor(mLineColor); mTextPaint.setColor(mTextColor); if (x >= start && x <= end) { canvas.drawLine(x, 0, x, mSelectedLineSpacing, mLinePaint); canvas.drawText(String.valueOf(i), x, mSelectedLineSpacing + mTextSize, mTextPaint); } else { canvas.drawLine(x, 0, x, mLineSpacing, mLinePaint); canvas.drawText(String.valueOf(i), x, mLineSpacing + mTextSize, mTextPaint); } } } } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: int x = (int) event.getX(); mCurrentValue = mMinValue + (mWidth / 2 - x) / mLineSpacing; if (mCurrentValue < mMinValue) { mCurrentValue = mMinValue; } else if (mCurrentValue > mMaxValue) { mCurrentValue = mMaxValue; } invalidate(); if (mListener != null) { mListener.onValueChange(mCurrentValue); } return true; } return super.onTouchEvent(event); } public void setOnValueChangeListener(OnValueChangeListener listener) { mListener = listener; } public interface OnValueChangeListener { void onValueChange(int value); } } ``` 可以在XML布局中用以下代码引用刻度尺视图: ``` <com.example.rulerview.RulerView android:id="@+id/ruler_view" android:layout_width="match_parent" android:layout_height="wrap_content" app:maxValue="100" app:minValue="0" app:defaultValue="50" app:lineSpacing="20dp" app:selectedLineSpacing="30dp" app:textSize="30sp" app:lineColor="@color/black" app:textColor="@color/black" app:selectedLineColor="@color/red" app:selectedTextColor="@color/red" /> ``` 其中,maxValue、minValue和defaultValue分别表示刻度尺的最大值、最小值和默认值,lineSpacing和selectedLineSpacing分别表示刻度线的间距和选中刻度线的间距,textSize表示刻度尺刻度值的字体大小,lineColor和selectedLineColor分别表示刻度线的颜色和选中刻度线的颜色,textColor和selectedTextColor分别表示刻度尺刻度值的颜色和选中刻度尺刻度值的颜色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值