| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.transition; |
| 17 |
|
| 18 |
import android.content.Context; |
| 19 |
import android.content.res.TypedArray; |
| 20 |
import android.graphics.Matrix; |
| 21 |
import android.graphics.Path; |
| 22 |
import android.graphics.PathMeasure; |
| 23 |
import android.util.AttributeSet; |
| 24 |
import android.util.PathParser; |
| 25 |
|
| 26 |
import com.android.internal.R; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class PatternPathMotion extends PathMotion { |
| 40 |
|
| 41 |
private Path mOriginalPatternPath; |
| 42 |
|
| 43 |
private final Path mPatternPath = new Path(); |
| 44 |
|
| 45 |
private final Matrix mTempMatrix = new Matrix(); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public PatternPathMotion() { |
| 51 |
mPatternPath.lineTo(1, 0); |
| 52 |
mOriginalPatternPath = mPatternPath; |
| 53 |
} |
| 54 |
|
| 55 |
public PatternPathMotion(Context context, AttributeSet attrs) { |
| 56 |
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PatternPathMotion); |
| 57 |
try { |
| 58 |
String pathData = a.getString(R.styleable.PatternPathMotion_patternPathData); |
| 59 |
if (pathData == null) { |
| 60 |
throw new RuntimeException("pathData must be supplied for patternPathMotion"); |
| 61 |
} |
| 62 |
Path pattern = PathParser.createPathFromPathData(pathData); |
| 63 |
setPatternPath(pattern); |
| 64 |
} finally { |
| 65 |
a.recycle(); |
| 66 |
} |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public PatternPathMotion(Path patternPath) { |
| 79 |
setPatternPath(patternPath); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public Path getPatternPath() { |
| 91 |
return mOriginalPatternPath; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public void setPatternPath(Path patternPath) { |
| 103 |
PathMeasure pathMeasure = new PathMeasure(patternPath, false); |
| 104 |
float length = pathMeasure.getLength(); |
| 105 |
float[] pos = new float[2]; |
| 106 |
pathMeasure.getPosTan(length, pos, null); |
| 107 |
float endX = pos[0]; |
| 108 |
float endY = pos[1]; |
| 109 |
pathMeasure.getPosTan(0, pos, null); |
| 110 |
float startX = pos[0]; |
| 111 |
float startY = pos[1]; |
| 112 |
|
| 113 |
if (startX == endX && startY == endY) { |
| 114 |
throw new IllegalArgumentException("pattern must not end at the starting point"); |
| 115 |
} |
| 116 |
|
| 117 |
mTempMatrix.setTranslate(-startX, -startY); |
| 118 |
float dx = endX - startX; |
| 119 |
float dy = endY - startY; |
| 120 |
float distance = (float) Math.hypot(dx, dy); |
| 121 |
float scale = 1 / distance; |
| 122 |
mTempMatrix.postScale(scale, scale); |
| 123 |
double angle = Math.atan2(dy, dx); |
| 124 |
mTempMatrix.postRotate((float) Math.toDegrees(-angle)); |
| 125 |
patternPath.transform(mTempMatrix, mPatternPath); |
| 126 |
mOriginalPatternPath = patternPath; |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public Path getPath(float startX, float startY, float endX, float endY) { |
| 131 |
double dx = endX - startX; |
| 132 |
double dy = endY - startY; |
| 133 |
float length = (float) Math.hypot(dx, dy); |
| 134 |
double angle = Math.atan2(dy, dx); |
| 135 |
|
| 136 |
mTempMatrix.setScale(length, length); |
| 137 |
mTempMatrix.postRotate((float) Math.toDegrees(angle)); |
| 138 |
mTempMatrix.postTranslate(startX, startY); |
| 139 |
Path path = new Path(); |
| 140 |
mPatternPath.transform(mTempMatrix, path); |
| 141 |
return path; |
| 142 |
} |
| 143 |
|
| 144 |
} |