Android 使用自定义动画显示活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3087366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying activity with custom animation
提问by blork
I have a widget which starts an activity when it is clicked. I'd like to have some kind of fancy animation to display this activity, rather than the standard scroll-from-right of Android. I'm having problems setting it, though. This is what I have:
我有一个小部件,它在单击时启动一个活动。我想要某种精美的动画来显示此活动,而不是标准的 Android 右侧滚动。但是,我在设置它时遇到了问题。这就是我所拥有的:
slide_top_to_bottom.xml
slide_top_to_bottom.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:fromYDelta="-100%" android:toXDelta="0" android:duration="100" />
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="50" />
</set>
...which is referenced in anim.xml
...在anim.xml 中引用
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="50%"
android:animation="@anim/slide_top_to_bottom" />
But then where do I reference it from? I've tried both the base element of the activity I want to slide in, and the activitiy's entry in the manifest, both times with
但是我从哪里引用它呢?我已经尝试了要滑入的活动的基本元素和清单中活动的条目,两次都使用
android:layoutAnimation="@+anim/anim"
I might be doing this all wrong. Any help is much appreciated!
我可能做错了这一切。任何帮助深表感谢!
回答by Akos Cz
You can create a custom Theme with a reference to your own animation and apply it to your Activity in your manifest file. I was successful in applying a custom animation for a floating window using the following style definition. You might be able to do something similar if you set the parent of your style to be "@android:style/Animation.Activity"
您可以使用对您自己的动画的引用来创建自定义主题,并将其应用于清单文件中的 Activity。我成功地使用以下样式定义为浮动窗口应用了自定义动画。如果您将样式的父项设置为“@android:style/Animation.Activity”,您也许可以执行类似的操作
Look at the following files for further details on what you can override.
查看以下文件以获取有关您可以覆盖的内容的更多详细信息。
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xmlhttps://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values /themes.xml
Here's my a portion of my styles.xml and manifest.xml
这是我的 style.xml 和 manifest.xml 的一部分
styles.xml
样式文件
<style name="MyTheme" parent="@android:style/Theme.Panel">
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowAnimationStyle">@style/MyAnimation.Window</item>
</style>
<!-- Animations -->
<style name="MyAnimation" />
<!-- Animations for a non-full-screen window or activity. -->
<style name="MyAnimation.Window" parent="@android:style/Animation.Dialog">
<item name="android:windowEnterAnimation">@anim/grow_from_middle</item>
<item name="android:windowExitAnimation">@anim/shrink_to_middle</item>
</style>
Manifest.xml
清单文件
<activity
android:name="com.me.activity.MyActivity"
android:label="@string/display_name"
android:theme="@style/MyTheme">
</activity>
回答by Praveen
startActivity(intent);
overridePendingTransition(R.anim.slide_top_to_bottom, R.anim.hold);
Check this link: overridePendingTransition method
检查此链接:overridePendingTransition 方法
Edit:
编辑:
To Achieve the Animation for the Views. You have use the startAnimation Method like below
实现视图的动画。您已经使用了如下所示的 startAnimation 方法
view.startAnimation(AnimationUtils.loadAnimation(
WidgetActivity.this,R.anim.slide_top_to_bottom));
Check this link:
检查此链接:
回答by Blundell
It doesn't matter that your starting from a widget, wrote a tutorial so that you can animate your activity's in and out. This animation is set within the activity that your bringing into focus so you can do it with pendingIntent as well.
你从一个小部件开始,写了一个教程,这样你就可以动画你的活动的进出,这并不重要。此动画设置在您聚焦的活动中,因此您也可以使用 pendingIntent 来完成。
Enjoy:
享受: