Android笔记 动画之tween(补间)动画demo

本文介绍了一个使用Android平台实现补间动画的示例项目。通过不同的按钮触发透明度、缩放、旋转和平移等动画效果,并展示了如何组合这些基本动画创建更复杂的动画序列。

简介:补间动画:做flash动画时,在两个关键帧中间需要做“补间动画”,才能实现图画的运动;插入补间动画后两个关键帧之间的插补帧是由计算机自动运算而得到的。(来自百度百科)

demo

1布局

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click1"
            android:text="透明度" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click2"
            android:text="缩放" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click3"
            android:text="旋转" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click4"
            android:text="平移" />
        
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click5"
            android:text="组合" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <ImageView
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</LinearLayout>

2Mainactivity

package com.example.a112tweenanimation;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
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.ImageView;

public class MainActivity extends Activity {

	private ImageView iv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		iv = (ImageView) findViewById(R.id.iv);
	}

	/**
	 * 透明度变化
	 */
	public void click1(View view) {
		// para 开始透明度 结束透明度
		AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
		// 动画播放5秒
		animation.setDuration(5000);
		// 重复播放两次 2+1=3
		animation.setRepeatCount(2);
		// 反向变化
		animation.setRepeatMode(Animation.REVERSE);
		iv.startAnimation(animation);
	}

	/**
	 * 缩放
	 */
	public void click2(View view) {
		// paras 1开始水平缩放比例 2放大到几倍 3开始垂直缩放比例 4放大到几倍 5x坐标类型RELATIVE_TO_SELF相对于自身
		// 6缩放时以那个位置为中心缩放 0.5f控件中心 7,8与五六类似
		ScaleAnimation animation = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		// 动画播放5秒
		animation.setDuration(5000);
		// 重复播放两次 2+1=3
		animation.setRepeatCount(2);
		// 反向变化
		animation.setRepeatMode(Animation.REVERSE);
		iv.startAnimation(animation);
	}

	/**
	 * 旋转
	 */
	public void click3(View view) {
		// paras 1fromDegrees从什么角度旋转 2toDegrees旋转多少度 3pivotXType 4pivotXValue
		// 5pivotYType 6pivotYValue 3-6参数指定旋转中心
		RotateAnimation animation = new RotateAnimation(0, 360,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		// 动画播放5秒
		animation.setDuration(5000);
		// 重复播放两次 2+1=3
		animation.setRepeatCount(2);
		// 反向变化
		animation.setRepeatMode(Animation.REVERSE);
		iv.startAnimation(animation);
	}

	/**
	 * 平移
	 */
	public void click4(View view) {
		// paras 1fromXType水平相当于谁平移 2fromXValue水平初始位置 3toXType 4toXValue
		// 参数3,4:移动后的位置 5fromYValue 6fromYValue 7toYType 8toYValue 与1234类似
		// 只是是y方向
		TranslateAnimation animation = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 1.0f);
		// 动画播放5秒
		animation.setDuration(5000);
		// 重复播放两次 2+1=3
		animation.setRepeatCount(2);
		// 反向变化
		animation.setRepeatMode(Animation.REVERSE);
		iv.startAnimation(animation);
	}
	
	/**
	 * 组合动画
	 */
	public void click5(View view) {
		//参数动画变化速率
		AnimationSet set = new AnimationSet(false);
		TranslateAnimation ta = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 1.0f);
		// 动画播放5秒
		ta.setDuration(5000);
		// 重复播放两次 2+1=3
		ta.setRepeatCount(2);
		// 反向变化
		ta.setRepeatMode(Animation.REVERSE);


		RotateAnimation ra = new RotateAnimation(0, 360,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		// 动画播放5秒
		ra.setDuration(5000);
		// 重复播放两次 2+1=3
		ra.setRepeatCount(2);
		// 反向变化
		ra.setRepeatMode(Animation.REVERSE);
		
		ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		// 动画播放5秒
		sa.setDuration(5000);
		// 重复播放两次 2+1=3
		sa.setRepeatCount(2);
		// 反向变化
		sa.setRepeatMode(Animation.REVERSE);
		
		set.addAnimation(sa);
		set.addAnimation(ra);
		set.addAnimation(ta);
		iv.startAnimation(set);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值