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,从而使活动透明至不透明。
1
2
3
4<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。
1
2
3
4<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。 在此文件中,我们将为相应的动画添加三个按钮。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23<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
1
2
3
4
5
6
7
8
9
10
11
12
13
14<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。 源代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13package 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动画的方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49package 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
最后
以上就是烂漫小松鼠最近收集整理的关于Android活动动画自定义教程的全部内容,更多相关Android活动动画自定义教程内容请搜索靠谱客的其他文章。
发表评论 取消回复