【问题标题】:Android. ImageButton. How to rotate an image on a button. Not whole button安卓。图像按钮。如何旋转按钮上的图像。不是整个按钮
【发布时间】:2017-09-21 17:49:21
【问题描述】:

我有一个带有图像的 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>

爪哇代码:
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 或任何其他方法?

【问题讨论】:

标签: android image button rotation imagebutton


【解决方案1】:

你有两个选择:

  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。您可以在动画期间提供可绘制对象列表。我强烈推荐这个。

【讨论】:

  • 我尝试使用 Animationdrawable,但它改变了背景和按钮变成了图像。结果如下:imgur.com/a/Y7Xv8 如您所见,没有灰色按钮背景。所以,我要试试你的第一种方法。
【解决方案2】:

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

1. res/amin/ic_play_sound_animation.xml 中的 Amination 文件:

<?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. Activity 中的 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

【讨论】:

    猜你喜欢
    • 2011-01-18
    • 2014-02-02
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    相关资源
    最近更新 更多