属性动画Animator学习
来源:互联网 发布:fifaonline3古利特数据 编辑:程序博客网 时间:2024/06/10 03:18
资源文件
<LinearLayout android:id="@+id/ll_root" 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="位移动画" android:onClick="lineAnimator"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="缩放动画" android:onClick="ScaleAnimator"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="透明度动画" android:onClick="alphaAnimator"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋转动画" android:onClick="circleAnimator"/> <View android:layout_width="match_parent" android:layout_height="50dp"/> <ImageView android:id="@+id/pic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/pic" android:layout_gravity="center"/></LinearLayout>
public class MainActivity extends AppCompatActivity { LinearLayout ll_root; ImageView pic; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ll_root= (LinearLayout) findViewById(R.id.ll_root); pic= (ImageView) findViewById(R.id.pic); } private void changeLocation(View view,int x,int y){ int left=x; int top=y; int right=x+view.getWidth(); int bottom=view.getHeight()+y; view.layout(left,top,right,bottom); } /** * 位移动画 * @param view */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) public void lineAnimator(View view) { int width=ll_root.getWidth(); int height=(int)pic.getY(); ValueAnimator animator=ValueAnimator.ofInt(height ,height+pic.getHeight()); animator.setDuration(1000); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int y = (int) animation.getAnimatedValue(); int x = (int) pic.getX(); changeLocation(pic, x, y); } });// animator.setInterpolator(new LinearInterpolator()); animator.start(); } /** * 缩放动画 * @param view */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) public void ScaleAnimator(View view) { AnimatorSet animatorSet=new AnimatorSet(); ValueAnimator animatorsmall=ValueAnimator.ofFloat(1f,0); animatorsmall.setDuration(1000); animatorsmall.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float scale= (float) animation.getAnimatedValue(); pic.setScaleX(scale);// pic.setScaleY(scale); } }); ValueAnimator animatorlarge=ValueAnimator.ofFloat(0, 1.2f, 1.0f); animatorlarge.setDuration(1000); animatorlarge.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float scale = (float) animation.getAnimatedValue(); pic.setScaleX(scale);// pic.setScaleY(scale); } }); animatorSet.play(animatorlarge).after(animatorsmall); animatorSet.start(); } /** * 透明度动画 * @param view */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) public void alphaAnimator(View view) { ValueAnimator animator=ValueAnimator.ofFloat(1f,0.5f,1f); animator.setDuration(1000); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { pic.setAlpha((Float) animation.getAnimatedValue()); } }); animator.start(); } /** * 圆形旋转动画 * @param view */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) public void circleAnimator(View view) { ValueAnimator animator=ValueAnimator.ofFloat(0f, (float)(2* PI));//这边的转过的角度应使用弧度[0,2π] animator.setDuration(3000); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int x = (int) (100 * cos((float) animation.getAnimatedValue()) + 200); int y = (int) (100 * sin((float) animation.getAnimatedValue()) + 300); changeLocation(pic, x, y); } }); animator.setInterpolator(new LinearInterpolator());//匀速 animator.start(); } public void moveCircle(int x,int y,int cx,int cy,int r){ }}
android:interpolator 属性可以控制动画的变化速度:<!--动画开始时速度比较慢,在中间的时加速--><set android:interpolator="@android:anim/accelerate_decelerate_interpolator"><!--动画开始时速度比较慢,然后开始加速--><set android:interpolator="@android:anim/accelerate_interpolator"><!--动画循环播放特定的次数,以正弦曲线改变速度--><set android:interpolator="@android:anim/cycle_interpolator"><!--动画开始时速度比较慢,然后开始减速--><set android:interpolator="@android:anim/decelerate_interpolator"><!--均匀速度--><set android:interpolator="@android:anim/linear_interpolator">
0 0
- 属性动画Animator学习
- 属性动画Animator
- android属性动画animator
- Animator属性动画
- animator属性动画
- 【Property Animator 属性动画】
- Android属性动画(Animator)
- Animator --属性动画
- 属性动画Animator
- Animator --- 属性动画
- 属性动画学习(一)--Animator类+ValueAnimator类源码解析
- Unity动画 Animator的属性
- Android之属性动画Animator
- Android Animator属性动画总结
- Animator 属性动画简单使用
- 属性动画Animator工具类
- Animator参考(属性动画)
- Android之Animator属性动画
- JS 跳转到指定链接
- linux下QT初试
- Assembly x64 Intro - SSE2 4x4D Transpose
- Title_消息加解密(java版)
- OC学习篇之---Foundation框架中的其他类(NSNumber,NSDate,NSExcetion)
- 属性动画Animator学习
- Ming Rpc
- 性能评估
- ios nsstring 字符串包含引号 处理方法
- 23种设计模式C++实例之外观模式
- Agile Java自学笔记(一)
- asp.net mvc4 集成reactjs
- Scala:Set
- C++隐藏规则