Android 动画简单实现
2017-05-03 22:47
232 查看
Andriod 动画
安卓动画主要有三种:帧动画、补间动画以及属性动画。
一、帧动画
是将做好的图片通过一定的排列顺序进行一张一张的进行快速播放,就好像以前放电影时一张张的图片快速播放以达到运动的效果。
开发步骤:
1、 在drawable文件夹下创建标签是animtion_list文件。
2、 把准备好的图片放进项目res/drawable文件夹下。
3、 在ImageView种加载animtion_list里的图片。
4、 在Activity中开始动画。
XML:
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="300dp"
android:layout_height="200dp"
android:background="@drawable/animtion_list"
/>
</LinearLayout>
Activity:
二、补间动画
补间动画主要有四个重要的属性:透明、旋转、平移、缩放。
所有动画都具有的一些属性:
setDuration(2000):动画动作的时间。
setFillAfter(true/false):动画动作完成后是否保持当前状态。
setRepeatCount(1):动画重复动作的次数
setRepeatMode(Animation.REVERSE(反向)/Animation.RESTART(重新开始)):动画重复动作的模式
透明(代码实现):
代码实现:在XML中监听控件,然后在方法中实现透明的相应代码。
XML
Activity实现透明:
旋转(代码实现):
XML:
Activity:
平移(通过XML实现):
XML实现步骤:
1、 res文件夹下面创建一个anim文件夹
2、 在anim文件夹下创建对应的动画标签文件
3、 在activity中通过AnimationUtils.loadAnimation()
4、 控件调startAnimation()
XML中填写属性:
5、 Activity中通过AnimationUtils.loadAnimation()调用
平移(方法同上):
XML:
Activity:
安卓动画主要有三种:帧动画、补间动画以及属性动画。
一、帧动画
是将做好的图片通过一定的排列顺序进行一张一张的进行快速播放,就好像以前放电影时一张张的图片快速播放以达到运动的效果。
开发步骤:
1、 在drawable文件夹下创建标签是animtion_list文件。
2、 把准备好的图片放进项目res/drawable文件夹下。
3、 在ImageView种加载animtion_list里的图片。
4、 在Activity中开始动画。
XML:
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="300dp"
android:layout_height="200dp"
android:background="@drawable/animtion_list"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/g1" android:duration="100"> </item> <item android:drawable="@drawable/g2" android:duration="100"> </item> <item android:drawable="@drawable/g3" android:duration="100"> </item> <item android:drawable="@drawable/g4" android:duration="100"> </item> <item android:drawable="@drawable/g5" android:duration="100"> </item> <item android:drawable="@drawable/g6" android:duration="100"> </item> <item android:drawable="@drawable/g7" android:duration="100"> </item> <item android:drawable="@drawable/g8" android:duration="100"> </item> <item android:drawable="@drawable/g9" android b35e :duration="100"> </item> <item android:drawable="@drawable/g10" android:duration="100"> </item> <item android:drawable="@drawable/g11" android:duration="100"> </item> </animation-list>
Activity:
public class Frame_Activity extends Activity{ ImageView img; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.frame_activity); img = (ImageView) findViewById(R.id.img); AnimationDrawable ad = (AnimationDrawable) img.getBackground(); ad.start(); }
二、补间动画
补间动画主要有四个重要的属性:透明、旋转、平移、缩放。
所有动画都具有的一些属性:
setDuration(2000):动画动作的时间。
setFillAfter(true/false):动画动作完成后是否保持当前状态。
setRepeatCount(1):动画重复动作的次数
setRepeatMode(Animation.REVERSE(反向)/Animation.RESTART(重新开始)):动画重复动作的模式
透明(代码实现):
代码实现:在XML中监听控件,然后在方法中实现透明的相应代码。
XML
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="alpha" android:text="透明"/>
Activity实现透明:
/** * 透明 * */ public void alpha(View view){ AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f); alphaAnimation.setDuration(5000); alphaAnimation.setFillAfter(true); img_tween.startAnimation(alphaAnimation); }
旋转(代码实现):
XML:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="rotate" android:text="旋转"/>
Activity:
/** *旋转 * */ public void rotate(View view){ RotateAnimation ra = new RotateAnimation(0.0f,-180.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); ra.setDuration(2000); ra.setFillAfter(true); img_tween.startAnimation(ra); }
平移(通过XML实现):
XML实现步骤:
1、 res文件夹下面创建一个anim文件夹
2、 在anim文件夹下创建对应的动画标签文件
3、 在activity中通过AnimationUtils.loadAnimation()
4、 控件调startAnimation()
XML中填写属性:
<?xml version="1.0" encoding="utf-8"?> <scale android:fromXScale="1" android:toXScale="2" android:fromYScale="1" android:toYScale="2" android:pivotX="50%" android:pivotY="50%" android:duration ="2000" xmlns:android="http://schemas.android.com/apk/res/android"> </scale>
5、 Activity中通过AnimationUtils.loadAnimation()调用
/** * 缩放 * */ public void scale(View view ){ Animation sa = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.scale); img_tween.startAnimation(sa); }
平移(方法同上):
XML:
<?xml version="1.0" encoding="utf-8"?> <translate android:fromXDelta="0" android:toXDelta="-80" android:fromYDelta="0" android:toYDelta="-80" android:duration="2000" android:fillAfter="true" xmlns:android="http://schemas.android.com/apk/res/android"> </translate>
Activity:
/** * 平移 * */ public void translate(View view){ Animation ta = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.translate); img_tween.startAnimation(ta); }
相关文章推荐
- 实现Android简单动画旋转案例
- android 简单图片动画播放实现
- Android超简单实现listview上下拉伸回弹动画效果
- android 手摸手教你用 Canvas 实现简单粒子动画
- Android简单用属性动画实现两个控件重叠
- 实现Android简单动画旋转案例
- Android成长之路-实现简单动画
- android,侧滑栏SlidingLayout、ViewPager实现画廊、简单用viewpager实现画廊、圆形图片、简单的跑马灯动画效果、可拖拽的弹簧式水泡动画集合
- Android动画效果Animation之Tween实现简单动画
- Android学习之Animations实现几种简单动画
- Android Graphics 学习(1)----播放gif动画简单的实现
- Android动画效果Animation之Tween实现简单动画
- 纯css3无js实现的Android Logo(有简单动画)
- Android基础-简单的动画实现 *补间动画 *透明度渐变 *旋转动画 *位移动画 *如果需要几个效果整合到一起,需要将所有效果全部放到set里
- Android成长之路-实现简单动画
- Android中一个视图弹出动画的简单实现
- 实现Android简单动画旋转案例
- Mono For Android中简单实现按钮的动画效果
- Android用两张图片实现简单动画效果
- Android 实现简单的帧动画