手指画得到改变的背景后慢 [英] Finger paint get slow after changing background

查看:142
本文介绍了手指画得到改变的背景后慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的画图软件,我用自定义的视图,这是在有手指画API演示。在code喜欢这里给出:

In my Paint App i used custom view, which is there in Finger paint API demo. The code like given here:

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

它工作正常,但在我的应用我把一个按钮的变化背景自定义视图像....

It's working fine, but in my application i put a button for change the background of custom view like....

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

使用这个code我得到的图像从SD卡,并与图像替换自定义视图的背景下,更换背景图片油漆后有手指和绘制路径之间有一些延迟。

Using this code i am getting image From SD card and replacing custom view background with that image, After replacing background image paint there is some delay between finger and drawing paths.

推荐答案

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

Also you can try to disable hardware acceleration using

<application android:hardwareAccelerated="false">

这篇关于手指画得到改变的背景后慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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