seekBar改变样式

2017-11-15  本文已影响0人  overhaha
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 背景效果 -->

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <!-- angle 是角度 -->
            <gradient
                android:angle="270"
                android:centerColor="#8e8989"
                android:centerY="0.75"
                android:endColor="#8e8989"
                android:startColor="#8e8989" />
        </shape>
    </item>

    <!-- 第一进度条 -->

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

                <gradient
                    android:angle="270"
                    android:centerColor="#60cae8"
                    android:centerY="0.75"
                    android:endColor="#60cae8"
                    android:startColor="#60cae8" />
            </shape>
        </clip>
    </item>

</layer-list>

在xml中

  <SeekBar
      android:id="@+id/telescope_seekBar"
      android:layout_width="250dp"
      android:layout_marginLeft="60dp"
      android:layout_marginTop="25dp"
      android:layout_height="wrap_content"
      android:thumb="@mipmap/btnzoom"//改变进度条上的按钮图标
      android:thumbOffset="0dip"//目的是图片和进度条对齐
      android:maxHeight="6dip"//设置 进度条的宽度
      android:minHeight="6dip"
      android:progressDrawable="@drawable/seekbar_style"/>

竖直的seekBar这样写


public class VerticalSeekBar extends SeekBar {
    public VerticalSeekBar(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldw, oldh);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.rotate(-90);
        canvas.translate(-getHeight(),0);
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()){
            return false;
        }
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                setProgress(getMax()
                        - (int) (getMax() * event.getY() / getHeight()));
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                break;

            case MotionEvent.ACTION_CANCEL:
                break;
        }

        return true;
    }
}
QQ截图20171115204324.png
上一篇下一篇

猜你喜欢

热点阅读