初学Android,图形图像之补间动画(三十六)

news/2024/7/7 16:06:45 标签: android, 图形, animation, layout, encoding, button

补间动画,开发人员只需指定开始,动画结束"关键帧",而动画变化的"中间帧"由系统计算,并补齐,所以被称为补间动画

上面的例子就是这样,只是定义了动作,变化的帧都由Android自行计算

主要是定义Interpolator参数,其API列表就不写了,用的时候再查

上面的例子定义了两个动画文件

anim.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定动画匀速改变 -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/linear_interpolator">
	<!-- 定义缩放变换 -->
	<scale android:fromXScale="1.0"  
		android:toXScale="0.01"  
		android:fromYScale="1.0"  
		android:toYScale="0.01"  
		android:pivotX="50%" 
		android:pivotY="50%" 
		android:fillAfter="true" 
		android:duration="3000"/> 
	<!-- 定义透明度的变换 -->
	<alpha 
		android:fromAlpha="1" 
		android:toAlpha="0.05" 
		android:duration="3000"/> 
	<!-- 定义旋转变换 -->
	<rotate 
		android:fromDegrees="0" 
		android:toDegrees="1800" 
		android:pivotX="50%" 
		android:pivotY="50%" 
		android:duration="3000"/>
</set>

reverse.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定动画匀速改变 -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/linear_interpolator"
	android:startOffset="3000">
	<!-- 定义缩放变换 -->
	<scale android:fromXScale="0.01"  
		android:toXScale="1"  
		android:fromYScale="0.01"  
		android:toYScale="1"  
		android:pivotX="50%" 
		android:pivotY="50%" 
		android:fillAfter="true" 
		android:duration="3000"/> 
	<!-- 定义透明度的变换 -->
	<alpha 
		android:fromAlpha="0.05" 
		android:toAlpha="1" 
		android:duration="3000"/> 
	<!-- 定义旋转变换 -->
	<rotate 
		android:fromDegrees="1800" 
		android:toDegrees="0" 
		android:pivotX="50%" 
		android:pivotY="50%" 
		android:duration="3000"/> 
</set>
主界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:gravity="center_horizontal">
  <Button android:id="@+id/bn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/play" /> 
  <ImageView android:id="@+id/girl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitCenter" android:src="@drawable/girl" />   
</LinearLayout>


主界面代码,控制播放动画

package Wangli.Graphics.TweenAnim;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class TweenAnim extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final ImageView girl = (ImageView)findViewById(R.id.girl);
        //加载第一份动画资源
        final Animation anim = AnimationUtils.loadAnimation(this, R.anim.anim);
        //设置动画结束后保留结束状态
        anim.setFillAfter(true);
        //加载第二份动画
        final Animation reverse = AnimationUtils.loadAnimation(this, R.anim.reverse);
        //设置动画结束后保留结束状态
        reverse.setFillAfter(true);
        Button bn = (Button)findViewById(R.id.bn);
        final Handler handler = new Handler()
        {
        	public void handleMessage(Message msg)
        	{
        		if(msg.what == 0x123)
        		{
        			girl.startAnimation(reverse);
        		}
        	}
        };
        bn.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View arg0)
        	{
        		girl.startAnimation(anim);
        		//设置3.5秒后启动第二个动画
        		new Timer().schedule(new TimerTask()
        		{
        			public void run()
        			{
        				handler.sendEmptyMessage(0x123);
        			}
        		}, 3500);
        	}
        });
    }
}




http://www.niftyadmin.cn/n/1411360.html

相关文章

react-router-dom示例讲解(六)——未匹配(404页面)

react-router-dom的官方示例中&#xff0c;未匹配的示例也是一个比较简单的示例了。其核心就是Switch组件的匹配规则——仅仅匹配第一个符合条件的Route。 实现的效果图&#xff1a; 相关示例代码&#xff1a; import React, {Component} from react; import {BrowserRo…

初学Android,图形图像之混合使用逐帧动画和补间动画(三十七)

下面例子混合使用了逐帧动画和补间动画&#xff0c;还有一个缺点&#xff0c;就是画面闪烁&#xff0c;一直没有找到解决办法上面点击ImageView,上面的人物就开始走路跟移动 定义动画文件<?xml version"1.0" encoding"utf-8"?> <animation-list…

POJ 2800 Joseph’s Problem 数论找规律

Description 求 Input 两个整数n和k(1<n,k<1e9) Output 输出 Sample Input 5 3 Sample Output 7 暴力超时&#xff0c;这样就打下表找下余数的规律。输入100,27&#xff0c;一下子就可以看出来&#xff0c;倒着的看&#xff0c;是一段一段的等差序列。 例如100 25 除数 …

初学Android,图形图像之自定义补间动画(三十八)

Android提供Animation作为补间动画抽象基类&#xff0c;而且为该抽象基类提供了 AlphaAnimation,RotateAnimation,ScaleAnimation,TranslateAnimation四个类 但是在实际项目中&#xff0c;这些很可能不够用&#xff0c;可能需要一些更复杂的动画&#xff0c;比如说立体空间的旋…

CentOS6.6系统安装

一步步教你安装CentOS 6.6系统说明&#xff1a;截止目前CentOS 6.x最新版本为CentOS 6.6&#xff0c;下面介绍CentOS 6.6的具体安装配置过程服务器相关设置如下&#xff1a;操作系统&#xff1a;CentOS 6.6 64位IP地址&#xff1a;192.168.21.129网关&#xff1a;192.168.21.2D…

react-router-dom示例讲解(七),路径递归

在react-router-dom的官方示例中&#xff0c;路径递归不是最难的&#xff0c;但是绝对是一个比较难理解的示例demo。 本示例的效果图如下&#xff1a; 相关示例核心代码&#xff1a; import React, {Component} from react; import {BrowserRouter as Router,Route,Link,S…

初学Android,图形图像之使用SurfaceView(三十九)

SurfaceView支持双缓冲&#xff0c;在实现游戏绘图上面比View更出色 它有几个重要方法 Canvas lockCanvas(): 锁定整个SurfaceView对象,获取该Surface上的Canvas Canvas lockCanvas(Rect dirty): 锁定SurfaceView上Rect划分的区域&#xff0c;获取该Surface上的Canvas unlock…

面向万物互联的时序数据库HiTSDB

现在填写调查问卷&#xff0c;将优先获得公测资格 当前物联网的浪潮席卷全球&#xff0c;甚至于人们还没有真正意识到物联网的存在&#xff0c;但它已经无处不在 。个人智能手环&#xff0c;家庭里使用的智能空调&#xff0c;空气净化器&#xff0c;电饭煲&#xff0c;到社会化…