android - 如何使用动画旋转 ImageButton 源图像。

标签 android android-xml image-rotation

我需要旋转 ImageButton 的可绘制资源。我已成功将功能旋转到我的按钮,但旋转功能会影响整个按钮。我只想在 ImageButton 中旋转可绘制对象。

enter image description here

如何处理这种情况? PS:我在 ImageButton 中访问了 drawable 但我无法提供任何动画功能。

感谢帮助

这是我的 ImageButton xml;

 <ImageButton
   android:id="@+id/button"
   android:layout_width="50dp"
   android:layout_height="50dp"
   android:layout_gravity="center_vertical"
   android:layout_marginTop="-40dp"
   android:background="@color/titlebackground_color"
   android:src="@drawable/open" />

先旋转xml;

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <rotate
        android:fromDegrees="-180"
        android:toDegrees="-360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="500"
        android:startOffset="0"
        />
</set>

旋转第二个xml;

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <rotate
        android:fromDegrees="-180"
        android:toDegrees="-0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="500"
        android:startOffset="0"

        />
</set>

动画功能;

public class LayerInfoFragment extends Fragment {
 int count = 0;
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.layer_info_main, container, false);
        btnClose = (ImageButton) v.findViewById(R.id.button);
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation rotate = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_first);
                Animation rotatex = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_last);

                if (count % 2 == 0) {
                    v.setRotation(180);
                    v.setAnimation(rotate);


                } else {
                     v.setRotation(0);
                     v.setAnimation(rotatex);
                }
                count++;


            }
        });
    }
}

最佳答案

我认为有两件事可以尝试。

1) 在v.setAnimation()之后添加代码v.startAnimation(rotate);。我怀疑你的动画从未开始。我之前在想,设置中的 android:startOffset 会触发启动,但我不清楚。

2) 尝试 RotateAnimation,它是 Animation 的直接子类,而不是 Animation 对象。似乎许多人使用 RotateAnimation 比其他任何东西都多。除此之外,我怀疑的另一个问题是在布局文件中。 示例:

RotateAnimation rotate = (RotateAnimation) AnimationUtils.loadAnimation...

告诉我们发生了什么。

关于android - 如何使用动画旋转 ImageButton 源图像。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31241744/

相关文章:

android - 如何在ConstraintLayout 上设置三分之一的布局宽度百分比?

android - 当编辑单个框时,如何防止 EditText 列表编辑多个框?

android - 对话 Activity - 文本在 android 中被剪切

javascript - 如何使用自定义选项制作旋转图像

c# - 如何翻转/旋转 C#/Windows 窗体中的标签?

android - 如何使用 adjustResize 去除屏幕顶部的白条

javascript - 想在 native react 中自动滚动平面列表

android - 回收器适配器中的图像拼贴

android - 使用 MultiChoiceItems 的 AlertDialog ListView 中的额外空间

iphone - 将 UIImage 或 UIView 旋转到特定角度,而不是角度量