`
chengfengyang
  • 浏览: 21501 次
社区版块
存档分类
最新评论

android中的animations的用法 (一)

阅读更多
Animations 可分为两大类:

一 Tweened Animations,该类Animations提供了旋转,移动,伸展,和淡入淡出等效果

二 Frame-by-Frame Animations ,该类Animations 可以创建一个Drawable序列,这些Drawable可能按照指定的时间一个一个的显示,类似于电影.

Tweened Animations 有这4种分类

1 Alpha         淡入淡出

2 Rotate       旋转

3 Scale          缩放

4 Translate     移动

使用Tweened Animations 的步骤

1.  创建一个Animations 对象

2.  根据需要创建相应的Animation对象

3.  按照需求,为Animation 对象设置相应的数据

4.  将Animation对象添加到AnimationSet对象当中

5.  使用控件对象开始执行AnimationSet 

各种Animation 的主要属性

Animation type           |             attributes                 |               valid  values               

     Alpha                             fromAlpha/toAlpha                       Float  from 0  to  1

     Scale                            fromXScale/toXScale                      Float  from 0 to   1

                                        fromYScale/toYScale                      Float  from 0 to    1

                                        pivotX/pivotY                                String of the percentage of graphic

                                                                                           width/height from 0% to 100%

   Translate                        fromX/to X                                   Float from 0 to 1

                                        from Y/to Y                                   Float from 0 to 1

   Rotate                          fromDegrees/toDegrees                  Float from 0 to 360

                                       pivotX/pivotY                                String of the percentage of graphic

                                                                                           width/height from 0% to 100%

---------------------------

layout布局文件mian.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <ImageView
    android:id="@+id/imageViewId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon"
    />
    <Button 
    android:id="@+id/alphaID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="淡入淡出效果"
    />
    <Button 
    android:id="@+id/scaleID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="缩放效果"
    />
    <Button 
    android:id="@+id/rotateID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="旋转效果"
    />
    <Button 
    android:id="@+id/translateID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="移动效果"
    />
</LinearLayout>

activity文件:
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;

public class TestAnimation01Activity extends Activity {
    /** Called when the activity is first created. */
	private ImageView imageView = null;
	private Button alphaButton = null;
	private Button scaleButton = null;
	private Button rotateButton = null;
	private Button translateButton = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        imageView = (ImageView)findViewById(R.id.imageViewId);
        alphaButton = (Button)findViewById(R.id.alphaID);
        alphaButton.setOnClickListener(new AlphaButtonListener());
        scaleButton = (Button)findViewById(R.id.scaleID);
        scaleButton.setOnClickListener(new ScaleButtonListener());
        rotateButton = (Button)findViewById(R.id.rotateID);
        rotateButton.setOnClickListener(new RotateButtonListener());
        translateButton = (Button)findViewById(R.id.translateID);
        translateButton.setOnClickListener(new TranslateButtonListener());
    }
    private class AlphaButtonListener implements OnClickListener
    {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AnimationSet animationSet = new AnimationSet(true);
			AlphaAnimation alpha = new AlphaAnimation(1,0);
			alpha.setDuration(1000);
			animationSet.addAnimation(alpha);
			imageView.startAnimation(animationSet);
			
		}
    	
    }
    private class ScaleButtonListener implements OnClickListener
    {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AnimationSet animationSet = new AnimationSet(true);
			ScaleAnimation scale = new ScaleAnimation(1,0.1f,1,0.1f,Animation.RELATIVE_TO_SELF,05f,Animation.RELATIVE_TO_SELF,0.5f);
			scale.setDuration(2000);
			animationSet.addAnimation(scale);
			imageView.startAnimation(scale);
		}
    	
    }
    private class RotateButtonListener implements OnClickListener
    {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AnimationSet animationSet = new AnimationSet(true);
			RotateAnimation rotate = new RotateAnimation(0,360,Animation.RELATIVE_TO_PARENT,1f,Animation.RELATIVE_TO_PARENT,0f);
			rotate.setDuration(5000);
			animationSet.addAnimation(rotate);
			imageView.startAnimation(animationSet);
		}
    	
    }
    private class TranslateButtonListener implements OnClickListener
    {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AnimationSet animationSet = new AnimationSet(true);
			TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f);
			translate.setDuration(2000);
			animationSet.addAnimation(translate);
			imageView.startAnimation(animationSet);
			
		}
    	
    }
    
}

Tween Animations 的通用属性

1. setDuration(long durationMills)  设置动画持续时间(单位:毫秒)

2. setFillAfter(boolean fillAfter)  如果fillAfter为true,表示动画执行后,控件将停留在执行结束的状态

3. setFillBefore(boolean fillBefore) 如果fillBefore 的值为true,表示动画执行后,控件将回到动画执行之前的状态

4. setStartOffSet(long startOffSet)  设置动画执行之前的等待时间

5. setRepeatCount(int count)  设置动画重复执行的次数

--------------------

下面再介绍Animations 另一种使用方法

1.  在res文件夹下面新建一个名为anim的文件夹;

2.  创建xml文件,并首先加入set标签,改标签如下:

     <set xmlns:android=http://schemas.android.com/apk/res/android"

           android:interpolator="@android:anim/acccelerate_interpolator" >

     </set>

3. 在该标签当中加入rotate,alpha,scale,translate标签

4. 在代码当中使用AnimationUtils当中装载xml文件,并生成   Animation  对象

在rotate.xml文件中

android:pivotX的值共有三种设置方法:

1.  android:pivotX="50"  使用绝对位置定位

2.  android:pivotX="50%"  使用相对于控件本身定位

3.  android:pivotX="50%p"  使用相对于控件的父控件定位



分享到:
评论

相关推荐

    Android-animations

    2. **示例项目**:可能包含了一些演示如何使用"nineoldandroids"的示例应用程序,这些示例可以帮助开发者快速掌握库的使用方法。 3. **文档**:可能有库的使用指南或者API文档,方便开发者查阅和参考。 4. **测试...

    十大热门Android开源项目之 Material-Animations

    Material-Animations是专门为Android平台设计的一款开源项目,它致力于实现Material Design规范中的各种动画效果,使得开发者可以轻松地在自己的应用中添加生动、流畅的过渡动画。 项目的核心目标是提供Activity...

    常用动画效果 Animations

    第一种常见动画效果实现方法是使用帧动画。帧动画是通过播放一系列预先定义好的静态图像(帧)来创建连续动作的效果。例如,在Android平台上,我们可以使用`AnimationDrawable`类来创建帧动画。每个帧被指定为一个...

    动画animations

    本文将深入探讨标题为“动画animations”的Demo,它展示了如何使用XML和Java代码实现Android中的四种基本动画类型。这四种动画包括了平移动画(Translate Animation)、旋转动画(Rotate Animation)、缩放动画...

    android-view-animations-java,Awesome Library可使用视图动画系统执行倾斜动画,如关注、反弹、淡入、翻转、旋转、滑动和缩放视图。.zip

    这个开源项目"android-view-animations-java"将这些动画效果封装成易于使用的库,开发者可以快速集成到自己的项目中,无需从零开始编写复杂的动画代码。通过阅读源代码,开发者可以学习到如何利用Android的视图动画...

    android_animations:Android动画-准备使用XML文件

    本教程将聚焦于“android_animations”项目,这个项目主要关注如何使用XML文件来创建和管理Android应用程序中的动画。 首先,让我们理解XML在Android动画中的作用。XML(eXtensible Markup Language)是一种用于...

    Android开发之Animations动画用法实例详解

    在Android开发中,Animations动画是实现用户界面动态效果的关键技术,它可以增强应用的用户体验和视觉吸引力。本篇文章将深入探讨Android开发中Animations动画的类型、组成、模式以及如何通过XML和Java代码进行定义...

    android-animations-examples:一个带有动画的Android项目

    "android-animations-examples" 是一个专门展示Android动画效果的项目,它提供了多种类型的动画示例,帮助开发者深入理解并应用Android动画技术。下面我们将详细探讨Android中的动画系统及其关键概念。 1. **基础...

    android-animations:演示在Android上创建动画的各种方法的示例

    使用变形动画将视图转换为另一个视图。 您还可以在两个活动或片段之间进行变形。 这是非常好的外观效果,将帮助您的用户更好地了解屏幕上发生的更改。 TransformationLayout使用...

    android-support-v4.jar.zip

    在Android应用开发中,Android Support Library v4是一个至关重要的组件,它提供了对Android API Level 4及更高版本的向下兼容性,使得开发者可以使用最新的API特性,同时确保应用能在广泛的设备上运行。本文将深入...

    android动画demo

    在"android动画demo"中,你可能会看到这些基本动画的组合使用,例如一个按钮在点击后可能先逐渐变透明(Alpha),然后缩小(Scale)并旋转(Rotate)离开屏幕(Translate)。这种动画通常通过Animation XML文件定义...

    深入浅出Android代码

    SQLite数据库的使用、SharedPreferences、文件存储、ContentProvider等都是Android中常见的数据管理方式。通过查看对应的代码,开发者可以了解如何在实际应用中存储和读取数据。 网络编程是现代应用不可或缺的一...

    Android2.2 ApiDemos

    5. Notifications(通知):展示了不同类型的系统通知以及通知的使用方法。 6. Intents(意图):涵盖了启动活动、服务、广播接收者等不同意图的用法。 7. Services(服务):展示后台服务的创建和使用,包括绑定...

    android 设置Alpha值实现图片渐变效果

    在Android开发中,设置Alpha值实现图片渐变效果是一种常见的动画技术,它可以为用户界面增添动态感和视觉吸引力。本文将深入探讨如何通过调整Alpha透明度来实现这种效果,并结合提供的资源文件`Xh_04_05_Test_01`...

    Android 点击屏幕更换图片源码.zip

    4. **动画效果**(Animations):项目名为"MyNewAnimation",暗示可能在图片更换过程中使用了Android的动画功能。Android提供多种动画类型,如帧动画(Frame Animation)、属性动画(Property Animation)等,可以...

    Android UI 基础教程

    1. **活动(Activity)**:Activity是Android应用中的一个核心概念,它代表了用户可以交互的一个屏幕。了解如何创建和管理Activity,以及如何在它们之间进行跳转,是学习Android UI的基础。 2. **布局(Layouts)**...

    android4.4samples(包含ApiDemos)

    Android 4.4 SDK样品包是一个集合,包含了各种API的示例代码,这些代码展示了如何在实际应用中使用Android框架的各种组件和服务。开发者可以通过这些示例,了解如何操作UI元素,处理多媒体数据,进行网络通信,管理...

    Android 开发入门与实践 配套源码

    源码中可能包含了这些库的使用方法。 14. **测试与调试**:源码中可能包含单元测试、集成测试的代码,以及使用Logcat、Android Profiler等工具进行性能优化的实践。 15. **Gradle构建系统**:源码将展示如何配置...

    android代码示例常见

    1. **Activity与Intent**:Activity是Android应用中的基本组件,它代表了用户界面的一个屏幕。Intent用于在不同的组件之间传递消息,如启动Activity或服务。了解如何创建和管理Activity,以及正确使用Intent进行通信...

Global site tag (gtag.js) - Google Analytics