0919Android基础动画
2015-09-21 08:43
615 查看
动画
动画分为逐帧动画,补间动画和属性动画。记录一下补间动画和属性动画。关于动画所需要知道的一些知识:上/article/1562103.html
中/article/1562102.html
下/article/1562101.html
补间动画
只能实现位移,放大缩小,旋转,渐变动作。在代码中写动画
@Override public void onClick(View v) { switch (v.getId()){ case R.id.button_scale_animation: // 放大缩小ScaleAnimation(float fromX, float toX, float fromY, float toY)默认从左上角开始放大缩小, // 注意其中的参数为放大缩小系数 ScaleAnimation animationScale=new ScaleAnimation(1,0.5f,1,0.5f); animationScale.setDuration(3000);//动画显示时间 mImg.startAnimation(animationScale); break; case R.id.button_rorate_animation: // 旋转RotateAnimation(float fromDegrees, float toDegrees)默认旋转中心为左上角 RotateAnimation animationRotate=new RotateAnimation(0,360); animationRotate.setDuration(3000);//动画显示时间 mImg.startAnimation(animationRotate); break; case R.id.button_alpha_animation: // 设置透明度AlphaAnimation(float fromAlpha, float toAlpha) AlphaAnimation animationAlpha=new AlphaAnimation(0.0f,1.0f); animationAlpha.setDuration(3000);//动画显示时间 mImg.startAnimation(animationAlpha); break; case R.id.button_trans_animation: // 位移TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) TranslateAnimation animationTrans = new TranslateAnimation(-mImg.getMeasuredWidth(),0,-mImg.getMeasuredHeight(),0); animationTrans.setDuration(3000);//动画显示时间 mImg.startAnimation(animationTrans); break; case R.id.button_set_animation: // AnimationSet (boolean shareInterpolator) // 传递true在这个组中的动画应该使用与此AnimationSet相关的插补器。传递false如果每个动画应该使用自己的内插器。 AnimationSet animationSet=new AnimationSet(false); ScaleAnimation animation1=new ScaleAnimation(1,0.5f,1,0.5f); RotateAnimation animation2=new RotateAnimation(0,360); AlphaAnimation animation3=new AlphaAnimation(0.0f,1.0f); TranslateAnimation animation4 = new TranslateAnimation(0,mImg.getMeasuredWidth(),0,-mImg.getMeasuredHeight()); animation1.setDuration(3000); animation2.setDuration(3000); animation3.setDuration(3000); animation4.setDuration(3000); animationSet.addAnimation(animation1); animationSet.addAnimation(animation2); animationSet.addAnimation(animation3); animationSet.addAnimation(animation4); mImg.startAnimation(animationSet); break; default: break; } }
XML文件中写动画
在res文件夹下新建一个目录,选择Anim类型,在其中新建一个文件interpolator是指动画变化方式,默认是linear_interpolator
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/cycle_interpolator">
旋转
以中心点旋转设置pivotX=”50%”,pivotY=”50%”,repeatCount是指重复次数这里虽然设置5
但是实际上旋转6次
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:duration="3000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="5" android:toDegrees="360" /> </set>
然后在活动中掉用运行就可。
Animation animationRotate= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animation_rorate); mImg.startAnimation(animationRotate);
放大缩小
startOffSet是设置多少秒以后开始动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/cycle_interpolator">
<scale android:fromXScale="1"
android:fromYScale="1"
android:toXScale="0.5f"
android:toYScale="0.5f"
android:duration="2000"
android:startOffset="1000"
android:pivotY="50%"
android:pivotX="50%"/>
</set>
然后在活动中掉用运行就可。
Animation animationScale=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animation_scale); mImg.startAnimation(animationScale);
其他的动作也可以这样写
属性动画ValueAnimator
参考鸿洋博客/article/1580279.html进阶http://www.it165.net/pro/html/201505/41670.html
基本用法
代码中直接实现public void startAnimator(View view){ // 不仅可以用于动画,还可以用于位置变化 // rotationX rotationY (0F-360F)alpha scaleX scaleY(0F-1F) translationX translationY translationZ ObjectAnimator.ofFloat(mImg,"scaleX",0.0f,1f).setDuration(2000).start(); }
意思是点击mImg所指的图片时,在scaleX属性上从0.0f变化到1f,动画两秒注意在图片上也要加上onClick属性
android:onClick="startAnimator"
通过XML实现
在ImageView的XML属性中添加
android:onClick="startAnimator"
首先现在res文件下创建目录,选择Animator类型,自动生成目录名。然后在其中添加一个文件
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:duration="2000" android:propertyName="scaleX" android:valueFrom="1f" android:valueTo="0.5f"></objectAnimator> <objectAnimator android:duration="2000" android:propertyName="scaleY" android:valueFrom="1f" android:valueTo="0.5f"></objectAnimator> </set>
然后在活动中调用
public void startAnimator(View view){ // 不仅可以用于动画,还可以用于位置变化 // rorateX rorateY alpha scaleX scaleY translationX translationY translationZ // ObjectAnimator.ofFloat(mImg,"scaleX",0.0f,1f).setDuration(2000).start(); Animator animator=AnimatorInflater.loadAnimator(getApplicationContext(),R.animator.animator_scale); animator.setTarget(mImg); animator.start(); }
布局动画Layout Animation(属于属性动画)
代码中添加View时的动画首先现在res文件下创建目录,选择Animator类型,自动生成目录名。然后在其中添加一个文件
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="together"> <objectAnimator android:duration="1000" android:propertyName="scaleX" android:valueFrom="0.5" android:valueTo="1"></objectAnimator> <objectAnimator android:duration="1000" android:propertyName="scaleY" android:valueFrom="0.5" android:valueTo="1"></objectAnimator> </set>
设置布局
<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" tools:context=".MainActivity"> <Button android:id="@+id/button_add_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加按钮" /> <LinearLayout android:id="@+id/linearlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> </LinearLayout> 在活动中添加View(这里是在LinearLayout中添加button) import android.animation.AnimatorInflater; import android.animation.LayoutTransition; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; public class MainActivity extends Activity { private Button mButton; private LinearLayout mLinearLayout; private int count; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton= (Button) findViewById(R.id.button_add_button); mLinearLayout= (LinearLayout) findViewById(R.id.linearlayout); LayoutTransition transition=new LayoutTransition(); transition.getDuration(2000); // View出现时自身的动画,使用自定义animator_scale transition.setAnimator(LayoutTransition.APPEARING, AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.animator_scale)); // View出现时其余View的动画,使用默认值 transition.setAnimator(LayoutTransition.CHANGE_APPEARING,transition.getAnimator(LayoutTransition.CHANGE_APPEARING)); // View消失时自身的动画,使用默认值 transition.setAnimator(LayoutTransition.DISAPPEARING, transition.getAnimator(LayoutTransition.DISAPPEARING)); // View消失时其他View的动画,使用默认值 transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,transition.getAnimator(LayoutTransition.CHANGE_DISAPPEARING)); // 将这些动画加到LinearLayout上面 mLinearLayout.setLayoutTransition(transition); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { count++; Button btn=new Button(MainActivity.this); // 设置添加的View的宽高 ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); btn.setLayoutParams(params); btn.setText("按钮" + count); // 设置初始的放大倍数 btn.setScaleX(0f); btn.setScaleY(0f); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 移除LinearLayout中点击到的View mLinearLayout.removeView(v); } }); mLinearLayout.addView(btn); } }); } }
相关文章推荐
- android项目epub格式电子书开源开发
- Android异步任务AsyncTask
- android传感器;摇抽奖功能
- Android开课20天——简单的学生管理系统
- Android:View中的performClick()触发条件
- android常见错误(实时更新)
- Android 动画——Layout Animations布局动画
- android给未签名的apk签名
- Android:Activity生命周期
- android Spinner控件详解
- Android:Activity & Intent
- Android中GridView使用
- 【Android】本地图片选择(打开媒体库,选择图片)
- Android中Context详解 ---- 你所不知道的Context
- listview增加footer时图片闪动问题
- [OK3354D--System]Android4.2系统安装
- 6 款好用的 PC+Android 同步 GTD 软件
- 6 款好用的 PC+Android 同步 GTD 软件
- 如何在Android开发中测试应用在真机上实验
- android图片的上传、下载和一些缩放操作