/*
* Created by Hanks
* Copyright (c) 2015 Hanks. All rights reserved
*
*/
package app.hanks.com.customanimateview.OpAnimateView;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Hanks on 2015/5/27.
*/
public class OpAnimationView extends View {
private Paint paint;
private int width;
private int height;
private int c;
private float progress = 0; //animation‘s progress
private boolean isAnimating = false; //is isAnimating?
private boolean isRgihtShape = false; //is in right_shap?
private int DURATION = 400; // animation DURATION
private int paintWidth = 0; // animation DURATION
public OpAnimationView(Context context) {
this(context, null);
}
public OpAnimationView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public OpAnimationView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public OpAnimationView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
/**
* init paint
*
* @param context
*/
private void init(Context context) {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(dp2px(6));
paint.setStrokeJoin(Paint.Join.ROUND);
}
private List<PointF> points = new ArrayList<>();
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
c = Math.min(w, h) / 2;
paintWidth = dp2px(6);
points.clear();
float g2 = (float) Math.sqrt(2);
points.add(new PointF(c, c / 3f));
points.add(new PointF(c, c * 5f / 3f));
points.add(new PointF(c / 3f, c));
points.add(new PointF(c * 5f / 3f, c));
points.add(new PointF(c * (1 + 3 * g2) / 3f, c * (3 - g2) / 3f));
points.add(new PointF(c * (1 + g2) / 3f + paintWidth/g2/2, c * (3 + g2) / 3f+paintWidth/g2/2));
points.add(new PointF(c * (1 + g2) / 3f - paintWidth/g2/2, c * (3 + g2) / 3f+paintWidth/g2/2));
}
@Override
protected void onDraw(Canvas canvas) {
if (isRgihtShape) { // right --> add
float x0 = (points.get(0).x - points.get(4).x) * progress + points.get(4).x;
float y0 = (points.get(0).y - points.get(4).y) * progress + points.get(4).y;
float x1 = (points.get(1).x - points.get(6).x) * progress + points.get(6).x;
float y1 = (points.get(1).y - points.get(6).y) * progress + points.get(6).y;
float x2 = points.get(2).x;
float y2 = points.get(2).y;
float x3 = (points.get(3).x - points.get(5).x) * progress + points.get(5).x;
float y3 = (points.get(3).y - points.get(5).y) * progress + points.get(5).y;
canvas.drawLine(x0, y0, x1, y1, paint);
canvas.drawLine(x2, y2, x3, y3, paint);
} else { //add --> right
float x0 = (points.get(4).x - points.get(0).x) * progress + points.get(0).x;
float y0 = (points.get(4).y - points.get(0).y) * progress + points.get(0).y;
float x1 = (points.get(6).x - points.get(1).x) * progress + points.get(1).x;
float y1 = (points.get(6).y - points.get(1).y) * progress + points.get(1).y;
float x2 = points.get(2).x;
float y2 = points.get(2).y;
float x3 = (points.get(5).x - points.get(3).x) * progress + points.get(3).x;
float y3 = (points.get(5).y - points.get(3).y) * progress + points.get(3).y;
canvas.drawLine(x0, y0, x1, y1, paint);
canvas.drawLine(x2, y2, x3, y3, paint);
}
}
public boolean isRightShape() {
return isRgihtShape;
}
/**
* add_shape to right_shape
*/
public void add2right() {
if (isAnimating) {
return;
}
progress = 0f;
isAnimating = true;
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
valueAnimator.setDuration(DURATION);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
progress = (float) animation.getAnimatedValue();
if (progress >= 1) {
isRgihtShape = true;
isAnimating = false;
} else {
invalidate();
}
}
});
valueAnimator.start();
}
/**
* right_shape to add_shape
*/
public void right2add() {
if (isAnimating) {
return;
}
progress = 0f;
isAnimating = true;
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
valueAnimator.setDuration(DURATION);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
progress = (float) animation.getAnimatedValue();
if (progress >= 1) {
isRgihtShape = false;
isAnimating = false;
} else {
invalidate();
}
}
});
valueAnimator.start();
}
/**
* dp×a px.
*
* @param value the value
* @return the int
*/
public int dp2px(float value) {
final float scale = getResources().getDisplayMetrics().densityDpi;
return (int) (value * (scale / 160) + 0.5f);
}
}
原文地址:http://blog.csdn.net/hpu_zyh/article/details/46040421