我需要旋转 ImageButton 的可绘制资源。我已成功将功能旋转到我的按钮,但旋转功能会影响整个按钮。我只想在 ImageButton 中旋转可绘制对象。
如何处理这种情况? 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/