大家都应该知道,当我们在应用中没有动画效果的话,那么你的应用做的真的会没有一个人玩,这个我都不用多说什么,大家就应该明白了,当我们的应用有了很炫的动画时,你这个就是一个比较不错的应用了,因为大多数的人还是比较爱看动画的,那么我们今天就教大家怎么样才能实现动画效果,废话不多说,来看看代码吧:
1.在图片显示过程中使用动画效果,可以给人一种感觉。比如渐进渐出的效果。
Java代码:
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
复制代码 android.R.anim.fade_in, android.R.anim.fade_out两种动画效果是系统自带的效果。
2.下面介绍自定义的动画效果。
Java代码:
// 实现动画效果
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
findViewById(R.id.pw).startAnimation(shake);
复制代码 里面用到的shake.xml文件,存放在anim目录下面。代码如下:
Java代码:<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="10"
android:duration="1000"
android:interpolator="@anim/cycle_7" />
复制代码 而里面的cycle_7.xml,代码如下所示:
Java代码:
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="7"
/>
复制代码 下面介绍APIDEMO中的动画效果。
第一种ViewFlipper中各背景图片的切换效果。
Java代码:
public class Animation2 extends Activity implements
AdapterView.OnItemSelectedListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation_2);
mFlipper = ((ViewFlipper) this.findViewById(R.id.flipper));
mFlipper.startFlipping();
Spinner s = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, mStrings);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
s.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
switch (position) {
case 0:
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_up_in));
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_up_out));
break;
case 1:
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
break;
case 2:
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
break;
default:
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.hyperspace_in));
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.hyperspace_out));
break;
}
}
public void onNothingSelected(AdapterView parent) {
}
private String[] mStrings = { "Push up", "Push left", "Cross fade", "Hyperspace"};
private ViewFlipper mFlipper;
}
复制代码
animation_2.xml文件如下:
Java代码:
<ViewFlipper
android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:flipInterval="2000"
android:layout_marginBottom="20dip" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="26sp"
android:text="@string/animation_2_text_1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="26sp"
android:text="@string/animation_2_text_2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="26sp"
android:text="@string/animation_2_text_3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="26sp"
android:text="@string/animation_2_text_4"/>
</ViewFlipper>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:text="@string/animation_2_instructions"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
分享到:
相关推荐
Android翻页动画效果完整实例代码,直接运行,内附手冢治虫《火之鸟 黎明篇》片段。代码聚焦于贝塞尔曲线的使用,注释详尽,纯干货,供参考。
Android 动画类实例,基本涵盖Android所有动画类
在Android中实现animation动画效果的实例代码,代码里没注释,遗憾啊,自己摸索吧。 运行环境:Java/Eclipse
Android translate动画、scale动画、alpha动画、rotate切换动画和Activity动画效果实例,包括了切换Activity时淡出淡入,渐隐渐现效果,还有控件位置调整实例源码,注:动画切换Activity只有在新启动Activity才有效...
Android动画工程实例,Demo,该实例能帮助Andorid研发人员快速熟悉Andorid动画的基本开发
Android自动解锁实例 Android自动解锁实例 Android自动解锁实例
Android ViewPager实现仿QQ多页面滑动切换以及动画效果
Android动画实例.欢迎大家下载学习.
实现了android中帧动画、补间动画、属性动画的各种方式
本文实例讲述了Android编程实现RotateAnimation设置中心点旋转动画效果。分享给大家供大家参考,具体如下: 在xml设置: <?xml version=1.0 encoding=utf-8?> <rotate xmlns:android=...
Android 平台提供了一套完整的动画框架,使得开发者可以用它来开发各种动画效果。Android 动画框架详解由原理篇和实例篇两部分组成。...第二部分实例篇将在原理篇的基础上,向您展示一个动画实例的实现
本代码为 TextView 祥云滚动动画效果图,可以将祥云换成其他图案,拿来就用,非常实用。
这是一个Android的补间动画小实例 ,大家可以进行参考
Android使用TweenAnimation生成动画效果的例程,生成补间动画的例子,是一个比较基础简单的Android动画特效实例。
渐变透明度动画效果、渐变尺寸伸缩动画效果、画面转换位置移动动画效果、画面转移旋转动画效果
android 本身是不支持gif动画显示的,现已整理一个显示gif动画的实例,有需要的可以下载
Android 动画实例。包括ValueAnimator和PropertyValuesHolder的使用
本例是基于lufylegend.js Html5游戏动画的实例,放到手机上运行超级绚。导入eclipse直接运行即可!
实例075: 用图片为背景实现动画效果 242 实例076: 在手机屏幕中实现Frame动画 效果 244 实例077: 对图片进行缩放处理 246 实例078: 旋转屏幕中的图片 250 实例079: 实现旗帜飞扬的效果 253 实例080: 模拟构建...