[英]How to set animation when I click on button at that time move image from left to right and right to left in android?
[英]Move image from left to right then rotate animation
我正在舉一個例子,即從左向右移動圖像然后自行旋轉。 我嘗試使用AnimationSet,但是圖像旋轉不正確。 它以一個周期而不是自身的周期運動。 如何解決這個問題?
我想要的是 主要活動
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AnimationSet set = new AnimationSet(true);
set.setFillAfter(true);
img_animation = (ImageView) findViewById(R.id.imgBanner);
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
rotation.setStartOffset(2000);
rotation.setDuration(2000);
TranslateAnimation moveLefttoRight = new TranslateAnimation(0, 200, 0, 0);
moveLefttoRight.setStartOffset(1000);
moveLefttoRight.setDuration(1000);
set.addAnimation(moveLefttoRight);
set.addAnimation(rotation);
img_animation.startAnimation(set);
}
Rotation.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="90" />
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imgBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/banner" />
</RelativeLayout>
任何建議對我都有幫助
嘗試使用此:
RotateAnimation anim = new RotateAnimation(0f, 360f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
因此它將相對於自身旋轉。 希望這可以幫助。
編輯:
image.clearAnimation();
RotateAnimation anim = new RotateAnimation(30, 360, image.getWidth()/2, image.getHeight()/2);
anim.setFillAfter(true);
anim.setRepeatCount(0);
anim.setDuration(10000);
image.startAnimation(anim);
我在演示應用程序中對此進行了測試。 它的工作很棒。 希望這對您有所幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.