最近又开始学习动画了,隔一段时间就想着学习一下,对于动画绝对是真爱。
效果图:
path.gifpackage com.example.sunxiaokun.studypath.activity;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import com.example.sunxiaokun.studypath.R;
import com.example.sunxiaokun.studypath.myview.CircleEvaluator;
import com.example.sunxiaokun.studypath.myview.DrawCircleView;
import com.example.sunxiaokun.studypath.myview.StudyPathView;
public class StudyPaintActivity extends AppCompatActivity {
private FrameLayout mStudy_ll_parent;
private DrawCircleView mDrawCircleView;
private StudyPathView mStudyPathView;
private ValueAnimator mValueAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_study_paint);
mStudy_ll_parent = (FrameLayout) findViewById(R.id.study_ll_parent);
mStudyPathView = new StudyPathView(this);
mDrawCircleView = new DrawCircleView(this);
mStudy_ll_parent.addView(mStudyPathView);
mStudy_ll_parent.addView(mDrawCircleView);
Point fromPoint = new Point(0 , 0);
Point endPoint = new Point(200 , 0);
Point controllPoint = new Point(100, 100);
//二阶贝塞尔曲线
mValueAnimator = ValueAnimator.ofObject(new CircleEvaluator(controllPoint),fromPoint,endPoint);
mValueAnimator.setDuration(3000);
mValueAnimator.start();
mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Point point = (Point) animation.getAnimatedValue();
mDrawCircleView.setX(point.x);
mDrawCircleView.setY(point.y);
}
});
mValueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
modifyAnimator(mDrawCircleView, "translationY", 0,100,2);
}
});
}
/**
* 平移
* @param view
* @param xy
* @param from
* @param end
* @param count
*/
public void modifyAnimator(View view, String xy, int from , int end, final int count){
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, xy, from, end);
objectAnimator.setDuration(2000);
objectAnimator.start();
objectAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if(count == 3){
modifyAnimator(mDrawCircleView, "translationY", 100,0,4);
}else if(count == 2){
modifyAnimator(mDrawCircleView, "translationX", 200,0,3);
}else if(count == 4){
mValueAnimator.start();
}
}
});
}
}
package com.example.sunxiaokun.studypath.myview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by xxx on 2017/6/29.
*/
public class DrawCircleView extends View {
private Paint mPaint;
public DrawCircleView(Context context) {
this(context, null);
}
public DrawCircleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.BLUE);
mPaint.setStrokeWidth(2);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(100,100,3, mPaint);
}
}
package com.example.sunxiaokun.studypath.myview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by sunxiaokun on 2017/6/29.
*/
public class StudyPathView extends View {
private Paint mPaint;
private Path mPath;
public StudyPathView(Context context) {
this(context, null);
}
public StudyPathView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStrokeWidth(1);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.GRAY);
mPath = new Path();
mPath.moveTo(100,100);
mPath.quadTo(200, 200, 300,100);
mPath.lineTo(300,200);
mPath.lineTo(100,200);
mPath.close();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mPath, mPaint);
}
}
网友评论