自定义控件(32)---【转载】Animation 动画(三)ObjectAnimator、valueAnimator

来源:互联网 发布:淘宝 导航 css居中 编辑:程序博客网 时间:2024/05/16 00:51

Android自定义控件三部曲文章索引

转自 启舰


package com.example.animation;import android.animation.Animator;import android.animation.Animator.AnimatorListener;import android.animation.AnimatorListenerAdapter;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.animation.PropertyValuesHolder;import android.animation.ValueAnimator;import android.animation.ValueAnimator.AnimatorUpdateListener;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {private Button button;private ImageView imageView;private Button button1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);button1 = (Button) findViewById(R.id.button1);imageView = (ImageView) findViewById(R.id.imageView);/** * 操作ImageView */button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {/** * 三个动画同时进行,方法一 */ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f).setDuration(2000).start();ObjectAnimator.ofFloat(imageView, "translationY", 0f, 200f).setDuration(2000).start();ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f).setDuration(2000).start();/** * 三个动画同时进行,方法二 */PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation", 0f, 360f);PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationX", 0f, 200f);PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationY", 0f, 200f);ObjectAnimator.ofPropertyValuesHolder(imageView, p1, p2, p3).setDuration(2000).start();/** * 三个动画同时进行,方法三 */ObjectAnimator ofFloat1 = ObjectAnimator.ofFloat(imageView,"translationX", 0f, 200f);ObjectAnimator ofFloat2 = ObjectAnimator.ofFloat(imageView,"translationY", 0f, 200f);ObjectAnimator ofFloat3 = ObjectAnimator.ofFloat(imageView,"rotation", 0f, 360f);AnimatorSet set = new AnimatorSet();set.playTogether(ofFloat1, ofFloat2, ofFloat3);set.setDuration(2000);set.start();/** * 三个动画同时进行,方法四 */ObjectAnimator anim = ObjectAnimator//.ofFloat(imageView, "zhy", 1.0F, 0.0F)//.setDuration(500);//anim.start();anim.addUpdateListener(new AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {float cVal = (Float) animation.getAnimatedValue();imageView.setAlpha(cVal);imageView.setScaleX(cVal);imageView.setScaleY(cVal);}});/** * 三个动画依次、依次进行 */ObjectAnimator ofFloat1 = ObjectAnimator.ofFloat(imageView,"translationX", 0f, 200f);ObjectAnimator ofFloat2 = ObjectAnimator.ofFloat(imageView,"translationY", 0f, 200f);ObjectAnimator ofFloat3 = ObjectAnimator.ofFloat(imageView,"rotation", 0f, 360f);AnimatorSet set = new AnimatorSet();set.playSequentially(ofFloat1, ofFloat2, ofFloat3);set.setDuration(2000);set.start();/** * 动画一和三一起执行,然后执行动画二 */ObjectAnimator ofFloat1 = ObjectAnimator.ofFloat(imageView,"translationX", 0f, 200f);ObjectAnimator ofFloat2 = ObjectAnimator.ofFloat(imageView,"translationY", 0f, 200f);ObjectAnimator ofFloat3 = ObjectAnimator.ofFloat(imageView,"rotation", 0f, 360f);AnimatorSet set = new AnimatorSet();set.play(ofFloat1).with(ofFloat3);set.play(ofFloat2).after(ofFloat3);set.setDuration(2000);set.start();}});button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {ObjectAnimator animator = ObjectAnimator.ofFloat(button1,"alpha", 0f, 1f);animator.setDuration(1000);// 添加监听方法一animator.addListener(new AnimatorListener() {@Overridepublic void onAnimationStart(Animator animation) {}@Overridepublic void onAnimationEnd(Animator animation) {// 用的最多Toast.makeText(MainActivity.this, "end anim", 0).show();}@Overridepublic void onAnimationCancel(Animator animation) {}@Overridepublic void onAnimationRepeat(Animator animation) {}});// 添加监听方法二animator.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {super.onAnimationEnd(animation);Toast.makeText(MainActivity.this, "end anim", 0).show();}});animator.start();}});}}



package com.example.zhy_property_animation;import android.animation.Animator;import android.animation.Animator.AnimatorListener;import android.animation.AnimatorListenerAdapter;import android.animation.ObjectAnimator;import android.animation.TypeEvaluator;import android.animation.ValueAnimator;import android.animation.ValueAnimator.AnimatorUpdateListener;import android.app.Activity;import android.graphics.PointF;import android.os.Bundle;import android.util.DisplayMetrics;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.animation.LinearInterpolator;import android.widget.Button;import android.widget.ImageView;public class ValueAnimatorActivity extends Activity {protected static final String TAG = "MainActivity";private ImageView mBlueBall;private float mScreenHeight;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);mBlueBall = (ImageView) findViewById(R.id.id_ball);DisplayMetrics outMetrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(outMetrics);mScreenHeight = outMetrics.heightPixels;}/** * 自由落体 *  * @param view */public void verticalRun(View view) {ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight- mBlueBall.getHeight());animator.setTarget(mBlueBall);animator.setDuration(1000).start();animator.addUpdateListener(new AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {mBlueBall.setTranslationY((Float) animation.getAnimatedValue());}});}/** * 抛物线 *  * @param view */public void paowuxian(View view) {ValueAnimator valueAnimator = new ValueAnimator();valueAnimator.setDuration(3000);valueAnimator.setObjectValues(new PointF(0, 0));valueAnimator.setInterpolator(new LinearInterpolator());valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {// fraction = t / duration@Overridepublic PointF evaluate(float fraction, PointF startValue,PointF endValue) {Log.e(TAG, "fraction-->>" + fraction);// x方向200px/s ,则y方向0.5 * g * t (g = 100px / s*s)PointF point = new PointF();point.x = 200 * fraction * 3;point.y = 0.5f * 100 * (fraction * 3) * (fraction * 3);return point;}});valueAnimator.start();valueAnimator.addUpdateListener(new AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {PointF point = (PointF) animation.getAnimatedValue();mBlueBall.setX(point.x);mBlueBall.setY(point.y);}});}/** * 淡出且删除 *  * @param view */public void fadeOut(View view) {ObjectAnimator anim = ObjectAnimator.ofFloat(mBlueBall, "alpha", 0.5f);anim.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {Log.e(TAG, "onAnimationEnd");ViewGroup parent = (ViewGroup) mBlueBall.getParent();if (parent != null)parent.removeView(mBlueBall);}});anim.addListener(new AnimatorListener() {@Overridepublic void onAnimationStart(Animator animation) {Log.e(TAG, "onAnimationStart");}@Overridepublic void onAnimationRepeat(Animator animation) {// TODO Auto-generated method stubLog.e(TAG, "onAnimationRepeat");}@Overridepublic void onAnimationEnd(Animator animation) {Log.e(TAG, "onAnimationEnd");ViewGroup parent = (ViewGroup) mBlueBall.getParent();if (parent != null)parent.removeView(mBlueBall);}@Overridepublic void onAnimationCancel(Animator animation) {// TODO Auto-generated method stubLog.e(TAG, "onAnimationCancel");}});anim.start();}public void onClick(View view) {}}


package com.example.BlogObjectAnimator1;public class Point {    private int mRadius;    public Point(int radius){        mRadius = radius;    }    public int getRadius() {        return mRadius;    }    public void setRadius(int radius) {        mRadius = radius;    }}

package com.example.BlogObjectAnimator1;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;public class MyPointView extends View {    private Point mPoint = new Point(100);    public MyPointView(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onDraw(Canvas canvas) {        if (mPoint != null){            Paint paint = new Paint();            paint.setAntiAlias(true);            paint.setColor(Color.RED);            paint.setStyle(Paint.Style.FILL);            canvas.drawCircle(300,300,mPoint.getRadius(),paint);        }        super.onDraw(canvas);    }    public int getPointRadius(){        return 50;    }    public void setPointRadius(int radius){        mPoint.setRadius(radius);        invalidate();    }}

package com.example.BlogObjectAnimator1;import android.animation.ObjectAnimator;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MyActivity extends Activity {    private Button btnStart;    private MyPointView mPointView;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        btnStart = (Button) findViewById(R.id.btn);        mPointView = (MyPointView) findViewById(R.id.pointview);        btnStart.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                /**                 * 二、1 自定义ObjectAnimator属性                 */                doPointViewAnimation();            }        });    }    /**     * 二、1 自定义ObjectAnimator属性     */    private void doPointViewAnimation() {        ObjectAnimator animator = ObjectAnimator.ofInt(mPointView, "pointRadius", 0, 300, 100);        animator.setDuration(2000);        animator.start();    }}


  • 如果去掉第0帧,将以第一个关键帧为起始位置
  • 如果去掉结束帧,将以最后一个关键帧为结束位置
  • 使用Keyframe来构建动画,至少要有两个或两个以上帧


package com.harvic.BlogKeyframe;import android.animation.*;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.animation.BounceInterpolator;import android.view.animation.LinearInterpolator;import android.widget.Button;import android.widget.ImageView;public class MyActivity extends Activity {private ImageView mImage;private Button mBtn;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mImage = (ImageView) findViewById(R.id.img);mBtn = (Button) findViewById(R.id.btn);mBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {doBellRingAnim();}});}/** * 二.6 开篇的电话响铃效果 */private void doBellRingAnim() {/** * 左右震动效果 */Keyframe frame0 = Keyframe.ofFloat(0f, 0);Keyframe frame1 = Keyframe.ofFloat(0.1f, -20f);Keyframe frame2 = Keyframe.ofFloat(0.2f, 20f);Keyframe frame3 = Keyframe.ofFloat(0.3f, -20f);Keyframe frame4 = Keyframe.ofFloat(0.4f, 20f);Keyframe frame5 = Keyframe.ofFloat(0.5f, -20f);Keyframe frame6 = Keyframe.ofFloat(0.6f, 20f);Keyframe frame7 = Keyframe.ofFloat(0.7f, -20f);Keyframe frame8 = Keyframe.ofFloat(0.8f, 20f);Keyframe frame9 = Keyframe.ofFloat(0.9f, -20f);Keyframe frame10 = Keyframe.ofFloat(1, 0);PropertyValuesHolder frameHolder1 = PropertyValuesHolder.ofKeyframe("rotation", frame0, frame1, frame2, frame3, frame4, frame5,frame6, frame7, frame8, frame9, frame10);/** * scaleX放大1.1倍 */Keyframe scaleXframe0 = Keyframe.ofFloat(0f, 1);Keyframe scaleXframe1 = Keyframe.ofFloat(0.1f, 1.1f);Keyframe scaleXframe2 = Keyframe.ofFloat(0.2f, 1.1f);Keyframe scaleXframe3 = Keyframe.ofFloat(0.3f, 1.1f);Keyframe scaleXframe4 = Keyframe.ofFloat(0.4f, 1.1f);Keyframe scaleXframe5 = Keyframe.ofFloat(0.5f, 1.1f);Keyframe scaleXframe6 = Keyframe.ofFloat(0.6f, 1.1f);Keyframe scaleXframe7 = Keyframe.ofFloat(0.7f, 1.1f);Keyframe scaleXframe8 = Keyframe.ofFloat(0.8f, 1.1f);Keyframe scaleXframe9 = Keyframe.ofFloat(0.9f, 1.1f);Keyframe scaleXframe10 = Keyframe.ofFloat(1, 1);PropertyValuesHolder frameHolder2 = PropertyValuesHolder.ofKeyframe("ScaleX", scaleXframe0, scaleXframe1, scaleXframe2,scaleXframe3, scaleXframe4, scaleXframe5, scaleXframe6,scaleXframe7, scaleXframe8, scaleXframe9, scaleXframe10);/** * scaleY放大1.1倍 */Keyframe scaleYframe0 = Keyframe.ofFloat(0f, 1);Keyframe scaleYframe1 = Keyframe.ofFloat(0.1f, 1.1f);Keyframe scaleYframe2 = Keyframe.ofFloat(0.2f, 1.1f);Keyframe scaleYframe3 = Keyframe.ofFloat(0.3f, 1.1f);Keyframe scaleYframe4 = Keyframe.ofFloat(0.4f, 1.1f);Keyframe scaleYframe5 = Keyframe.ofFloat(0.5f, 1.1f);Keyframe scaleYframe6 = Keyframe.ofFloat(0.6f, 1.1f);Keyframe scaleYframe7 = Keyframe.ofFloat(0.7f, 1.1f);Keyframe scaleYframe8 = Keyframe.ofFloat(0.8f, 1.1f);Keyframe scaleYframe9 = Keyframe.ofFloat(0.9f, 1.1f);Keyframe scaleYframe10 = Keyframe.ofFloat(1, 1);PropertyValuesHolder frameHolder3 = PropertyValuesHolder.ofKeyframe("ScaleY", scaleYframe0, scaleYframe1, scaleYframe2,scaleYframe3, scaleYframe4, scaleYframe5, scaleYframe6,scaleYframe7, scaleYframe8, scaleYframe9, scaleYframe10);/** * 构建动画 */Animator animator = ObjectAnimator.ofPropertyValuesHolder(mImage,frameHolder1, frameHolder2, frameHolder3);animator.setDuration(1000);animator.start();}}


0 0
原创粉丝点击