服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - android Tween Animation属性设置方法实例

android Tween Animation属性设置方法实例

2021-02-07 18:45Android开发网 Android

在Android开发中,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控件的宽和高长度。
界面如下所示(动态效果就不一张一张截图演示了):
android Tween Animation属性设置方法实例


在设置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里面设置的动画效果了。
这次试验的效果和第一种情况的一样,只是图片换了一张。
效果演示界面如下:

android Tween Animation属性设置方法实例

 


在用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

延伸 · 阅读

精彩推荐