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="lesapplication.blogspot.com" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="segmentLength"/>
<SeekBar
android:id="@+id/segmentLength"
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="deviation"/>
<SeekBar
android:id="@+id/deviation"
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="strokeWidth"/>
<SeekBar
android:id="@+id/strokeWidth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="20"/>
<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 seekBarSegmentLength, seekBarDeviation;
private SeekBar seekBarStrokeWidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myView = (MyView)findViewById(R.id.myview);
seekBarSegmentLength = (SeekBar)findViewById(R.id.segmentLength);
seekBarSegmentLength.setOnSeekBarChangeListener(seekBarSegmentLengthChangeListener);
seekBarDeviation = (SeekBar)findViewById(R.id.deviation);
seekBarDeviation.setOnSeekBarChangeListener(seekBarDeviationChangeListener);
seekBarStrokeWidth = (SeekBar)findViewById(R.id.strokeWidth);
seekBarStrokeWidth.setOnSeekBarChangeListener(seekBarStrokeWidthChangeListener);
}
OnSeekBarChangeListener seekBarSegmentLengthChangeListener =
new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
myView.setSegmentLength(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
};
OnSeekBarChangeListener seekBarDeviationChangeListener =
new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
myView.setDeviation(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
};
OnSeekBarChangeListener seekBarStrokeWidthChangeListener =
new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
myView.setStrokeWidth(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
};
}
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.DiscretePathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
Paint paintBorder, paintCircle;
Path pathBorder, pathCircle;
Path pathShape;
float segmentLength;
float deviation;
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() {
paintBorder = new Paint();
paintBorder.setColor(Color.BLUE);
paintBorder.setStrokeWidth(20);
paintBorder.setStyle(Paint.Style.STROKE);
paintCircle = new Paint();
paintCircle.setColor(Color.RED);
paintCircle.setStrokeWidth(20);
paintCircle.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();
segmentLength = 30.0f;
deviation = 30.0f;
}
@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);
DiscretePathEffect discretePathEffect =
new DiscretePathEffect(segmentLength, deviation);
paintBorder.setPathEffect(discretePathEffect);
paintCircle.setPathEffect(discretePathEffect);
canvas.drawPath(pathBorder, paintBorder);
canvas.drawPath(pathCircle, paintCircle);
}
public void setDeviation(int dev){
deviation = (float)dev;
invalidate();
}
public void setSegmentLength(int seglen){
//force segmentLength not 0
if (seglen==0){
seglen = 1;
}
segmentLength = (float)seglen;
invalidate();
}
public void setStrokeWidth(int strwidth){
paintBorder.setStrokeWidth(strwidth);
paintCircle.setStrokeWidth(strwidth);
invalidate();
}
}
- More examples of drawing path on custom View.
0 Response to "DiscretePathEffect example"
Posting Komentar