Android活动动画自定义教程

如果您正在考虑自定义Activity过渡的动画,那么您可能会寻找Android 4.1(Jelly bean)中引入的ActivityOptions类。 此类提供了三种方法,可以帮助您自定义活动动画。 这些在下表中给出。

ActivityOptions类方法

功能名称 描述
makeCustomAnimation 此方法允许传递自定义动画,并在启动Atyctivi时对其进行相应渲染。 在这里,您可以传递动画以过渡出Activity以及在Activity中过渡
makeScaleUpAnimation 此方法将活动从初始大小按比例放大到最终的代表性大小。 它可以用于从启动此活动的视图中按比例放大活动。
makeThumbnailScaleUpAnimation 在此动画中,活动的缩略图按比例放大到活动的最终大小。
捆绑 此方法返回Bundle对象,该对象可以在startActivity()方法中传递以获得所需的动画。

有关ActivityOptions的更多信息,请参见此处

项目信息:有关项目的元数据。
平台版本: Android API级别16。
IDE: Eclipse Helios服务版本2 模拟器: Android 4.1(API 16)

先决条件:对Android应用程序框架和Intent有初步了解。

样本源代码:

我们使用eclipse创建一个项目,然后在res(resource)文件夹下创建anim(Animation)文件夹。 现在,我们将在xml文件中定义动画属性,并将其放在anim文件夹中。 在这里,我们将定义两个动画,这些动画将在makeCustomAnimation()方法中使用。 makeCustomAnimation需要两个动画文件,一个用于传入活动,另一个用于传出活动。 动画中的任何一个都可以为null,在这种情况下,将不为该特定活动执行动画。 现在,我们将为传入的活动定义fade_in.xml。 在这里,我们将Alpha值从0更改为1,从而使活动透明至不透明。

<alpha xmlns:android='http://schemas.android.com/apk/res/android'
   android:interpolator='@android:anim/anticipate_interpolator'
   android:fromAlpha='0.0' android:toAlpha='1.0'
   android:duration='@android:integer/config_longAnimTime' />

现在,我们将定义另一个名为fade_out.xml的文件,用于过渡出Activity。 在这里,我们将Alpha的值从1更改为0。

<alpha xmlns:android='http://schemas.android.com/apk/res/android'
   android:interpolator='@android:anim/anticipate_interpolator'
   android:fromAlpha='1.0' android:toAlpha='0.0'
   android:duration='@android:integer/config_longAnimTime' />

现在,我们将为主要活动定义布局文件。 将此文件命名为acitivity_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' >

    <Button
        android:layout_width='match_parent'
        android:layout_height='wrap_content'
        android:onClick='fadeAnimation'
        android:text='@string/btFadeAnimation' />
    <Button
        android:layout_width='match_parent'
        android:layout_height='wrap_content'
        android:onClick='scaleupAnimation'
        android:text='@string/btScaleupAni' />
   <Button
        android:layout_width='match_parent'
        android:layout_height='wrap_content'
        android:onClick='thumbNailScaleAnimation'
        android:text='@string/btThumbNailScaleupAni' />

</LinearLayout>

您可能已经注意到,我们已经在每个按钮上附加了onclick方法。 使用startActivity()方法启动活动时,这些方法将为活动设置动画。 现在,让我们使用一个ImageView为目标Activity定义另一种布局。 将图像放在可绘制文件夹中,然后将该图像用作“图像”视图的src。 在这里,我将“ freelance2.jpg”图像放置在可绘制文件夹中,并使用了android:src标签来使用该图像。 将布局文件命名为activity_animation.xml

<RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android'
   android:layout_width='match_parent'
   android:layout_height='match_parent'
   android:orientation='vertical' >
   <ImageView
     android:id='@+id/imageView1'
     android:layout_width='match_parent'
     android:layout_height='match_parent'
     android:layout_marginRight='44dp'
     android:layout_marginTop='54dp'
     android:layout_centerInParent='true'
     android:src='@drawable/freelancer2' />

</RelativeLayout>

定义此布局后,我们需要定义相应的Activity类。 让我们将此类命名为AnimationActivity。 源代码如下:

package com.example.jellybeananimationexample;

import android.app.Activity;
import android.os.Bundle;

public class AnimationActivity extends Activity {

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

现在,该定义MainActivity类了,该类具有自定义Activity动画的方法。

package com.example.jellybeananimationexample;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

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

 public void scaleupAnimation(View view) {
  // Create a scale-up animation that originates at the button
  // being pressed.
  ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(view, 0, 0,
    view.getWidth(), view.getHeight());
  // Request the activity be started, using the custom animation options.
  startActivity(new Intent(MainActivity.this, AnimationActivity.class),
    opts.toBundle());
 }

 public void thumbNailScaleAnimation(View view) {
  view.setDrawingCacheEnabled(true);
  view.setPressed(false);
  view.refreshDrawableState();
  Bitmap bitmap = view.getDrawingCache();
  ActivityOptions opts = ActivityOptions.makeThumbnailScaleUpAnimation(
    view, bitmap, 0, 0);
  // Request the activity be started, using the custom animation options.
  startActivity(new Intent(MainActivity.this, AnimationActivity.class),
    opts.toBundle());
  view.setDrawingCacheEnabled(false);
 }

 public void fadeAnimation(View view) {
  ActivityOptions opts = ActivityOptions.makeCustomAnimation(
    MainActivity.this, R.anim.fade_in, R.anim.fade_out);
  // Request the activity be started, using the custom animation options.
  startActivity(new Intent(MainActivity.this, AnimationActivity.class),
    opts.toBundle());
 }

}

完成代码后,执行它。 单击应用程序按钮后,您将看到自定义的活动动画。

您可以从此处获取更新的Android Animation源代码

对于Android教程,请访问此处

参考: Code4Reference博客上的JCG合作伙伴 Rakesh Cusat提供的有关定制Android Activity Animation的教程


翻译自: https://www.javacodegeeks.com/2012/08/android-activity-animation.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值