May 8, 2014

Running PathDashPathEffect example

This example implement running PathDashPathEffect, by varying phase parameter.


Modify MyView.java from the post.

package com.example.androidview;

import android.annotation.SuppressLint;
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;
 
 Path pathShape;
 float phase;
 float advance;
 PathDashPathEffect.Style style;

 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();
  
  pathShape = new Path();
  pathShape.addCircle(10, 10, 10, Direction.CCW);
  
  phase = 0;
  advance = 30.0f;
  style = PathDashPathEffect.Style.ROTATE;

 }

 @SuppressLint("DrawAllocation")
 @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();
  
  phase++;
  PathDashPathEffect pathDashPathEffect =
    new PathDashPathEffect(pathShape, advance, phase, style);
  paint.setPathEffect(pathDashPathEffect);
  
  canvas.drawPath(path, paint);
  
  invalidate();
 }

}


- Effect of advance, phase, style in PathDashPathEffect

- More examples of drawing path on custom View

No comments:

Post a Comment

Infolinks In Text Ads