android Tween Animation属性设置方法实例

在Android开发中,Animation是用来给控件制作效果的。大多数的控件都可以用这个类,这个类包含了4种基本动作,分别为移动,旋转,淡入淡出,缩放。在使用Animation时,可以在.java文件中用java代码对其进行设置,这样的优点是可以方便调试程序效果;另外一种方法就是在xml中对控件的属性做设置,好处是代码的重用性比较高,缺点是不方便调试。

一、在java代码中使用Animation
在java代码中使用Animation主要分为下面4个步骤。
创建一个AnimationSet类,AnimationSet类是一个Animation集合,里面可以许多Animation,且在AnimationSet中设置的属性适用于里面的所有Animation。
根据我们需要的动态效果创建一个Animation类,主要有4个这样的类,分别为AlphaAnimation,ScaleAnimation,RotateAnimation,TranslateAnimation,分别对应着一种动画效果。
将上面建立好的Animation设置相应的动态属性,然后加入到AnimationSet中。
最后在需要适用这个动态的效果的控件中加载这个AnimationSet。

这里,做了一个简单的实验,分别试了下上面的动态效果。实验室对一个image图标进行动态演示,下面有4个按钮,每个按钮对应一个动态演示的效果。这4中效果分别是:image图标由完全透明到完全不透明变化,持续时间为1s;image图标由原来大小尺寸沿自身尺寸中心逐渐缩放到0,持续时间为1s;image图标以自身中心为圆心旋转360度,持续时间为1s;image图标从自身位置开始同时向右和向下移动了imageView控件的宽和高长度。
界面如下所示(动态效果就不一张一张截图演示了):

在设置Animation属性的时候,有一点需要注意的是,在进行尺度缩放,平移,旋转会涉及到中心点以及移动的距离那些参数,这些参数的值的设置是需要依据它值属性来的,如果值属性为Animation.RELATIVE_TO_SELF,那么就是参考控件自身的坐标,如果是Animation.RELATIVE_TO_PARENT,那么就是参考程序界面的坐标。

Java文件代码如下:
复制代码 代码如下:
package com.example.anim_1;

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

private ImageView image  = null;
    private Button alpha = null;
    private Button scale = null;
    private Button rotate = null;
    private Button translate = null;

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

image = (ImageView)findViewById(R.id.image);
        alpha = (Button)findViewById(R.id.alpha);
        scale = (Button)findViewById(R.id.scale);
        rotate = (Button)findViewById(R.id.rotate);
        translate = (Button)findViewById(R.id.translate);

alpha.setOnClickListener(new AlphaButtonListener());
        scale.setOnClickListener(new ScaleButtonListener());
        rotate.setOnClickListener(new RotateButtonListener());
        translate.setOnClickListener(new TranslateButtonListener());

}

private class AlphaButtonListener implements OnClickListener{

public void onClick(View v) {
            // TODO Auto-generated method stub
            //这里设置ture参数表示共享interpolator
            AnimationSet alpha_animation_set = new AnimationSet(true);
            //从完全不透明到完全透明
            //这里的数字后面用f难道表示浮点型?
            AlphaAnimation alpha_animation = new AlphaAnimation(1.0f, 0.0f);   
            alpha_animation_set.addAnimation(alpha_animation);   
            alpha_animation.setDuration(1000);//1s钟   
            image.startAnimation(alpha_animation_set);
        }

}

private class ScaleButtonListener implements OnClickListener{

public void onClick(View v) {
            // TODO Auto-generated method stub
            AnimationSet scale_animation_set = new AnimationSet(true);
            //以自身为尺度缩放中心,从原大小尺寸逐渐缩放到0尺寸
            ScaleAnimation scale_animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
                                            Animation.RELATIVE_TO_SELF, 0.5f,
                                            Animation.RELATIVE_TO_SELF, 0.5f);   
            scale_animation_set.addAnimation(scale_animation);   
            scale_animation.setDuration(1000);//1s钟   
            image.startAnimation(scale_animation_set);
        }

}

private class RotateButtonListener implements OnClickListener{

public void onClick(View v) {
            // TODO Auto-generated method stub
            AnimationSet rotate_animation_set = new AnimationSet(true);
            //以自身中心为圆心,旋转360度
            RotateAnimation rotate_animation = new RotateAnimation(0, 360,
                                            Animation.RELATIVE_TO_SELF, 0.5f,
                                            Animation.RELATIVE_TO_SELF, 0.5f);   
            rotate_animation_set.addAnimation(rotate_animation);   
            rotate_animation.setDuration(1000);//1s钟   
            image.startAnimation(rotate_animation_set);
        }

}

private class TranslateButtonListener implements OnClickListener{

public void onClick(View v) {
            // TODO Auto-generated method stub
            AnimationSet translate_animation_set = new AnimationSet(true);
            //以自身为坐标系和长度单位,从(0,0)移动到(1,1)
            TranslateAnimation translate_animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                                                        0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
                                                        Animation.RELATIVE_TO_SELF, 0.0f,
                                                        Animation.RELATIVE_TO_SELF, 1.0f);   
            translate_animation_set.addAnimation(translate_animation);   
            translate_animation.setDuration(1000);//1s钟   
            image.startAnimation(translate_animation_set);
        }

}

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

本次试验的xml布局文件代码如下:
复制代码 代码如下:
<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" >

<ImageView
        android:id="@+id/image"
        android:layout_centerHorizontal="true"
        android:paddingTop="80px"
        android:paddingBottom="80px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/girl"
        />

<LinearLayout
        android:id="@+id/button_group"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:layout_below="@id/image"
        >
        <Button
            android:id="@+id/alpha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5px"
            android:text="@string/alpha"
            android:ems="1"
            />
        <Button
            android:id="@+id/scale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5px"
            android:text="@string/scale"
            android:ems="1"
            />
        <Button
            android:id="@+id/rotate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5px"
            android:text="@string/rotate"
            android:ems="1"
            />
        <Button
            android:id="@+id/translate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5px"
            android:text="@string/translate"
            android:ems="1"
            />
    </LinearLayout>

</RelativeLayout>

二、在xml文件中使用animation
在xml中使用animation,只需对xml文件中的动画属性进行设置,这样写好的一个xml文件可以被多次利用。
完成该功能步骤大概如下:
首先在res目录下新建anim目录,在anim目录里面建立xml文件,每个xml文件对应里面可以设置各种动画,可以将上面讲到的4种动画都放里面。这些xml文件的属性设置应该都在set标签下面。
在java语句中,只需要对需要动画显示的控件加载一个animation对象,该animation对象采用AnimationUtils.loadAnimation()方法来获得,该方法里面就有一个参数为anim下的一个xml,因此这个控件也就得到了相应xml里面设置的动画效果了。
这次试验的效果和第一种情况的一样,只是图片换了一张。
效果演示界面如下:

在用xml设置动态属性的时候,有些属性比如旋转中心,平移尺寸的设置,有如下规则:
如果android:pivotX=”N”,则表示绝对坐标比例,即屏幕的坐标比例。
如果android:pivotX=”N%”,则表示相对自身的坐标比例。
如果android:pivotX=”N%p”,则表示相对于父控件的坐标比例。

实验代码如下(附录有工程code下载链接):

MainActivity.java:
复制代码 代码如下:
package com.example.anim_2;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

private ImageView image  = null;
    private Button alpha = null;
    private Button scale = null;
    private Button rotate = null;
    private Button translate = null;

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

image = (ImageView)findViewById(R.id.image);
        alpha = (Button)findViewById(R.id.alpha);
        scale = (Button)findViewById(R.id.scale);
        rotate = (Button)findViewById(R.id.rotate);
        translate = (Button)findViewById(R.id.translate);

alpha.setOnClickListener(new AlphaButtonListener());
        scale.setOnClickListener(new ScaleButtonListener());
        rotate.setOnClickListener(new RotateButtonListener());
        translate.setOnClickListener(new TranslateButtonListener());

}

private class AlphaButtonListener implements OnClickListener{

public void onClick(View v) {
            // TODO Auto-generated method stub
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
            image.startAnimation(animation);
        }

}

private class ScaleButtonListener implements OnClickListener{

public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
            image.startAnimation(animation);
        }

}

private class RotateButtonListener implements OnClickListener{

public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
            image.startAnimation(animation);
        }

}

private class TranslateButtonListener implements OnClickListener{

public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
            image.startAnimation(animation);
        }

}

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

activity_main.xml:
复制代码 代码如下:
<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"
    >

<LinearLayout
        android:id="@+id/image_layout"
        android:layout_width="fill_parent"
        android:layout_height="250dip"
        android:gravity="center"
        >
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/olympic"
            />          
    </LinearLayout>

<LinearLayout
        android:id="@+id/button_group"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/image_layout"
        >
        <Button
            android:id="@+id/alpha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:text="@string/alpha"
            android:ems="1"
            />
        <Button
            android:id="@+id/scale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:text="@string/scale"
            android:ems="1"
            />
        <Button
            android:id="@+id/rotate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:text="@string/rotate"
            android:ems="1"
            />
        <Button
            android:id="@+id/translate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:text="@string/translate"
            android:ems="1"
            />
    </LinearLayout>

</LinearLayout>

anim/alpha.xml:
复制代码 代码如下:
set
    android:interpolator="@android:anim/accelerate_interpolator"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="1000"
        />  
</set>

anim/scale.xml:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <scale
        android:fromXScale="1.0"
        android:toXScale="0.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"      
        />

</set>

anim/rotate.xml:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    >  
    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"
        />

</set>

anim/translate.xml:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    android:interpolator="@android:anim/accelerate_interpolator"
    >
    <translate
        android:fromXDelta="0%"
        android:toXDelta="100%"
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="1000"
        />
</set>

总结:本次实验主要讲android中animation这个类对控件的动画效果设置,对用java代码和用xml代码2种方式都做了个简单的实验。懂得其使用方法的大体流程。

作者:tornadomeet

时间: 2024-07-31 20:37:22

android Tween Animation属性设置方法实例的相关文章

android Tween Animation属性设置方法实例_Android

在Android开发中,Animation是用来给控件制作效果的.大多数的控件都可以用这个类,这个类包含了4种基本动作,分别为移动,旋转,淡入淡出,缩放.在使用Animation时,可以在.java文件中用java代码对其进行设置,这样的优点是可以方便调试程序效果:另外一种方法就是在xml中对控件的属性做设置,好处是代码的重用性比较高,缺点是不方便调试. 一.在java代码中使用Animation在java代码中使用Animation主要分为下面4个步骤.创建一个AnimationSet类,An

Android编程中TextView字体属性设置方法(大小、字体、下划线、背景色)_Android

本文实例讲述了Android编程中TextView字体属性设置方法(大小.字体.下划线.背景色).分享给大家供大家参考,具体如下: import android.content.Context; import android.graphics.Color; import android.text.SpannableString; import android.text.Spanned; import android.text.style.AbsoluteSizeSpan; import andr

Android TextView字体颜色设置方法小结_Android

本文实例总结了Android TextView字体颜色设置方法.分享给大家供大家参考,具体如下: 对于setTextView(int a)这里的a是传进去颜色的值.例如,红色0xff0000是指0xff0000如何直接传入R.color.red是没有办法设置颜色的,只有通过文章中的第三种方法先拿到资源的颜色值再传进去. tv.setTextColor(this.getResources().getColor(R.color.red)); 关键字: android textview color T

Android连接指定Wifi的方法实例代码

本篇文章主要记录一下Android中打开Wifi.获取Wifi接入点信息及连接指接入点的方法. 自己写的demo主要用于测试接口的基本功能,因此界面及底层逻辑比较粗糙. demo的整体界面如下所示: 上图中的OPEN按键负责开启Wifi: GET按键负责获取扫描到的接入点信息. 当获取到接入点信息后,我选取了其中的名称及信号强度,以列表的形式显示在主界面下方,如下图: 当点击列表中的Item时,就会去连接对应的接入点. 自己的逻辑比较简单,测试时的代码,假定连接的是不许要密码或密码已知的接入点.

Android TextView字体颜色设置方法小结

本文实例总结了Android TextView字体颜色设置方法.分享给大家供大家参考,具体如下: 对于setTextView(int a)这里的a是传进去颜色的值.例如,红色0xff0000是指0xff0000如何直接传入R.color.red是没有办法设置颜色的,只有通过文章中的第三种方法先拿到资源的颜色值再传进去. tv.setTextColor(this.getResources().getColor(R.color.red)); 关键字: android textview color T

Android线程的优先级设置方法技巧

对于Android平台上的线程优先级设置来说可以处理很多并发线程的阻塞问题,比如很多无关紧要的线程会占用大量的CPU时间,虽然通过了MultiThread来解决慢速I/O但是合理分配优先级对于并发编程来说十分重要.Android在线程方面主要使用的是Java本身的Thread类,我们可以在Thread或Runnable接口中的run方法首句加入Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); //设置线程优先级为后台,这

【ANDROID游戏开发十四】深入ANIMATION,在SURFACEVIEW中照样使用ANDROID—TWEEN ANIMATION!

本站文章均为 李华明Himi 原创,转载务必在明显处注明:  转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/android-game/331.html ----------------------- 『很多童鞋说我的代码运行后,点击home或者back后会程序异常,如果你也这样遇到过,那么你肯定没有仔细读完Himi的博文,第十九篇Himi专门写了关于这些错误的原因和解决方法,这里我在博客都补充说明下,省的童鞋们总疑惑这一块:请点击下面联系进入阅读:

Android编程之九宫格实现方法实例分析_Android

本文实例讲述了Android编程之九宫格实现方法.分享给大家供大家参考,具体如下: 显示九宫格需要用GridView , 要显示每个格子中的视图有两种方式,第一种方式是做成xml文件,再将xml文件做成视图.第二种方式就是在代码中构建出这样一种布局,这里采用第一种方式来实现: GridView: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="ht

Android编程之九宫格实现方法实例分析

本文实例讲述了Android编程之九宫格实现方法.分享给大家供大家参考,具体如下: 显示九宫格需要用GridView , 要显示每个格子中的视图有两种方式,第一种方式是做成xml文件,再将xml文件做成视图.第二种方式就是在代码中构建出这样一种布局,这里采用第一种方式来实现: GridView: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="ht