2017-04-24 151 views
2

我有ImageButton與圖像。我想旋轉的唯一圖像。
不喜歡這樣的: rotating buttonAndroid。的ImageButton。如何旋轉按鈕上的圖像。不是整個按鈕

這就是我:

<ImageButton 
 
    android:id="@+id/imageButton" 
 
    android:layout_width="100dp" 
 
    android:layout_height="100dp" 
 
    android:layout_alignBottom="@+id/button" 
 
    android:src="@drawable/up" 
 
    />

動畫:

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
 
    <rotate 
 
    android:fromDegrees="0" 
 
    android:toDegrees="360" 
 
    android:pivotX="50%" 
 
    android:pivotY="50%" 
 
    android:repeatCount="0" 
 
    android:duration="1000" /> 
 
</set>

java代碼:

public void clockwise(View view){ 
    ImageButton image = (ImageButton)findViewById(R.id.imageButton); 
    Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),   R.anim.myanimation);<p> 
    image.startAnimation(animation); 
} 

我應該使用AnimationDrawable或矩陣或任何其他辦法?

+0

是你的代碼旋轉整個按鈕? –

+0

是的,這是它現在的樣子:https://i.stack.imgur.com/NcVwv.gif –

回答

0

你有兩個選擇:

  1. 獨立的Button從使用FrameLayout你的形象。在這種情況下,您可以在Button上設置點擊偵聽器,並將旋轉僅應用於ImageView。這是更容易和更快完成,但它是一個解決方法和性能都與第二選擇更好,因爲你沒有嵌套佈局:

    <FrameLayout 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:layout_alignBottom="@+id/button"> 
    
        <Button 
         android:id="@+id/imageButton" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" /> 
    
        <ImageView 
         android:id="@+id/imageButtonContent" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" /> 
    </FrameLayout> 
    
  2. 使用AnimationDrawable。您可以在動畫過程中提供可繪製的列表。我認真推薦這一款。

+0

我嘗試使用Animationdrawable,但它改變了背景和按鈕成爲圖像。這裏是一個結果: http://imgur.com/a/Y7Xv8你可以看到,沒有灰色的按鈕背景。 所以,我會嘗試你的第一個方法。 –

0

這是一個答案,如何旋轉唯一的圖像。 謝謝@Fondesa,我採納了你的建議。在RES /阿明/ ic_play_sound_animation.xml

1.胺化的文件:

<?xml version="1.0" encoding="utf-8"?> 
 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:shareInterpolator="false"> 
 
    <rotate 
 
    android:duration="1000" 
 
    android:fromDegrees="0" 
 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
 
    android:pivotX="50%" 
 
    android:pivotY="50%" 
 
    android:repeatCount="infinite" 
 
    android:repeatMode="restart" 
 
    android:toDegrees="359"/> 
 
</set>

2.一塊佈局頁面與兩個視圖(一按鈕和圖像上方):

<FrameLayout 
 
     android:id="@+id/frame" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_column="4" 
 
     android:layout_row="3"> 
 
     <Button 
 
     android:id="@+id/id_btn_play_sound" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content"/> 
 
     <ImageView 
 
     android:id="@+id/id_iv_play_sound_icon" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="match_parent" 
 
     android:layout_gravity="center" 
 
     android:scaleType="fitCenter" 
 
     android:src="@drawable/ic_play_sound_1"/> 
 
    </FrameLayout>

3. Java代碼的活動:

private ImageView playSoundIcon = (ImageView)view. findViewById (R.id.id_iv_play_sound_icon); 

    private void runBtnAnimation() { 
     playSoundIcon.setImageResource(R.drawable.ic_playing_sound_2); 
     Animation rotation = AnimationUtils 
       .loadAnimation(getActivity().getApplicationContext(), 
         R.anim.ic_play_sound_animation); 
     playSoundIcon.startAnimation(rotation); 
    } 

    private void stopBtnAnimation() { 
     playSoundIcon.clearAnimation(); 
     playSoundIcon.setImageResource(R.drawable.ic_play_sound_1); 
    } 

4。你可以看GIF動畫,它看起來像: final animation on the button