Created
October 16, 2014 02:39
-
-
Save nightbear1009/26aaa77e64c509348f64 to your computer and use it in GitHub Desktop.
a note of DrawerArrowDrawable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.ted.myapplication; | |
import android.animation.ObjectAnimator; | |
import android.animation.ValueAnimator; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.ColorFilter; | |
import android.graphics.Matrix; | |
import android.graphics.Paint; | |
import android.graphics.Path; | |
import android.graphics.PathMeasure; | |
import android.graphics.Rect; | |
import android.graphics.drawable.Drawable; | |
import android.util.Property; | |
import android.view.animation.Interpolator; | |
import android.view.animation.LinearInterpolator; | |
import static android.graphics.Paint.Style.STROKE; | |
/** | |
* Created by ted on 2014/10/16. | |
*/ | |
public class CustomView extends Drawable { | |
Path first; | |
Path second; | |
Paint linePaint; | |
Paint curve; | |
Paint curve2; | |
float param = 0; | |
private ObjectAnimator mObjectAnimatorAngle; | |
Rect bounds; | |
Path three; | |
Path four; | |
public CustomView(){ | |
init(); | |
} | |
private void init() { | |
bounds = new Rect(0, 0, 1000,1000); | |
first = new Path(); | |
first.moveTo(5.042f, 20f); | |
first.rCubicTo(8.125f, -16.317f, 39.753f, -27.851f, 55.49f, -2.765f); | |
second = new Path(); | |
second.moveTo(60.531f, 17.235f); | |
second.rCubicTo(11.301f, 18.015f, -3.699f, 46.083f, -23.725f, 43.456f); | |
linePaint = new Paint(Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); | |
linePaint.setColor(Color.RED); | |
linePaint.setStyle(STROKE); | |
scalePath(first,0); | |
scalePath(second,0); | |
three = new Path(); | |
three.moveTo(64.959f, 20f); | |
three.rCubicTo(4.457f, 16.75f, 1.512f, 37.982f, -22.557f, 42.699f); | |
four = new Path(); | |
four.moveTo(42.402f, 62.699f); | |
four.cubicTo(18.333f, 67.418f, 8.807f, 45.646f, 8.807f, 32.823f); | |
scalePath(three,0); | |
scalePath(four,0); | |
curve = new Paint(Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); | |
curve.setColor(Color.BLUE); | |
curve2 = new Paint(Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); | |
curve2.setColor(Color.YELLOW); | |
setupAnimations(); | |
} | |
private static void scalePath(Path path, float density) { | |
Matrix scaleMatrix = new Matrix(); | |
scaleMatrix.setScale(2f,2f, 0f, 0f); | |
path.transform(scaleMatrix); | |
} | |
private Property<CustomView, Float> mAngleProperty = | |
new Property<CustomView, Float>(Float.class, "angle") { | |
@Override | |
public Float get(CustomView object) { | |
return object.getCurrentParam(); | |
} | |
@Override | |
public void set(CustomView object, Float value) { | |
object.setCurrentParam(value); | |
} | |
}; | |
public Float getCurrentParam(){ | |
return param; | |
} | |
public void setCurrentParam(Float param){ | |
this.param = param; | |
invalidateSelf(); | |
} | |
private static final Interpolator ANGLE_INTERPOLATOR = new LinearInterpolator(); | |
private static final int ANGLE_ANIMATOR_DURATION = 2000; | |
private void setupAnimations() { | |
mObjectAnimatorAngle = ObjectAnimator.ofFloat(this, mAngleProperty, 1f); | |
mObjectAnimatorAngle.setInterpolator(ANGLE_INTERPOLATOR); | |
mObjectAnimatorAngle.setDuration(ANGLE_ANIMATOR_DURATION); | |
mObjectAnimatorAngle.setRepeatMode(ValueAnimator.RESTART); | |
mObjectAnimatorAngle.setRepeatCount(ValueAnimator.INFINITE); | |
mObjectAnimatorAngle.start(); | |
} | |
@Override public int getIntrinsicHeight() { | |
return bounds.height(); | |
} | |
@Override public int getIntrinsicWidth() { | |
return bounds.width(); | |
} | |
@Override | |
public void draw(Canvas canvas) { | |
float coordsA[] = { 0f, 0f }; | |
float coordsB[] = { 0f, 0f }; | |
PathMeasure measureFirst = new PathMeasure(first, false); | |
PathMeasure measureFirst2 = new PathMeasure(second, false); | |
PathMeasure measureFirst3 = new PathMeasure(three, false); | |
PathMeasure measureFirst4 = new PathMeasure(four, false); | |
float lengthFirst = measureFirst.getLength(); | |
float lengthFirst2 = measureFirst2.getLength(); | |
float lengthFirst3 = measureFirst3.getLength(); | |
float lengthFirst4 = measureFirst4.getLength(); | |
if(param <=0.5f) { | |
param *= 2; | |
measureFirst.getPosTan(lengthFirst * param, coordsA, null); | |
measureFirst3.getPosTan(lengthFirst3 * param, coordsB, null); | |
}else { | |
param -= .5f; | |
param *= 2; | |
// measureFirst3.getPosTan(lengthFirst3 * param, coordsA, null); | |
measureFirst4.getPosTan(lengthFirst4 * param, coordsA, null); | |
measureFirst2.getPosTan(lengthFirst2 * param, coordsB, null); | |
} | |
canvas.drawPath(first,curve); | |
canvas.drawPath(second,curve2); | |
canvas.drawPath(three,curve); | |
canvas.drawPath(four,curve2); | |
canvas.drawLine(coordsA[0], coordsA[1], coordsB[0], coordsB[1], linePaint); | |
if(param==1){ | |
param = 0; | |
} | |
} | |
@Override | |
public void setAlpha(int alpha) { | |
} | |
@Override | |
public void setColorFilter(ColorFilter cf) { | |
} | |
@Override | |
public int getOpacity() { | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment