I am trying to create a Drawing Application, the Application will be able to draw different Brush Textures along the touched path on the Screen.
What i have done so far:
Here is the code of my Custom View:
public class TestDrawingView extends View{
private Bitmap mBitmapBrush;
private Vector2 mBitmapBrushDimensions;
private List<Vector2> mPositions = new ArrayList<Vector2>(100);
public TestDrawingView(Context context) {
super(context);
// TODO Auto-generated constructor stub
// load your brush here
mBitmapBrush = BitmapFactory.decodeResource(context.getResources(), R.drawable.test_sand_brush);
mBitmapBrushDimensions = new Vector2(10, 10);
setBackgroundColor(0xffffffff);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (Vector2 pos : mPositions) {
canvas.drawBitmap(mBitmapBrush, pos.x, pos.y, null);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_MOVE:
final float posX = event.getX();
final float posY = event.getY();
mPositions.add(new Vector2(posX - mBitmapBrushDimensions.x / 2, posY - mBitmapBrushDimensions.y / 2));
invalidate();
}
return true;
}
private static final class Vector2 {
public Vector2(float x, float y) {
this.x = x;
this.y = y;
}
public final float x;
public final float y;
}
}
I have taken this example code from this Question How to make custom brush for canvas in android?
The Texture Image that i have used:

What results i am getting :

The Result i want to Achieve :

Any help is highly appreciated.
Drawing one bitmap on every registered touch location is a good start, but in order to create a smooth effect like the one you see here, you're going to need a bit more logic here. I'll outline some steps for you to implement:
Math.atan2(p2.y - p1.y, p2.x - p1.x);This should get you started, although you may wish to implement more complex logic in cases in which the line intersects itself - in which case you may want to have (or construct) an "intersection" bitmap and use more logic to identify when this happens and how to rotate the bitmap accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With