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

来源:互联网 发布:ubuntu 设置默认路径 编辑:程序博客网 时间:2024/05/22 14:17

简介:补间动画:做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;@Overrideprotected 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=3animation.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=3animation.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=3animation.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=3animation.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=3ta.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=3ra.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=3sa.setRepeatCount(2);// 反向变化sa.setRepeatMode(Animation.REVERSE);set.addAnimation(sa);set.addAnimation(ra);set.addAnimation(ta);iv.startAnimation(set);}}


0 0
原创粉丝点击