Android自定义View绘制SeekBar的背景线和刻度

在这里插入图片描述

        <MySeekBar
            android:id="@+id/seekBar"
            android:progress="50"
            android:progressTint="@color/transparent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp" />

public class MySeekBar extends android.support.v7.widget.AppCompatSeekBar {

    public MySeekBar(Context context) {
        super(context);
    }

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

    public MySeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(getResources().getColor(R.color.white));
        paint.setStrokeWidth(4);

        //绘制中间的线条
        float centerY  = getHeight()/2;
        canvas.drawLine(0,centerY,getWidth(),centerY,paint);

        //绘制刻度
        float startX = 0;
        for (int i = 0; i < 5; i++) {
            startX = startX + i*getWidth()/4;
            canvas.drawLine(startX,centerY-10,startX,centerY,paint);
        }

        super.onDraw(canvas);
    }
}

猜你喜欢

转载自blog.csdn.net/yu540135101/article/details/84962495