May 7, 2014

PathDashPathEffect example

android.graphics.PathDashPathEffect dash the drawn path by stamping it with the specified shape.

Example:
PathDashPathEffect example
PathDashPathEffect example

Modify MyView.java from the post.
package com.example.androidview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.PathDashPathEffect;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

 Paint paint;
 Path path;

 public MyView(Context context) {
  super(context);
  init();
 }

 public MyView(Context context, AttributeSet attrs) {
  super(context, attrs);
  init();
 }

 public MyView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  init();
 }

 private void init() {
  paint = new Paint();
  paint.setColor(Color.RED);
  paint.setStrokeWidth(20);
  paint.setStyle(Paint.Style.STROKE);

  path = new Path();
  
  Path pathShape = new Path();
  pathShape.addCircle(10, 10, 10, Direction.CCW);
  
  float advance = 30.0f;
  float phase = 20.0f;
  PathDashPathEffect.Style style = PathDashPathEffect.Style.ROTATE;
  
  PathDashPathEffect pathDashPathEffect =
   new PathDashPathEffect(pathShape, advance, phase, style);
  
  paint.setPathEffect(pathDashPathEffect);

 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  canvas.drawColor(Color.GRAY);

  path.reset();
  path.moveTo(50,  50);
  path.lineTo(50, getHeight()-50);
  path.lineTo(getWidth()-50, getHeight()-50);
  path.lineTo(getWidth()-50, 50);
  path.close();
  
  canvas.drawPath(path, paint);
 }

}

No comments:

Post a Comment

Infolinks In Text Ads