Android的:一个给定的图像上的自定义绘制(带SurfaceView) [英] Android: custom draw on an given image (with SurfaceView)

查看:128
本文介绍了Android的:一个给定的图像上的自定义绘制(带SurfaceView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将图像加载到SurfaceView,并在这个形象画一些划痕。

I am trying to load an image to SurfaceView, and draw some scratch upon this image.

要来,我有创建一个自定义的SurfaceView,MySurfaceView,具有以下功能:

To to that, I have create an custom SurfaceView, MySurfaceView, with the following functions:

public MySurfaceView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    mHolder = getHolder();
    mHolder.addCallback(this);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(3);
    mPaint.setColor(Color.WHITE);
    setWillNotDraw(false);
}
public void setImageBitmap(Bitmap bitmap) {
    mBitmap = bitmap;
}
@Override
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(mBitmap, x, y, null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        mPath = new Path();
        mPath.moveTo(event.getX(), event.getY());
    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        mPath.lineTo(event.getX(), event.getY());
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        mPath.lineTo(event.getX(), event.getY());
    }
    if (mPath != null) {
        Canvas canvas = mHolder.lockCanvas();
        canvas.drawPath(mPath, mPaint);
        mHolder.unlockCanvasAndPost(canvas);
    }
    return true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {

}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

基本上,如果保持 setWillNotDraw(假)在构造函数中,SurfaceView会证明我将 mBitmap 的位图。否则,如果我评论了这一点,就会让我画在黑色背景上的路径。

Basically if keep setWillNotDraw(false) in the constructor, the SurfaceView will show the bitmap that I set mBitmap. Otherwise, if I comment this out, it will allow me to draw paths on a black background.

有什么办法,我可以在后台都做,把图像(mBitmap),并在这个图像上绘制路径?难道你们提供一些code这样做。非常感谢!

Is there any way that I can do both, put image (mBitmap) in the background and draw paths on this image? Could you guys provide some code to do that. Thanks a lot!

推荐答案

我知道了。

它基本上是把平局路径的onDraw(帆布油画)功能以及。为了确保SurfaceView调用的OnDraw一旦路径被吸引了。我们需要调用的无效()在触摸事件。

It is basically put the draw path to onDraw(Canvas canvas) function as well. To make sure the SurfaceView call onDraw once a path is drew. We need to call invalidate() in on touch event.

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(mBitmapScaled, x, y, null);
    if (mPath != null) {
        canvas.drawPath(mPath, mPaint);
    }
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        mPath = new Path();
        mPath.moveTo(event.getX(), event.getY());
    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        mPath.lineTo(event.getX(), event.getY());
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        mPath.lineTo(event.getX(), event.getY());
    }
    invalidate();
    return true;
}

甚至更好,你可以改变的mpath成一个ArrayList,这样就可以把图像上更多的路径。

Or even better, you can change the mPath into an ArrayList, so that you can put more paths on the image.

这篇关于Android的:一个给定的图像上的自定义绘制(带SurfaceView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆