May 6, 2014

ComposePathEffect example

android.graphics.ComposePathEffect create PathEffect from other two PathEffects, to apply first the inner effect and the outer pathEffect.

Here is a example to create ComposePathEffect from CornerPathEffect and DashPathEffect, in various combination.



Where:

  • paint0: no effect
  • paint1: CornerPathEffect
  • paint2: DashPathEffect
  • paint3: ComposePathEffect(cornerPathEffect, dashPathEffect)
  • paint4: ComposePathEffect(dashPathEffect, cornerPathEffect)
  • paint5: ComposePathEffect(cornerPathEffect, cornerPathEffect)
  • ComposePathEffect(another dashPathEffect, dashPathEffect)

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.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

 Paint paint0, paint1, paint2, paint3, paint4, paint5, paint6;
 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() {
  paint0 = new Paint();
  paint0.setColor(Color.BLACK);
  paint0.setStrokeWidth(10);
  paint0.setStyle(Paint.Style.STROKE);
  
  paint1 = new Paint();
  paint1.set(paint0);
  paint1.setColor(Color.RED);
  
  paint2 = new Paint();
  paint2.set(paint0);
  paint2.setColor(Color.GREEN);
  
  paint3 = new Paint();
  paint3.set(paint0);
  paint3.setColor(Color.BLUE);
  
  paint4 = new Paint();
  paint4.set(paint0);
  paint4.setColor(Color.CYAN);
  
  paint5 = new Paint();
  paint5.set(paint0);
  paint5.setColor(Color.MAGENTA);
  
  paint6 = new Paint();
  paint6.set(paint0);
  paint6.setColor(Color.GRAY);

  path = new Path();
  path.moveTo(20, 20);
  path.lineTo(20, 60);
  path.lineTo(100, 80);
  path.lineTo(10, 150);
  path.lineTo(20, 200);
  path.lineTo(20, 500);
  path.lineTo(100, 500);
  path.lineTo(100, 300);
  path.lineTo(150, 300);

  float radius = 50.0f;
  CornerPathEffect cornerPathEffect = 
   new CornerPathEffect(radius);
  
  float[] intervals = new float[]{80.0f, 30.0f};
  float phase = 0;
  DashPathEffect dashPathEffect = 
   new DashPathEffect(intervals, phase);

  ComposePathEffect composePathEffect3 = 
   new ComposePathEffect(cornerPathEffect, dashPathEffect);
  ComposePathEffect composePathEffect4 = 
    new ComposePathEffect(dashPathEffect, cornerPathEffect);
  ComposePathEffect composePathEffect5 = 
    new ComposePathEffect(cornerPathEffect, cornerPathEffect);
  
  float[] intervals6 = new float[]{5.0f, 5.0f};
  float phase6 = 0;
  DashPathEffect dashPathEffect6 = 
    new DashPathEffect(intervals6, phase6);
  ComposePathEffect composePathEffect6 = 
    new ComposePathEffect(dashPathEffect6, dashPathEffect);

  paint1.setPathEffect(cornerPathEffect);
  paint2.setPathEffect(dashPathEffect);
  paint3.setPathEffect(composePathEffect3);
  paint4.setPathEffect(composePathEffect4);
  paint5.setPathEffect(composePathEffect5);
  paint6.setPathEffect(composePathEffect6);

 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  canvas.drawPath(path, paint0); //no effect
  canvas.translate(150, 0);
  canvas.drawPath(path, paint1);
  canvas.translate(150, 0);
  canvas.drawPath(path, paint2);
  canvas.translate(150, 0);
  canvas.drawPath(path, paint3);
  canvas.translate(150, 0);
  canvas.drawPath(path, paint4);
  canvas.translate(150, 0);
  canvas.drawPath(path, paint5);
  canvas.translate(150, 0);
  canvas.drawPath(path, paint6);
 }

}

No comments:

Post a Comment

Infolinks In Text Ads