简体   繁体   English

Android:如何在移动seekbar时更改seekbar拇指的尺寸

[英]Android : How to change seekbar thumb's dimension as seekbar is moved

Seekbar thumb has to have two circles . Seekbar拇指必须有两个圆圈。 Outer circle's dimension has to be fixed while inner circle dimension will grow as thumb is moved along the seek bar. 外圆的尺寸必须固定,而内圆的尺寸将随着拇指沿搜索栏移动而增大。

Position 1: 位置1: 在此处输入图片说明

Position 2 : 位置2:

在此处输入图片说明

I have created a drawable to be used as thumb : 我创建了一个可绘制的图形用作拇指:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/gather_seek_bar_thumb_outer_ring"
        android:height="30dp"
          android:width="30dp"
          android:gravity="center">
        <shape
            android:shape="oval">
            <stroke
                android:width="1dp"/>
        </shape>
    </item>

    <item android:id="@+id/gather_seek_bar_thumb_inner_ring"
          android:width="10dp"
          android:height="10dp"
          android:gravity="center">
        <shape android:shape="oval">
            <solid
                android:color="#48b3ff"/>
        </shape>
    </item>
</layer-list>

Then on seekbar i am doing this 然后在seekbar上我正在这样做

mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            LayerDrawable drawable = (LayerDrawable) mSeekBar.getThumb();
            Drawable innerDrawable = drawable.findDrawableByLayerId(R.id.gather_seek_bar_thumb_inner_ring);
            Rect innerRect = innerDrawable.getBounds();
            Drawable outerDrawable = drawable.findDrawableByLayerId(R.id.gather_seek_bar_thumb_outer_ring);
            Rect outerRect = outerDrawable.getBounds();
            int left = innerRect.left > outerRect.left ? innerRect.left - 4 : outerRect.left;
            int right = innerRect.right < outerRect.right ? innerRect.right + 4 : outerRect.right;
            int top = innerRect.top > outerRect.top ? innerRect.top - 4 : outerRect.top;
            int bottom = innerRect.bottom < outerRect.bottom ? innerRect.bottom + 4 : outerRect.bottom;
            innerDrawable.setBounds(left, top, right, bottom);
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

But it doesn't change the size of inner circle. 但这不会改变内圆的大小。 Probably because inner-circle is already drawn . 可能是因为已经画了内圆。 Can someone suggest any solution ? 有人可以提出任何解决方案吗?

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/seek_bar_thumb_outer_ring"
        android:gravity="center">
        <shape
            android:shape="oval">
            <stroke
                android:width="1dp"/>
            <size android:height="30dp"
                  android:width="30dp"/>
        </shape>

    </item>
    <item android:id="@+id/seek_bar_thumb_inner_ring">
        <scale android:drawable="@drawable/seekbarinnercircle"
               android:scaleHeight="100%"
               android:scaleWidth="100%"
               android:scaleGravity="center_vertical|center_horizontal">
        </scale>
    </item>
</layer-list>

Solution to this problem is to use scale Drawable . 解决此问题的方法是使用Scale Drawable。

 LayerDrawable drawable = (LayerDrawable) mSeekBar.getThumb();
    ScaleDrawable innerDrawable = (ScaleDrawable) drawable.findDrawableByLayerId(R.id.seek_bar_thumb_inner_ring);
    innerDrawable.setLevel(5000);

On progress listener , 在进度监听器上,

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            LayerDrawable drawable = (LayerDrawable) mSeekBar.getThumb();
            ScaleDrawable innerDrawable = (ScaleDrawable)drawable.findDrawableByLayerId(R.id.seek_bar_thumb_inner_ring);
            innerDrawable.setLevel(progress * 100);

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM