Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.66 KB | None | 0 0
  1. PathMeasure pm = new PathMeasure(myPath, false);
  2.  
  3. float position = 0;
  4. float end = pm.getLength();
  5. float[] coord = {0,0,0,0,0,0,0,0,0};
  6.  
  7. while (position < end){
  8. Matrix m = new Matrix();
  9. // put the current path position coordinates into the matrix
  10. pm.getMatrix(position, m, PathMeasure.POSITION_MATRIX_FLAG | PathMeasure.TANGENT_MATRIX_FLAG);
  11. // put the matrix data into the coord array (coord[2] = x and coord[5] = y)
  12. m.getValues(coord);
  13. ????
  14. position += 1;
  15.  
  16. }
  17.  
  18. private float[] mIntervals = { 0f, 0f };
  19. private float drawSpeed = 2f;
  20. private int currentPath = -1;
  21. private PathMeasure mPathMeasure = new PathMeasure();
  22. private ArrayList<Path> mListPath = new ArrayList<Path>(this.pathCount);
  23.  
  24.  
  25. @Override
  26. protected void onDraw(Canvas canvas) {
  27. if (mIntervals[1] <= 0f && currentPath < (pathCount - 1)) {
  28. // Set the current path to draw
  29. // getPath(int num) a function to return a path.
  30. Path newPath = this.getPath(mListPath.size());
  31. this.mListPath.add(newPath);
  32. this.mPathMeasure.setPath(newPath, false);
  33. mIntervals[0] = 0;
  34. mIntervals[1] = this.mPathMeasure.getLength();
  35. }
  36.  
  37. if (mIntervals[1] > 0) {
  38. // draw the previous path
  39. int last = this.mListPath.size();
  40. for (int i = 0; i < last; i++) {
  41. canvas.drawPath(this.mListPath.get(i), mPaint);
  42. }
  43. // partially draw the last path
  44. this.mPaint.setPathEffect(new DashPathEffect(mIntervals, 0f));
  45.  
  46. canvas.drawPath(this.mListPath.get(last), mPaint);
  47.  
  48. // Update the path effects values, to draw a little more
  49. // on the path.
  50. mIntervals[0] += drawSpeed;
  51. mIntervals[1] -= drawSpeed;
  52.  
  53. super.invalidate();
  54. } else {
  55. // The drawing have been done, draw it entirely
  56. for (int i = 0; i < this.mListPath.size(); i++) {
  57. canvas.drawPath(this.mListPath.get(i), mPaint);
  58. }
  59. }
  60. }
  61.  
  62. import android.animation.ObjectAnimator;
  63. import android.content.Context;
  64. import android.graphics.Canvas;
  65. import android.graphics.Color;
  66. import android.graphics.DashPathEffect;
  67. import android.graphics.Paint;
  68. import android.graphics.Path;
  69. import android.graphics.PathEffect;
  70. import android.graphics.PathMeasure;
  71. import android.util.AttributeSet;
  72. import android.view.View;
  73. import android.util.Log;
  74.  
  75. public class PathView extends View
  76. {
  77. Path path;
  78. Paint paint;
  79. float length;
  80.  
  81. public PathView(Context context)
  82. {
  83. super(context);
  84. }
  85.  
  86. public PathView(Context context, AttributeSet attrs)
  87. {
  88. super(context, attrs);
  89. }
  90.  
  91. public PathView(Context context, AttributeSet attrs, int defStyleAttr)
  92. {
  93. super(context, attrs, defStyleAttr);
  94. }
  95.  
  96. public void init()
  97. {
  98. paint = new Paint();
  99. paint.setColor(Color.BLUE);
  100. paint.setStrokeWidth(10);
  101. paint.setStyle(Paint.Style.STROKE);
  102.  
  103. path = new Path();
  104. path.moveTo(50, 50);
  105. path.lineTo(50, 500);
  106. path.lineTo(200, 500);
  107. path.lineTo(200, 300);
  108. path.lineTo(350, 300);
  109.  
  110. // Measure the path
  111. PathMeasure measure = new PathMeasure(path, false);
  112. length = measure.getLength();
  113.  
  114. float[] intervals = new float[]{length, length};
  115.  
  116. ObjectAnimator animator = ObjectAnimator.ofFloat(PathView.this, "phase", 1.0f, 0.0f);
  117. animator.setDuration(3000);
  118. animator.start();
  119. }
  120.  
  121. //is called by animtor object
  122. public void setPhase(float phase)
  123. {
  124. Log.d("pathview","setPhase called with:" + String.valueOf(phase));
  125. paint.setPathEffect(createPathEffect(length, phase, 0.0f));
  126. invalidate();//will calll onDraw
  127. }
  128.  
  129. private static PathEffect createPathEffect(float pathLength, float phase, float offset)
  130. {
  131. return new DashPathEffect(new float[] { pathLength, pathLength },
  132. Math.max(phase * pathLength, offset));
  133. }
  134.  
  135. @Override
  136. public void onDraw(Canvas c)
  137. {
  138. super.onDraw(c);
  139. c.drawPath(path, paint);
  140. }
  141. }
  142.  
  143. PathView path_view = (PathView) root_view.findViewById(R.id.path);
  144. path_view.init();
  145.  
  146. package com.sample;
  147. /**
  148. * Created by Sumit
  149. */
  150. public class PathView extends View {
  151.  
  152. Paint mPaint;
  153. Path mPath;
  154. int mStrokeColor;
  155. float mStrokeWidth;
  156.  
  157. float mProgress = 0.0f;
  158. float mLength = 0f;
  159. float mTotal;
  160.  
  161. public PathView(Context context) {
  162. this(context, null);
  163. init();
  164. }
  165.  
  166. public PathView(Context context, AttributeSet attrs) {
  167. this(context, attrs, 0);
  168. init();
  169. }
  170.  
  171. public PathView(Context context, AttributeSet attrs, int defStyle) {
  172. super(context, attrs, defStyle);
  173.  
  174. mStrokeColor = Color.RED;
  175. mStrokeWidth = 8.0f;
  176. init();
  177. }
  178.  
  179. private void init() {
  180. mPaint = new Paint();
  181. mPaint.setColor(mStrokeColor);
  182. mPaint.setStyle(Paint.Style.STROKE);
  183. mPaint.setStrokeWidth(mStrokeWidth);
  184. mPaint.setStrokeCap(Paint.Cap.ROUND);
  185. mPaint.setAntiAlias(true);
  186. mPaint.setDither(true);
  187.  
  188. setPath(new Path());
  189. // setPath2(new Path());
  190. }
  191.  
  192. public void setPath(Path p) {
  193. mPath = p;
  194. PathMeasure measure = new PathMeasure(mPath, false);
  195. mPathLength = measure.getLength();
  196. }
  197.  
  198.  
  199. public void setPath(List<float[][]> list) {
  200. Log.d("Path", "size " + list.size());
  201. Path p = new Path();
  202. p.moveTo(list.get(0)[0][0], list.get(1)[0][1]);
  203.  
  204. for (int i = 1; i < list.size(); i++) {
  205. p.lineTo(list.get(i)[0][0], list.get(i)[0][1]);
  206. //if (i > 100)
  207. //p.moveTo(list.get(i)[0][0], list.get(i)[0][1]);
  208. }
  209. //p.setFillType(FillType.WINDING);
  210. setPath(p);
  211. }
  212.  
  213. @Override
  214. protected void onDraw(Canvas canvas) {
  215. super.onDraw(canvas);
  216. mTotal = (mLength - mLength * mProgress);
  217. PathEffect pathEffect = new DashPathEffect(new float[] { mLength, mLength }, mTotal);
  218. Log.d("Path Tag", "length =" + mLength + ", totla=" + mTotal);
  219.  
  220. mPaint.setPathEffect(pathEffect);
  221.  
  222. canvas.save();
  223. // canvas.translate(getPaddingLeft(), getPaddingTop());
  224. canvas.drawPath(mPath, mPaint);
  225. canvas.restore();
  226. }
  227.  
  228. @Override
  229. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  230. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  231. int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  232. int heightSize = MeasureSpec.getSize(heightMeasureSpec);
  233. int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  234. int heightMode = MeasureSpec.getMode(widthMeasureSpec);
  235.  
  236. int measuredWidth, measuredHeight;
  237.  
  238. if (widthMode == MeasureSpec.AT_MOST)
  239. throw new IllegalStateException("Use MATCH_PARENT");
  240. else
  241. measuredWidth = widthSize;
  242.  
  243. if (heightMode == MeasureSpec.AT_MOST)
  244. throw new IllegalStateException("Use MATCH_PARENT");
  245. else
  246. measuredHeight = heightSize;
  247.  
  248. setMeasuredDimension(measuredWidth, measuredHeight);
  249. setPath();
  250. }
  251.  
  252. void setPath() {
  253. int cX = getWidth() / 2;
  254. int cY = getHeight() / 2;
  255. cY += 50;
  256. cX -= 50;
  257. List<float[][]> list = new ArrayList<float[][]>();
  258.  
  259. for (int i = 0; i < 50; i++) {
  260. list.add(new float[][] { { cX--, cY++ } });
  261. }
  262.  
  263. for (int i = 0; i < 100; i++) {
  264. list.add(new float[][] { { cX--, cY-- } });
  265. }
  266.  
  267. for (int i = 0; i < 100; i++) {
  268. list.add(new float[][] { { cX++, cY-- } });
  269. }
  270.  
  271. for (int i = 0; i < 200; i++) {
  272. list.add(new float[][] { { cX++, cY++ } });
  273. }
  274.  
  275. for (int i = 0; i < 100; i++) {
  276. list.add(new float[][] { { cX++, cY-- } });
  277. }
  278. for (int i = 0; i < 100; i++) {
  279. list.add(new float[][] { { cX--, cY-- } });
  280. }
  281.  
  282. for (int i = 0; i < 100; i++) {
  283. list.add(new float[][] { { cX--, cY++ } });
  284. }
  285.  
  286. setPath(list);
  287. }
  288.  
  289. final PathView pathView = (PathView) findViewById(R.id.path_view);
  290. pathView.setOnClickListener(new View.OnClickListener() {
  291. @Override
  292. public void onClick(View view) {
  293. ObjectAnimator anim = ObjectAnimator.ofFloat(view, "percentage", 0.0f, 1.0f);
  294. anim.setDuration(2000);
  295. anim.setInterpolator(new LinearInterpolator());
  296. anim.setRepeatCount(Animation.INFINITE);
  297. anim.start();
  298. }
  299. });
  300.  
  301. package com.careem.acma.widget;
  302.  
  303. import android.content.Context;
  304. import android.content.res.TypedArray;
  305. import android.graphics.drawable.ColorDrawable;
  306. import android.os.Handler;
  307. import android.support.v4.content.ContextCompat;
  308. import android.util.AttributeSet;
  309. import android.view.View;
  310. import android.view.animation.Animation;
  311. import android.view.animation.AnimationUtils;
  312. import android.widget.ImageView;
  313. import android.widget.RelativeLayout;
  314. import android.widget.TextView;
  315.  
  316. import com.careem.acma.R;
  317.  
  318.  
  319. public class AnimatorLineView extends RelativeLayout {
  320.  
  321. private View animatorLineView;
  322. private View simpleLineView;
  323. View animatorLine;
  324. private int colorBeforeAnimation;
  325. private int colorAfterAnimation;
  326. private int colorForErrorLine;
  327.  
  328. public AnimatorLineView(Context context) {
  329. super(context);
  330. init();
  331. startAnimation();
  332. }
  333.  
  334. public AnimatorLineView(Context context, AttributeSet attrs) {
  335. super(context, attrs);
  336. init();
  337. initAttributes(context, attrs);
  338. setColors();
  339. startAnimation();
  340. }
  341.  
  342. public AnimatorLineView(Context context, AttributeSet attrs, int defStyleAttr) {
  343. super(context, attrs, defStyleAttr);
  344. init();
  345. initAttributes(context, attrs);
  346. setColors();
  347. startAnimation();
  348. }
  349.  
  350.  
  351. private void setColors() {
  352. simpleLineView.setBackgroundColor(colorBeforeAnimation);
  353. animatorLine.setBackgroundColor(colorAfterAnimation);
  354. }
  355.  
  356. public void init() {
  357. animatorLineView = inflate(getContext(), R.layout.ainimator_line_view, this);
  358. animatorLine = findViewById(R.id.simple_line);
  359. simpleLineView = findViewById(R.id.animator_line);
  360.  
  361. }
  362.  
  363. public void setColor(int color) {
  364. animatorLine.setBackgroundColor(color);
  365. }
  366.  
  367. public void startAnimation() {
  368. animatorLine.setVisibility(View.VISIBLE);
  369. Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.enter_animation_underline);
  370. animatorLine.startAnimation(animation);
  371. }
  372.  
  373. public void showErrorLine(){
  374. animatorLine.setBackgroundColor(colorForErrorLine);
  375. animatorLine.setVisibility(View.VISIBLE);
  376. Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.enter_animation_underline);
  377. animatorLine.startAnimation(animation);
  378. }
  379.  
  380. public void hideErrorLine(){
  381. animatorLine.setBackgroundColor(colorAfterAnimation);
  382. animatorLine.setVisibility(View.VISIBLE);
  383. Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.enter_animation_underline);
  384. animatorLine.startAnimation(animation);
  385. }
  386.  
  387. private void initAttributes(Context context, AttributeSet attributeSet) {
  388. TypedArray attr = getTypedArray(context, attributeSet, R.styleable.ProgressButton);
  389. if (attr == null) {
  390. return;
  391. }
  392. try {
  393. colorBeforeAnimation = attr.getColor(R.styleable.AnimatorLineView_al_color_after_animation,ContextCompat.getColor(getContext(), R.color.animation_line_text_color));
  394. colorAfterAnimation = attr.getColor(R.styleable.ProgressButton_pb_text_color, ContextCompat.getColor(getContext(), R.color.black_color));
  395. colorForErrorLine = attr.getColor(R.styleable.ProgressButton_pb_text_color, ContextCompat.getColor(getContext(), R.color.error_msgs_text_color));
  396. } finally {
  397. attr.recycle();
  398. }
  399. }
  400.  
  401. protected TypedArray getTypedArray(Context context, AttributeSet attributeSet, int[] attr) {
  402. return context.obtainStyledAttributes(attributeSet, attr, 0, 0);
  403. }
  404.  
  405. public void resetColor(){
  406. animatorLine.setBackgroundColor(colorAfterAnimation);
  407. animatorLine.setVisibility(View.GONE);
  408. }
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement