单击按钮时我试图在圆形图像上显示动画。单击操作时,我需要将图像放大 360 度。
最初的图像如下
onClick 操作后我希望图像如下
,比实际尺寸大,直到再次单击按钮之前它必须是相同的
我正在使用以下代码:
TranslateAnimation moveAnim = new TranslateAnimation(-25, -25, -25, -25);
moveAnim.setInterpolator(new AccelerateInterpolator());
moveAnim.setDuration(600);
moveAnim.setFillEnabled(true);
moveAnim.setFillAfter(true);
moveAnim.setRepeatMode(AnimationSet.REVERSE);
moveAnim.setRepeatCount(AnimationSet.INFINITE);
startAnimation(moveAnim);
这样,图像就会稍微偏离其位置,并且不会扩展。
如何在onclick
中展开图像,然后再次单击将图像移动到其原始位置
最佳答案
也许这可以完成这项工作(/res/anim/myanim.xml):
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.1"
android:toXScale="1.0"
android:fromYScale="0.1"
android:toYScale="1.0"
android:duration="600"
android:pivotX="50%"
android:pivotY="50%" >
</scale>
</set>
在您的 fragment (或 Activity 或您正在使用的任何内容)中:
public class TestZoomFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_layout, container, false);
Button btnZoom = (Button) view.findViewById(R.id.test_btn_zoom);
final ImageView image = (ImageView) view.findViewById(R.id.test_img);
btnZoom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.zoom);
image.startAnimation(animation);
}
});
return view;
}
}
顺便说一句,如果您希望图像在单击按钮之前不可见,只需将默认可见性设置为“消失”,然后在 OnClick()
中将其设置回“可见”即可.
引用: Android Animations Tutorial
test_layout.xml 很简单。但万一有人需要它:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/test_btn_zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:text="zoom!" />
<ImageView
android:id="@+id/test_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/test_image" />
</LinearLayout>
关于android动画将圆形图像向各个方向扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21375758/