关于Android中的几种Animation

 

Activity:

package cn.howay;

import android.R.anim;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class AnimationTest extends Activity{

 private ImageView imageView = null;
 private Button alaphaButton = null;
 private Button rotateButton = null;
 private Button translateButton = null;
 private Button scaleButton = null;
 
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.animationtest);
  
  imageView = (ImageView) findViewById(R.id.image);
  alaphaButton = (Button) findViewById(R.id.alpha);
  rotateButton = (Button) findViewById(R.id.rotate);
  translateButton = (Button) findViewById(R.id.translate);
  scaleButton = (Button) findViewById(R.id.scale);
  
  alaphaButton.setOnClickListener(new OnClickListener() {
   
   public void onClick(View v) {
    //创建一个AnimationSet对象
    AnimationSet animationSet = new AnimationSet(true);
    //创建一个AlaphaAnimation对象
    AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
    //设置动画执行时间
    alphaAnimation.setDuration(5000);
    //将AlphaAnimation添加到AnimationSet中
    animationSet.addAnimation(alphaAnimation);
    //使用ImageView中的startAnimation()方法开始执行动画
    imageView.startAnimation(animationSet);
   }
  });
  
  rotateButton.setOnClickListener(new OnClickListener() {
   
   public void onClick(View v) {
    AnimationSet animationSet = new AnimationSet(true);
    RotateAnimation rotateAnimation = new RotateAnimation(
      0, 360,
      Animation.RELATIVE_TO_PARENT, 1f,
      Animation.RELATIVE_TO_PARENT, 0f);
    rotateAnimation.setDuration(5000);
    animationSet.addAnimation(rotateAnimation);
    imageView.startAnimation(animationSet);
   }
  });
  
  translateButton.setOnClickListener(new OnClickListener() {
   
   public void onClick(View v) {
    AnimationSet animationSet = new AnimationSet(true);
    TranslateAnimation translateAnimation = new TranslateAnimation(
      Animation.RELATIVE_TO_SELF, 0f,
      Animation.RELATIVE_TO_SELF, 0.5f,
      Animation.RELATIVE_TO_SELF, 0f,
      Animation.RELATIVE_TO_SELF, 1.0f);
    translateAnimation.setDuration(5000);
    animationSet.addAnimation(translateAnimation);
    imageView.startAnimation(translateAnimation);
   }
  });
  
  scaleButton.setOnClickListener(new OnClickListener() {
   
   public void onClick(View v) {
    AnimationSet animationSet = new AnimationSet(true);
    ScaleAnimation scaleAnimation = new ScaleAnimation(
      1, 0.1f, 1, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    animationSet.addAnimation(scaleAnimation);
    animationSet.setStartOffset(1000);
    animationSet.setFillAfter(true);
    animationSet.setFillAfter(false);
    animationSet.setDuration(5000);
    imageView.startAnimation(scaleAnimation);
   }
  });
 }
}

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="200px"
  >
  <ImageView
  android:id="@+id/image"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/icon"
  />
 
  </LinearLayout>
 
  <Button
  android:id="@+id/alpha"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="测试Alpha"
  />
  <Button
  android:id="@+id/scale"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="测试Scale"
  />
  <Button
  android:id="@+id/translate"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="测试Translate"
  />
  <Button
  android:id="@+id/rotate"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="测试rotate"
  />
</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值