Implement Running Dash Path, Using Pathdashpatheffect

Posted on

Example to implement running dash path, using PathDashPathEffect.

Modify from previous instance “DashPathEffect, use dash upshot on path“, implement PathDashPathEffect. In club to become far running, advance stage of PathDashPathEffect, together with call invalidate() in onDraw() method.

MyView.java

package com.example.androiddrawpath;  import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PathDashPathEffect; import android.graphics.Path.Direction; import android.util.AttributeSet; import android.view.View; import android.widget.TextView;  populace flat MyView extends View {    MyShape myShape;  float ratioRadius, ratioInnerRadius;  int numberOfPoint = 3; //default    float rotate;    //corresponding to UI chemical ingredient  TextView textLayerInfo;   Path dashPath;  float phase;   populace MyView(Context context) {   super(context);   initMyView();  }   populace MyView(Context context, AttributeSet attrs) {   super(context, attrs);   initMyView();  }   populace MyView(Context context, AttributeSet attrs, int defStyleAttr) {   super(context, attrs, defStyleAttr);   initMyView();  }    populace void initMyView(){   myShape = novel MyShape();      dashPath = novel Path();   dashPath.addCircle(0, 0, 3, Direction.CCW);      stage = 0.0f;  }    @Override  protected void onDraw(Canvas canvas) {   super.onDraw(canvas);      long starting = System.nanoTime();      int w = getWidth();   int h = getHeight();      if((w==0) || (h==0)){    return;   }      float x = (float)w/2.0f;   float y = (float)h/2.0f;   float radius, innerRadius;   if(w > h){    radius = h * ratioRadius;    innerRadius = h * ratioInnerRadius;   }else{    radius = w * ratioRadius;    innerRadius = w * ratioInnerRadius;   }      myShape.setStar(x, y, radius, innerRadius, numberOfPoint);      //Save together with rotate sheet    canvas.save();   canvas.rotate(rotate, x, y);      phase++;   PathDashPathEffect pathDashPathEffect =     novel PathDashPathEffect(dashPath, 15.0f, phase, PathDashPathEffect.Style.MORPH);      Paint paintDash = myShape.getPaint();   paintDash.setPathEffect(pathDashPathEffect);      canvas.drawPath(myShape.getPath(), myShape.getPaint());      //restore sheet   canvas.restore();      long cease = System.nanoTime();      String information = "myView.isHardwareAccelerated() = " + isHardwareAccelerated() + "\n"     + "canvas.isHardwareAccelerated() = " + canvas.isHardwareAccelerated() + "\n"     + "processing fourth dimension (reference only) : " + String.valueOf(end - starting) + " (ns)";   textLayerInfo.setText(info);      invalidate();     }    populace void setShapeRadiusRatio(float ratio){   ratioRadius = ratio;  }    populace void setShapeInnerRadiusRatio(float ratio){   ratioInnerRadius = ratio;  }    populace void setNumberOfPoint(int pt){   numberOfPoint = pt;  }    populace void passElements(TextView textLayerInfo){   this.textLayerInfo = textLayerInfo;  }   populace void setShapeRotate(int rot){   rotate = (float)rot;  }  } 

Other files, scream to last example.

corresponding to UI chemical ingredient  TextView textLayerInfo Implement running dash path, using PathDashPathEffectDownload the files.

More instance of Drawing Path on sheet of custom View.