Creating dashed circle and bulls eye(Marker)





In this tutorial, I am going to explain how to create dashed line or circle.

All we have to do for dashed circle, create a paint object with some special settings. To paint object we have to set some properties. Android have a class called "DashPathEffect".


DashPathEffect dashPath = new DashPathEffect(new float[]{5,5}, 1};


Then to paint object, we have to attach this "dashPath" by,


paint.setPathEffect(dashPath);

then

paint.setStyle(Style.STROKE);
paint.setStrokeWidth(3.0f);

Then in onDraw(), use this paint object in the drawCircle().

The complete source code is,


public class AndDashedLine extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ViewDashedLine(this));
}

class ViewDashedLine extends View {

Context mContext;
Paint linePaint;
Paint dotPaint;
Paint linePaint1;
DashPathEffect dashPath;
public ViewDashedLine(Context context) {
super(context);
mContext = context;
dashPath = new DashPathEffect(new float[]{5, 5}, 1);

linePaint = new Paint();
linePaint.setAntiAlias(true);
linePaint.setARGB(255, 255, 0, 0);
linePaint.setPathEffect(dashPath);
linePaint.setStyle(Style.STROKE);
linePaint.setStrokeWidth(3.0f);

dotPaint = new Paint();
dotPaint.setAntiAlias(true);
dotPaint.setARGB(255, 255, 0, 0);
dotPaint.setStyle(Style.FILL);

linePaint1 = new Paint();
linePaint1.setAntiAlias(true);
linePaint1.setARGB(255, 255, 0, 0);
linePaint1.setStrokeWidth(3.0f);
}

public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(160, 200, 40, linePaint);
canvas.drawCircle(160, 200, 20, linePaint);
canvas.drawCircle(160, 200, 4, dotPaint);

canvas.drawLine(160, 160, 160, 130, linePaint1);
canvas.drawLine(160, 240, 160, 270, linePaint1);
canvas.drawLine(200, 200, 230, 200, linePaint1);
canvas.drawLine(120, 200, 90, 200, linePaint1);
}
}
}

Comments

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android

Open Gallery and get the selected image in appplication