当前位置:操作系统 > 安卓/Android >>

Android---Tween动画示例(代码中定义的动画)

1、在layout目录中新建XML文件:

[html]
<span style="font-size:18px;"><?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"> 
    <ImageView 
        android:id="@+id/imgTween" 
        android:src="@drawable/c01" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_weight="1.0" /> 
    <Button 
        android:id="@+id/btnControl" 
        android:text="开始" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 
 
</LinearLayout> 
</span> 
<span style="font-size:18px;"><?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">
 <ImageView
  android:id="@+id/imgTween"
  android:src="@drawable/c01"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:layout_weight="1.0" />
 <Button
  android:id="@+id/btnControl"
  android:text="开始"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />

</LinearLayout>
</span>2、在Activity代码中写:

[java]
<span style="font-size:18px;">package com.bison; 
 
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; 
import android.widget.ImageView.ScaleType; 
 
public class TweenAnimationDemo extends Activity implements OnClickListener { 
    // 声明一个开始停止的标识符  
    private boolean flags = true; 
    private ImageView imgTween; 
    private Button btnCtrl; 
    private AnimationSet as; 
 
    /** 初始化 */ 
    public void init() { 
        // 声明AnimationSet  
        as = new AnimationSet(true); 
        // 声明Alpha、Scale、Translate、Rotate 等Animation  
        AlphaAnimation aa = alphaAnim(1, 0.3f); 
        ScaleAnimation sa = scaleAnim(0.2f, 1.0f, 0.2f, 1.0f, 1, 1); 
        TranslateAnimation ta = translateAnim(50f, 100f, 50f, 100f); 
        RotateAnimation ra = rotateAnim(0, 360); 
        // 添加各种动画  
        as.addAnimation(aa); 
        as.addAnimation(sa); 
        as.addAnimation(ta); 
        as.addAnimation(ra); 
 
        imgTween = (ImageView) findViewById(R.id.imgTween); 
        imgTween.setScaleType(ScaleType.CENTER_INSIDE); 
 
        btnCtrl = (Button) findViewById(R.id.btnControl); 
        btnCtrl.setOnClickListener(this); 
 
    } 
 
    /** 缩放 */ 
    private ScaleAnimation scaleAnim(float start_x, float end_x, float start_y, 
            float end_y, float x2, float y2) { 
        // 开始x坐标伸缩尺寸,结束x坐标伸缩尺寸,开始y坐标伸缩尺寸,结束y坐标伸缩尺寸,x轴的百分比,y轴的百分比  
        ScaleAnimation sa = new ScaleAnimation(start_x, end_x, start_y, end_y, 
                x2, y2); 
        sa.setDuration(3000); 
        sa.setRepeatMode(Animation.REVERSE); 
        sa.setRepeatCount(5); 
        return sa; 
    } 
 
    /** 透明度 */ 
    private AlphaAnimation alphaAnim(float x, float y) { 
        AlphaAnimation aa = new AlphaAnimation(x, y); 
        aa.setDuration(2000); 
        aa.setRepeatMode(Animation.REVERSE); 
        aa.setRepeatCount(5); 
        return aa; 
    } 
 
    /** 移动 */ 
    private TranslateAnimation translateAnim(float startX, float endX, 
            float startY, float endY) { 
        TranslateAnimation ta = new TranslateAnimation(startX, endX, startY, 
                endY); 
        ta.setDuration(3000); 
        ta.setRepeatMode(Animation.REVERSE); 
        ta.setRepeatCount(5); 
        return ta; 
    } 
 
    /** 旋转 */ 
    private Ro

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,