拖动时Seekbar的拇指动画

Seekbar's thumb animation while dragging

我正在创建一个带有自定义缩略图的搜索栏。所以我的拇指是 layeredDrawable。所以我想在拖动拇指的同时调整我的拇指图像的大小。

我的意思是什么时候我会拖动我的拇指...拇指大小会增加并且搜索栏位置不应该改变。

请帮助我并提前致谢。

像这样使用搜索栏缩略图选择器

thumb.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/thumb_small" android:state_enabled="false"/>
    <item android:drawable="@drawable/thumb_big" android:state_pressed="true"/>
    <item android:drawable="@drawable/thumb_small" android:state_selected="true"/>
    <item android:drawable="@drawable/thumb_small"/>
</selector>

并像这样在搜索栏中引用它 你的布局 file.xml

        <SeekBar
            android:id="@+id/seekBarRadius"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:indeterminate="false"
            android:max="100"
            android:maxHeight="10dp"
            android:minHeight="10dp"
            android:progress="100"
            android:thumb="@drawable/thumb" />

thumb_big.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <corners android:radius="20dp" />
        <solid android:color="#FF049BFF" />
        <size
            android:width="40dp"
            android:height="40dp" />
    </shape>
</item>

thumb_small.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <corners android:radius="15dp" />
        <solid android:color="#FF049BFF" />
        <size
            android:width="30dp"
            android:height="30dp" />
    </shape>
</item>

滑动时更改搜索栏缩略图大小需要一些代码

android:thumb="@drawable/round"

代码

private SeekBar seekBar;

OnCreate中添加

seekBar = (SeekBar) findViewById(R.id.mySeekBarID);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            int progress = 0;

            @Override

            public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {

                progress = progresValue;

                Resources res = getResources();
                Drawable thumb = res.getDrawable(R.drawable.round);
                int h = progress / 10;
                int w = h;
                Bitmap bmpOrg = ((BitmapDrawable) thumb).getBitmap();
                Bitmap bmpScaled = Bitmap.createScaledBitmap(bmpOrg, w, h, true);
                Drawable newThumb = new BitmapDrawable(res, bmpScaled);
                newThumb.setBounds(0, 0, newThumb.getIntrinsicWidth(), newThumb.getIntrinsicHeight());
                seekBar.setThumb(newThumb);

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

已测试 并且按预期工作。即当你移动拇指时,它向左变小,向右变大

此处的这一行 int h = progress / 10;` 用于在您滑动时计算新的拇指大小,因此您需要进行一些数学运算才能获得所需的确切结果