码迷,mamicode.com
首页 > 移动开发 > 详细

Android:关于Animation的几种常见的动画

时间:2015-04-11 13:15:57      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:android   动画   animation   

适当的添加一些动画效果,能够获得更好的用户体验,这次讲讲一些常见的动画~
如:透明动画,渐变动画等等。

先看一下运行截图:

技术分享

附上代码,注释写在代码中:

MainActivity.java:

package com.vrinux.animotiondemo;

import android.app.Activity;
import android.os.Bundle;
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 imgObj;

    private Animation animation;

    private AnimationSet animationSet;

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

        imgObj = (ImageView) findViewById(R.id.imgid);

    }

    public void animationshow(View view) {
        switch (view.getId()) {
        case R.id.btn01id:
            /**
             * 透明动画: AlphaAnimation(float fromAlpha,//变化起始状态的Alpha值; float
             * toAlpha)//变化结束状态的Alpha值;
             */
            animation = new AlphaAnimation(0.0f, 1.0f);
            // 设置动画时长;
            animation.setDuration(3000);
            // 为组件添加动画;
            imgObj.setAnimation(animation);
            break;

        case R.id.btn02id:
            /**
             * 渐变动画: ScaleAnimation(float fromX, //变化起始状态x轴(即宽度)占控件的x轴(即宽度)的比例,
             * 1.0f为控件的宽度,0.0f表示没有宽度, 大于1.0f标识控件宽度的相应倍数; float
             * toX,//变化结束状态x轴(即宽度)占控件的x轴(即宽度)的比例; float fromY,
             * //变化起始状态y轴(即高度)占控件的y轴(即高度)的比例,和fromX同理; float toY,
             * //变化结束状态y轴(即高度)占控件的y轴(即高度)的比例; float pivotX,//变化的原点x轴坐标; float
             * pivotY)//变化的原点y轴坐标;
             */
            animation = new ScaleAnimation(0.5f, 2.0f, 0.5f, 2.0f, 50.0f, 50.0f);
            animation.setDuration(3000);
            imgObj.setAnimation(animation);
            break;

        case R.id.btn03id:
            /**
             * 位移动画: TranslateAnimation(float fromXDelta, //位移起始x轴位置; float
             * toXDelta, //位移结束x轴位置; float fromYDelta, //位移起始y轴位置; float
             * toYDelta)//位移结束y轴位置;
             */
            animation = new TranslateAnimation(0.0f, 100.0f, 0.0f, 100.0f);
            animation.setDuration(3000);
            imgObj.setAnimation(animation);
            break;

        case R.id.btn04id:
            /**
             * 旋转动画: RotateAnimation(float fromDegrees,//起始旋转度数; float
             * toDegrees, //结束旋转度数; float pivotX, //旋转原点的x轴坐标; float
             * pivotY)//旋转原点的y轴坐标;
             */
            animation = new RotateAnimation(0.0f, 360.0f, 50.0f, 50.0f);
            animation.setDuration(3000);
            imgObj.setAnimation(animation);
            break;

        case R.id.btn05id:
            /**
             * AnimationSet(true)//动画集合(组合);顾名思义;
             */
            AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f);
            ScaleAnimation animation2 = new ScaleAnimation(0.5f, 2.0f, 0.5f,
                    2.0f, 50.0f, 50.0f);
            TranslateAnimation animation3 = new TranslateAnimation(0.0f,
                    100.0f, 0.0f, 100.0f);
            RotateAnimation animation4 = new RotateAnimation(0.0f, 360.0f,
                    50.0f, 50.0f);

            animationSet = new AnimationSet(true);
            animationSet.addAnimation(animation1);
            animationSet.addAnimation(animation2);
            animationSet.addAnimation(animation3);
            animationSet.addAnimation(animation4);
            animationSet.setDuration(4000);
            imgObj.setAnimation(animationSet);

            break;
        }
    }
}

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgid"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:src="@drawable/img" />

    <Button
        android:id="@+id/btn01id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="animationshow"
        android:text="透明动画"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn02id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="animationshow"
        android:text="渐变动画"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn03id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="animationshow"
        android:text="位移动画"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn04id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="animationshow"
        android:text="旋转动画"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn05id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="animationshow"
        android:text="动画组合"
        android:textSize="20sp" />

</LinearLayout>

图片资源:

img.jpg:

技术分享

Android:关于Animation的几种常见的动画

标签:android   动画   animation   

原文地址:http://blog.csdn.net/vrinux/article/details/44994357

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!