3

在我的 Paint App 中,我使用了自定义视图,该视图位于 Finger Paint API 演示中。像这里给出的代码:

public class MyView extends View {

private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;
Paint mPaint;
Bitmap mBitmap;
Canvas mCanvas;
Path mPath;
private Paint mBitmapPaint;
int w, h;

public MyView(Context c) {
    super(c);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(0xFFFF0000);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(1);
    DisplayMetrics metrics = getContext().getResources()
            .getDisplayMetrics();
    w = metrics.widthPixels;
    h = metrics.heightPixels;
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
    mPath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {
    // canvas.drawColor(0xFFAAAAAA);

    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

    canvas.drawPath(mPath, mPaint);
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

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);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    // kill this so we don't double draw
    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;
    }
}

它工作正常,但在我的应用程序中,我放了一个按钮来更改自定义视图的背景,例如......

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path===========> : "
                    + selectedImagePath);
            img = (ImageView) findViewById(R.id.imageBackGround);
            // img.setImageURI(selectedImageUri);
            try {

                Bitmap bmp = BitmapFactory
                .decodeStream(getContentResolver().openInputStream(
                        selectedImageUri));
        Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0,
                bmp.getWidth(), bmp.getHeight());
        Drawable drawBackground = Drawable.createFromPath(selectedImagePath);
        myView.setBackgroundDrawable(drawBackground);
        myView.invalidate();
        //img.setImageBitmap(resizedBitmap);
        //img.setScaleType(ImageView.ScaleType.FIT_XY);

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

private String getPath(Uri uri) {
    // TODO Auto-generated method stub
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if (cursor != null) {
        // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
        // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else {
        return null;
    }
}

使用此代码,我从 SD 卡获取图像并用该图像替换自定义视图背景,在替换背景图像绘制后,手指和绘图路径之间存在一些延迟。

4

1 回答 1

2

您也可以尝试使用禁用硬件加速

<application android:hardwareAccelerated="false">
于 2012-12-01T14:55:20.780 回答