package com.example.android_shape; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; public class AnimActivity extends Activity { ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_anim); imageView = (ImageView) findViewById(R.id.imageView1); } public void btnclick(View view) { Animation animation; switch (view.getId()) { case R.id.button1: // 加载动画 animation = AnimationUtils.loadAnimation(AnimActivity.this, R.anim.alpha); imageView.setAnimation(animation); break; case R.id.button2: animation = AnimationUtils.loadAnimation(AnimActivity.this, R.anim.scale); imageView.setAnimation(animation); break; case R.id.button3: animation = AnimationUtils.loadAnimation(AnimActivity.this, R.anim.translate); imageView.setAnimation(animation); break; case R.id.button4: animation = AnimationUtils.loadAnimation(AnimActivity.this, R.anim.roate); imageView.setAnimation(animation); break; case R.id.button5: animation = AnimationUtils.loadAnimation(AnimActivity.this, R.anim.all); imageView.setAnimation(animation); break; } } }
activity_anim.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" > <ImageView android:id="@+id/imageView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="btnclick" android:text="透明度" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="btnclick" android:text="缩放" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="btnclick" android:text="位移" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="btnclick" android:text="旋转" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="btnclick" android:text="全部" /> </LinearLayout> </LinearLayout>
/res/anim/alpha.xml(透明度)
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromAlpha="1" android:toAlpha="0.5" /> <!-- 透明度 -->
/res/anim/rotate.xml(旋转)
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:duration="2000" android:toDegrees="180" /> <!-- fromDegrees 原始角度 --> <!-- toDegrees 目标角度 --> <!-- pivotX,pivotY 中心点 -->
/res/anim/scale.xml(缩放)
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:toXScale="2.0" android:toYScale="0.5" /> <!-- 缩放 --> <!-- android:fillAfter="true" 是否要保持动画后的状态 -->
/res/anim/translate.xml(位移)
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="200" android:toYDelta="200" /> <!-- 位移 --> <!-- 都是以中心点计算 -->
/res/anim/all.xml(全部动画集合)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="2000" android:fillAfter="true" android:fromAlpha="1" android:toAlpha="0.5" /> <rotate android:duration="2000" android:fillAfter="true" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="180" /> <scale android:duration="2000" android:fillAfter="true" android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:toXScale="2.0" android:toYScale="0.5" /> <translate android:duration="2000" android:fillAfter="false" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="200" android:toYDelta="200" /> <!-- <translate android:duration="2000" android:fillAfter="false" android:fromXDelta="0" android:fromYDelta="0" android:startOffset="2000" android:toXDelta="0" android:toYDelta="200" /> --> </set> <!-- 动画集合 -->
android:startOffset="2000" 动画延迟