如何在画布上绘制多个位图

发布于 2024-10-27 08:50:35 字数 687 浏览 0 评论 0原文

我在使用画布功能时遇到问题,用画布绘制 1 个位图没有问题,但一旦添加第二个位图,它就只绘制最后一个位图,而不显示两者。 请帮忙。这是我的代码。 我的目的是在屏幕上独立地对这两个不同的位图进行动画处理。

@Override
        protected void onDraw(Canvas canvas) {
          football = BitmapFactory.decodeResource(getResources(),
                    R.drawable.ballicon);

        receiver = BitmapFactory.decodeResource(getResources(),
                R.drawable.rec);


       canvas.drawBitmap(football, translate, null);
       canvas.drawBitmap(receiver, translate, null);


        Matrix m = canvas.getMatrix();
        Log.d(DEBUG_TAG, "Matrix: " + translate.toShortString());
        Log.d(DEBUG_TAG, "Canvas: " + m.toShortString());
    }

谢谢

I am having trouble with the canvas function, I have no problem drawing 1 bitmap with the canvas but as soon as I add a 2nd bitmap, it only draws the last bitmap, does not show both.
please help. here is my code.
My intent, is to then animate these 2 different bitmaps independently on the screen.

@Override
        protected void onDraw(Canvas canvas) {
          football = BitmapFactory.decodeResource(getResources(),
                    R.drawable.ballicon);

        receiver = BitmapFactory.decodeResource(getResources(),
                R.drawable.rec);


       canvas.drawBitmap(football, translate, null);
       canvas.drawBitmap(receiver, translate, null);


        Matrix m = canvas.getMatrix();
        Log.d(DEBUG_TAG, "Matrix: " + translate.toShortString());
        Log.d(DEBUG_TAG, "Canvas: " + m.toShortString());
    }

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

农村范ル 2024-11-03 08:50:35

这是一个用于使用多个位图进行绘制的画布视图

public class AdvanceCanvasView extends View {
private Bitmap bitmap;
private Canvas bitmapCanvas;
private Bitmap mBitmapBrush;
private ArrayList<Bitmap> bitmapArrayList;
private Vector2 mBitmapBrushDimensions;
private Paint paintLine;
private List<Vector2> mPositions = new ArrayList<Vector2>(100);
private HashMap<Integer, Path> pathMap; // current Paths being drawn
private HashMap<Integer, Point> previousPointMap; // current Points
private int i = 0;

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;
}

@SuppressLint("UseSparseArrays")
public AdvanceCanvasView(Context context, AttributeSet attrs) {
    super(context, attrs); // pass context to View's constructor
}

public AdvanceCanvasView(Context c) {
    super(c);
    pathMap = new HashMap<>();
    previousPointMap = new HashMap<>();
    bitmapArrayList = new ArrayList<>();
    paintLine = new Paint();
}

@Override
public void onSizeChanged(int w, int h, int oldW, int oldH) {
    bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
            Bitmap.Config.ARGB_8888);
    bitmapCanvas = new Canvas(bitmap);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(bitmap, 0, 0, null);
    for (int i = 0; i < mPositions.size(); i++) {
        canvas.drawBitmap(bitmapArrayList.get(i), mPositions.get(i).x, mPositions.get(i).y, null);
    }

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getActionMasked();
    int actionIndex = event.getActionIndex();

    if (action == MotionEvent.ACTION_DOWN
            || action == MotionEvent.ACTION_POINTER_DOWN) {
        touchStarted(event.getX(actionIndex), event.getY(actionIndex),
                event.getPointerId(actionIndex));
    }
    else if (action == MotionEvent.ACTION_UP
            || action == MotionEvent.ACTION_POINTER_UP) {
        touchEnded(event.getPointerId(actionIndex));
    }
    else {
        touchMoved(event);
    }

    invalidate();

    return true;
}

private void touchStarted(float x, float y, int lineID) {
    Path path;
    Point point;
    path = new Path(); // create a new Path
    pathMap.put(lineID, path); // add the Path to Map
    point = new Point(); // create a new Point
    previousPointMap.put(lineID, point); // add the Point to the Map
    path = new Path(); // create a new Path
    point = new Point(); // create a new Point
    path.moveTo(x, y);
    point.x = (int) x;
    point.y = (int) y;

} // end method touchStarted

private void touchMoved(MotionEvent event) {
    // for each of the pointers in the given MotionEvent
    for (int i = 0; i < event.getPointerCount(); i++) {
        final float posX = event.getX();
        final float posY = event.getY();
        mPositions.add(new Vector2(posX - mBitmapBrushDimensions.x / 2, posY - mBitmapBrushDimensions.y / 2));
        bitmapArrayList.add(mBitmapBrush);
    }
    invalidate();

}

private void touchEnded(int lineID) {
    Path path = pathMap.get(lineID);
    path.reset();
}

public void init(Bitmap bitmap) {
    mBitmapBrush = bitmap;
    BitmapShader shader = new BitmapShader(mBitmapBrush, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
    paintLine.setShader(shader);
    mBitmapBrushDimensions = new Vector2(mBitmapBrush.getWidth(), mBitmapBrush.getHeight());
}

}

Here is a canvasview for drawing with multiple bitmaps

public class AdvanceCanvasView extends View {
private Bitmap bitmap;
private Canvas bitmapCanvas;
private Bitmap mBitmapBrush;
private ArrayList<Bitmap> bitmapArrayList;
private Vector2 mBitmapBrushDimensions;
private Paint paintLine;
private List<Vector2> mPositions = new ArrayList<Vector2>(100);
private HashMap<Integer, Path> pathMap; // current Paths being drawn
private HashMap<Integer, Point> previousPointMap; // current Points
private int i = 0;

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;
}

@SuppressLint("UseSparseArrays")
public AdvanceCanvasView(Context context, AttributeSet attrs) {
    super(context, attrs); // pass context to View's constructor
}

public AdvanceCanvasView(Context c) {
    super(c);
    pathMap = new HashMap<>();
    previousPointMap = new HashMap<>();
    bitmapArrayList = new ArrayList<>();
    paintLine = new Paint();
}

@Override
public void onSizeChanged(int w, int h, int oldW, int oldH) {
    bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
            Bitmap.Config.ARGB_8888);
    bitmapCanvas = new Canvas(bitmap);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(bitmap, 0, 0, null);
    for (int i = 0; i < mPositions.size(); i++) {
        canvas.drawBitmap(bitmapArrayList.get(i), mPositions.get(i).x, mPositions.get(i).y, null);
    }

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getActionMasked();
    int actionIndex = event.getActionIndex();

    if (action == MotionEvent.ACTION_DOWN
            || action == MotionEvent.ACTION_POINTER_DOWN) {
        touchStarted(event.getX(actionIndex), event.getY(actionIndex),
                event.getPointerId(actionIndex));
    }
    else if (action == MotionEvent.ACTION_UP
            || action == MotionEvent.ACTION_POINTER_UP) {
        touchEnded(event.getPointerId(actionIndex));
    }
    else {
        touchMoved(event);
    }

    invalidate();

    return true;
}

private void touchStarted(float x, float y, int lineID) {
    Path path;
    Point point;
    path = new Path(); // create a new Path
    pathMap.put(lineID, path); // add the Path to Map
    point = new Point(); // create a new Point
    previousPointMap.put(lineID, point); // add the Point to the Map
    path = new Path(); // create a new Path
    point = new Point(); // create a new Point
    path.moveTo(x, y);
    point.x = (int) x;
    point.y = (int) y;

} // end method touchStarted

private void touchMoved(MotionEvent event) {
    // for each of the pointers in the given MotionEvent
    for (int i = 0; i < event.getPointerCount(); i++) {
        final float posX = event.getX();
        final float posY = event.getY();
        mPositions.add(new Vector2(posX - mBitmapBrushDimensions.x / 2, posY - mBitmapBrushDimensions.y / 2));
        bitmapArrayList.add(mBitmapBrush);
    }
    invalidate();

}

private void touchEnded(int lineID) {
    Path path = pathMap.get(lineID);
    path.reset();
}

public void init(Bitmap bitmap) {
    mBitmapBrush = bitmap;
    BitmapShader shader = new BitmapShader(mBitmapBrush, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
    paintLine.setShader(shader);
    mBitmapBrushDimensions = new Vector2(mBitmapBrush.getWidth(), mBitmapBrush.getHeight());
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文