2016-01-19 62 views
1

我正在用自定義的拇指創建一個seekbar。所以我的拇指它layeredDrawable。所以我想在拖動拇指的同時調整我的拇指圖像。Seekbar的拇指動畫拖動

我的意思是,當我拖動我的拇指...拇指大小將增加,並且seekbar位置不應該改變。

請幫助我,並提前致謝。

+0

所以基本上你有一個圖像,你想在移動seekbar時重新調整它的大小? – Tasos

+0

是@Tasos我正在找:) :) – Newts

+0

@Newts是我的回答有幫助 –

回答

2

更改搜索欄拇指大小,你滑動需要一些代碼

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;``當你滑動時計算新的拇指大小,所以你需要玩一些數學來獲得你需要的確切結果

+0

我正在使用layerdrawable拇指圖像....所以不能夠位圖:( – Newts

+0

@Newts - 你可以將其轉換爲位圖 - http://stackoverflow.com/questions/15805060/how-to-convert-the-layerdrawable-to-drawable-in- android – Tasos

+0

我轉換了...但它變得非常小,有時候它也不正確,有時候看不見吧 – Newts

1

使用選擇的搜索條拇指像這樣的

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> 

+0

不,我已經嘗試過這個......但我希望拖動它。 – Newts

+0

@Newts(拖動狀態=當你的拇指狀態被按下時)我已經包含在選擇器中了我想你應該給它嘗試 –

+0

其增加seekbar的大小,而拖動:( – Newts