学徒浅析Android开发:第十三讲——Tween动画

       今天小编给大家介绍一下Android中动画的制作,动画就是讲静态的图片按照一定的次序,在短时间中展示,所以现在主流动画的帧数为24帧和30帧(即一秒播放24张静态图或30张静态图)。Android动画的制作分成两大类,一类是Tween动画,一类是Frame动画。Frame动画就是按照帧数播放静态图片。今天小编给大家介绍的是Tween动画,用Android提供的Amination类来对某些图片实现一定的动画效果。

    Amination一共有四个基本效果类:AlphaAnimation(渐变透明度),ScaleAnimation(渐变尺寸收缩),TranslateAnimation(页面位移),RotateAnimation(页面旋转),同时还提供了AnimationSet类,该类可封装多种基本效果,实现更复杂的效果。下面大家就看着代码领悟一下吧,本次就不传效果图了,动画的没法提现,大家运行一下就知道了。

      

package com.example.demo_animation;
/**
 * @author Arthur Lee
 * @time 08/16/2014
 * */
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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 MainActivity extends Activity {

	Button bt_alpha, bt_tranlate, bt_rotate, bt_scale, bt_tween;
	View view;
	ImageView image;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		view = findViewById(R.id.animation);
		image = (ImageView) findViewById(R.id.image);
		bt_alpha = (Button) findViewById(R.id.bt_alpha);
		bt_tranlate = (Button) findViewById(R.id.bt_tranlate);
		bt_rotate = (Button) findViewById(R.id.bt_rotate);
		bt_scale = (Button) findViewById(R.id.bt_scale);
		bt_tween = (Button) findViewById(R.id.bt_tween);
		
		bt_alpha.setOnClickListener(new MyListener());
		bt_tranlate.setOnClickListener(new MyListener());
		bt_rotate.setOnClickListener(new MyListener());
		bt_scale.setOnClickListener(new MyListener());
		bt_tween.setOnClickListener(new MyListener());
	}
	/**
	 * 渐变透明度,有两个参数
	 * @param float fromAlpha 起始透明度
	 * @param float toAlpha 终止透明度
	 * 效果:实现图片的逐渐消失或逐渐显示效果,模糊效果*/
	public void setAlpha() {
		Animation alpha = new AlphaAnimation(1.0f, 0.1f);
		alpha.setDuration(50000);//设置效果持续时间
		view.setAnimation(alpha);//添加动画
		view.startAnimation(alpha);//启动动画
	}
	/**
	 * 渐变尺寸(即改变图片大小),有八个参数
	 * @param float fromX 起始X坐标
	 * @param float toX  起始X坐标
	 * @param float fromY 起始Y坐标
	 * @param float toY 起始Y坐标
	 * @param int pivotXType X轴样式
	 * @param float pivotXValue 样式值
	 * @param int pivotYType Y轴样式
	 * @param float pivotYValue 样式值
	 * 效果:实现图片的缩放*/
	public void setScale() {
		Animation scale = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		scale.setDuration(50000);
		view.setAnimation(scale);
		view.startAnimation(scale);
	}
	/**
	 *旋转,有六个参数
	 *@param float fromDegrees 
      @param float toDegrees 
      @param int pivotXType 
      @param float pivotXValue 
      @param int pivotYType 
      @param float pivotYValue  
      */
	public void setRotate() {
		Animation rotate = new RotateAnimation(0.0f, +360.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotate.setDuration(50000);
		view.setAnimation(rotate);
		view.startAnimation(rotate);
	}
	/**
	 *位移,有四个参数 
	 *@param float fromXDelta 起始X坐标
	 *@param float toXDelta 终止X坐标
	 *@param float fromYDelta 起始Y坐标
	 *@param float toYDelta 终止Y坐标*/
	public void setTranslate() {
		Animation translate = new TranslateAnimation(10, 100, 10, 100);
		translate.setDuration(50000);
		view.setAnimation(translate);
		view.startAnimation(translate);
	}

	/**
	 *组合显示
	 *AnimationSet可以讲多个效果一起显示,设计出不同的效果来 */
	public void setTween() {
		AnimationSet set = new AnimationSet(true);
		Animation alpha = new AlphaAnimation(0.1f, 1.0f);
		Animation scale = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		Animation translate = new TranslateAnimation(10, 100, 10, 100);
		Animation rotate = new RotateAnimation(0.0f, +360.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		set.addAnimation(rotate);
		set.addAnimation(translate);
		set.addAnimation(alpha);
		set.addAnimation(scale);
		set.setDuration(5000);
		view.setAnimation(set);
		view.startAnimation(set);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	class MyListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.bt_alpha:
				setAlpha();
				break;
			case R.id.bt_tranlate:
				setTranslate();
				break;
			case R.id.bt_rotate:
				setRotate();
				break;
			case R.id.bt_scale:
				setScale();
				break;
			case R.id.bt_tween:
				setTween();
				break;
			default:
				break;
			}
		}

	}
}

<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" >

  
    <LinearLayout
        android:id="@+id/buttonGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">
        <Button 
            android:id="@+id/bt_alpha"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="渐变透明度"/>
        <Button 
            android:id="@+id/bt_scale"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="渐变尺寸"/>
        <Button 
            android:id="@+id/bt_rotate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="旋转"/>
        <Button 
            android:id="@+id/bt_tranlate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="位移"/>
        <Button 
            android:id="@+id/bt_tween"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="组合效果"/>
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/animation"
        android:layout_below="@id/buttonGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView 
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:scaleType="centerInside"/>
    </LinearLayout>

</RelativeLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值