仿QQ设置字体大小自定义SeekBar

1、先上截图
仿QQ设置字体大小自定义SeekBar_第1张图片

2、思路:自定义view其实就像一个画画过程,步骤如下:
a:首先画刻度条;
b:刻度描述;
c:然后画拖动块;
d:响应手势。

代码1:重写onDraw方法
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

    /**
     *  canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint);
     *  Rect src: 是对图片进行裁截,若是空null则显示整个图片
     *  RectF dst:是图片在Canvas画布中显示的区域,
     *  大于src则把src的裁截区放大,
     *  小于src则把src的裁截区缩小。
     */
    canvas.drawBitmap(bgBitmap, null, rectF, bgPaint);

    for (int i = 0; i < titles.length; i++) {
        float textWidth = textPint.measureText(titles[i]);
        canvas.drawText(titles[i], (i * perWidth + 20) - textWidth / 2, 40, textPint);
    }

    canvas.drawCircle(perWidth * cur_sections + marginKeduLeft, heightSize - marginKeduBottom - bgHeight / 2, 20, thumbPaint);//圆心落在刻度条的刻度中心

// canvas.drawBitmap(thumbBitmap, 20 - thubmWidth/2, 100 - thumbHeight/2, thumbPaint);
}

代码2:重写onTouchEvent方法
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = (int) event.getX();
downY = (int) event.getY();
responseTouch(downX, downY);
break;
case MotionEvent.ACTION_MOVE:
moveX = (int) event.getX();
moveY = (int) event.getY();
responseTouch(moveX, moveY);
break;
case MotionEvent.ACTION_UP:
upX = (int) event.getX();
upY = (int) event.getY();
responseTouch(upX, upY);
responseOnTouch.onTouchResponse(cur_sections);
break;
}
return true;
}

3、完整代码:https://github.com/QinFenFen/MySeekBar

你可能感兴趣的:(Android,Android,SeekBar,自定义View)