Jumat, 09 Mei 2014

Effect of advance, phase, style in PathDashPathEffect

The example make PathDashPathEffect with interactive setting of advance, phase and style; such that you can know how they affect the result.


Main layout, /res/layout/activity_main.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidview.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="advance"/>
<SeekBar
android:id="@+id/advance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="phase"/>
<SeekBar
android:id="@+id/phase"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30"/>
<Spinner
android:id="@+id/style"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<com.example.androidview.MyView
android:id="@+id/myview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

MainActivity.java
package com.example.androidview;

import android.app.Activity;
import android.graphics.PathDashPathEffect;
import android.graphics.PathDashPathEffect.Style;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;

public class MainActivity extends Activity {

private MyView myView;
private SeekBar seekBarAdvance, seekBarPhase;
private Spinner spinnerStyle;

private String[] styleNames ={
"PathDashPathEffect.Style.MORPH",
"PathDashPathEffect.Style.ROTATE",
"PathDashPathEffect.Style.TRANSLATE"};
private Style[] styleSettings = {
PathDashPathEffect.Style.MORPH,
PathDashPathEffect.Style.ROTATE,
PathDashPathEffect.Style.TRANSLATE};

private ArrayAdapter<String> spinnerStyleAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myView = (MyView)findViewById(R.id.myview);

seekBarAdvance = (SeekBar)findViewById(R.id.advance);
seekBarAdvance.setOnSeekBarChangeListener(seekBarAdvanceChangeListener);
seekBarPhase = (SeekBar)findViewById(R.id.phase);
seekBarPhase.setOnSeekBarChangeListener(seekBarPhaseChangeListener);

spinnerStyle = (Spinner)findViewById(R.id.style);
spinnerStyleAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, styleNames);
spinnerStyleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerStyle.setAdapter(spinnerStyleAdapter);
spinnerStyle.setOnItemSelectedListener(spinnerStyleOnItemSelectedListener);

}

OnSeekBarChangeListener seekBarAdvanceChangeListener =
new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
myView.setAdvance(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {}

};

OnSeekBarChangeListener seekBarPhaseChangeListener =
new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
myView.setPhase(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}

};

OnItemSelectedListener spinnerStyleOnItemSelectedListener =
new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
myView.setStype(styleSettings[position]);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}
};

}

MyView.java
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.PathDashPathEffect;
import android.graphics.Path.Direction;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

Paint paint;
Path pathBorder, pathCircle;

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.BLUE);
paint.setStrokeWidth(20);
paint.setStyle(Paint.Style.STROKE);

pathBorder = new Path();
pathCircle = new Path();

pathShape = new Path();
pathShape.moveTo(0, 0);
pathShape.lineTo(10, 20);
pathShape.lineTo(20, 0);
pathShape.close();

phase = 30.0f;
advance = 30.0f;
style = PathDashPathEffect.Style.MORPH;
}

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

pathBorder.reset();
pathBorder.moveTo(50, 50);
pathBorder.lineTo(50, getHeight()-50);
pathBorder.lineTo(getWidth()-50, getHeight()-50);
pathBorder.lineTo(getWidth()-50, 50);
pathBorder.close();

float radius;
pathCircle.reset();
if(getWidth()>getHeight()){
radius = getHeight()/4;
}else{
radius = getWidth()/4;
}
pathCircle.addCircle(getWidth()/2, getHeight()/2, radius, Direction.CCW);

PathDashPathEffect pathDashPathEffect =
new PathDashPathEffect(pathShape, advance, phase, style);
paint.setPathEffect(pathDashPathEffect);

canvas.drawPath(pathCircle, paint);
canvas.drawPath(pathBorder, paint);

}

public void setAdvance(int adv){
advance = (float)adv;
invalidate();
}

public void setPhase(int ph){
phase = (float)ph;
invalidate();
}

public void setStype(PathDashPathEffect.Style sty){
style = sty;
invalidate();
}

}


- Running PathDashPathEffect example

- More examples of drawing path on custom View.

0 komentar:

Posting Komentar

Copyright © 2012 Codding News All Right Reserved