我正在尝试创建一款绘画应用程序,该应用程序能够在屏幕上沿着触摸路径绘制不同的画笔纹理。
我已经完成的部分:
这是我的自定义视图的代码:
我从这个问题中获取了这个示例代码:如何在Android中为画布制作自定义画笔? 我使用的纹理图像如下:
我得到的结果如下:
我想要实现的结果如下:
非常感谢任何帮助。
我已经完成的部分:
这是我的自定义视图的代码:
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;
}
}
我从这个问题中获取了这个示例代码:如何在Android中为画布制作自定义画笔? 我使用的纹理图像如下:
![enter image description here](https://istack.dev59.com/kCD95.webp)
![enter image description here](https://istack.dev59.com/BA3sQ.webp)
![enter image description here](https://istack.dev59.com/hjVAv.webp)