Android。图片按钮。如何旋转按钮上的图像。不是整个按钮

Android. ImageButton. How to rotate an image on a button. Not whole button

我有 ImageButton 和一张图片。我想旋转 唯一的图像。
不是这样的: rotating button

这就是我所拥有的:

 <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 还是 Matrix 或任何其他方法?

你有两个选择:

  1. 使用 FrameLayout 将您的 Button 与图像分开。在这种情况下,您可以在 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。您可以在动画期间提供可绘制对象列表。真心推荐这个。

这是一个答案,如何旋转唯一的图像。 谢谢@Fondesa,我采纳了你的建议。

1. 文件在res/amin/ic_play_sound_animation.xml:

<?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 Activity 中的代码:

    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