0

我有一个包含我尝试在其上绘制的 CustomImage(扩展视图)的 Activity,我可以看到线条,但在我的 touchUp 上它们消失了。我这样设置油漆:

paint = (CustomImage) findViewById(R.id.paint);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFF000000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setXfermode(null);
mPaint.setAlpha(0xFF);
mPaint.setMaskFilter(null);
mPaint.setStrokeWidth(50);
paint.setPaint(mPaint);

这是我的 CustomImage 类:

public class CustomImage extends View {

private Bitmap bitmap;

public Context context;

Paint paint = new Paint();

private Canvas canvas = new Canvas();

private Path mPath = new Path();

private float mX, mY;

private static final float TOUCH_TOLERANCE = 4;

private float screenDensity;

int height, width;

public CustomImage(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

public CustomImage(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public CustomImage(Context context) {
    super(context);
    init(context);
}

private void init(Context context) {
    this.context = context;
}

public void setImg(Context context, Canvas canvas, Bitmap bitmap, int width, int height) {
    this.bitmap = bitmap;
    canvas.drawBitmap(bitmap, 0, 0, null);
}

public void setPaint(Paint paint) {
    this.paint = paint;
}
public void getBitmap(Bitmap bitmap) {
    this.bitmap = bitmap;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(bitmap, 0, 0, null);
    canvas.drawPath(mPath, paint);

}

private void touch_start(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
}

private void touch_move(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if ((dx >= TOUCH_TOLERANCE) || (dy >= TOUCH_TOLERANCE)) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
    }
}

private void touch_up() {
    mPath.lineTo(mX, mY);
    mPath.moveTo(mX, mY);
    canvas.drawPath(mPath, paint);
    mPath.reset();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        touch_start(x, y);

        invalidate();
        break;
    case MotionEvent.ACTION_MOVE:
        touch_move(x, y);
        invalidate();
        break;
    case MotionEvent.ACTION_UP:
        touch_up();
        invalidate();
        break;
    }
    return true;
}
}

首先,我在视图上设置了背景图像。然后我应该能够画出那幅画。该应用程序绘制了线条,但它们在修饰时消失了。我怎样才能避免这种情况?

4

2 回答 2

1

我使位图可变,然后应用 Canvas canvas = new Canvas(bitmap); 我如何使位图可变:

   public void setBitmap(Bitmap bitmap, Bitmap.Config targetConfig) throws Exception {
    System.out.println("CustomImage.setBitmap()");
    File file = new File("/mnt/sdcard/sample/temp.txt");
    file.getParentFile().mkdirs();
    randomAccessFile = new RandomAccessFile(file, "rw");
    int bWidth = bitmap.getWidth();
    int bHeight = bitmap.getHeight();
    FileChannel channel = randomAccessFile.getChannel();
    MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, bWidth*bHeight*4);
    bitmap.copyPixelsToBuffer(map);
    bitmap.recycle();
    this.bitmap = Bitmap.createBitmap(bWidth, bHeight, targetConfig);
    map.position(0);
    this.bitmap.copyPixelsFromBuffer(map);
    channel.close();
    randomAccessFile.close();
    //        this.bitmap = bitmap;


    // canvas = new Canvas();
}
于 2012-11-02T10:29:30.147 回答
0

您可以使用画布和位图;

    mBitmap = Bitmap.createBitmap(mPictureWidth, mPictureHeight,
            Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
    mCanvas.save();

绘制时:

@Override
protected void onDraw(Canvas canvas) {
    mCanvas.restore();
    mCanvas.drawPath(path, paint);
    mCanvas.save();
    if (mBitmap != null)
        canvas.drawBitmap(mBitmap, 0, 0, null);
}

先绘制到自己创建的画布上,再绘制到视图的画布上,记录保存在位图中

于 2013-09-21T14:01:43.250 回答