XMl實現補間動畫
阿新 • • 發佈:2018-11-24
1.定義圖片和按鈕
<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" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:id="@+id/linear" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/translat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="位移" android:onClick="translat" /> <Button android:id="@+id/scale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="縮放" android:onClick="scale" /> <Button android:id="@+id/rotate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋轉" android:onClick="rotate" /> <Button android:id="@+id/alphat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="透明" android:onClick="alphat" /> <Button android:id="@+id/set" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="組合動畫" android:onClick="set" /> </LinearLayout> <ImageView android:id="@+id/image" android:layout_width="80dp" android:layout_height="80dp" android:background="@drawable/n" android:layout_below="@id/linear" /> </RelativeLayout>
2.在res資料夾裡建立anim資料夾並在裡面分別建立xml檔案
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1.0"
android:duration="3000"
>
</alpha>
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="720" android:pivotX="50%" android:pivotY="50%" android:duration="3000" > </rotate>
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0" android:toXScale="1.0" android:fromYScale="0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="3000" > </scale>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
>
<alpha
android:fromAlpha="0"
android:toAlpha="1.0"
/>
<scale
android:fromXScale="0"
android:toXScale="1.0"
android:fromYScale="0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
/>
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
/>
<translate
android:fromXDelta="0%p"
android:toXDelta="100%p"
android:fromYDelta="0%p"
android:toYDelta="100%p"
/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0%p"
android:toXDelta="100%p"
android:fromYDelta="0%p"
android:toYDelta="100%p"
android:duration="3000"
>
<!-- %p 圍繞父窗體 %圍繞自身 -->
</translate>
3.Java程式碼
載入檔案
package com.example.day16_animaction_java_demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取資源ID
imageView = (ImageView) findViewById(R.id.image);
}
//位移
public void translat(View v){
//載入XML檔案
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
imageView.startAnimation(animation);
}
//縮放
public void scale(View v){
//載入XML檔案
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
imageView.startAnimation(animation);
}
//旋轉
public void rotate(View v){
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
imageView.startAnimation(animation);
}
//透明
public void alphat(View v){
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
imageView.startAnimation(animation);
}
//組合
public void set(View v){
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.set);
imageView.startAnimation(animation);
}
}