Skip to content Skip to sidebar Skip to footer

Problems Drawing A Bitmap On A Layout

I create the Layout programatically in onCreate: new Handler().postDelayed(new Runnable() { @Override public void run() { myView = new MyView(SketchAct

Solution 1:

This solved it:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    intwidth= bm.getWidth();
    intheight= bm.getHeight();
    floatscaleWidth= ((float) newWidth) / width;
    floatscaleHeight= ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATIONMatrixmatrix=newMatrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);
    // RECREATE THE NEW BITMAPBitmapresizedBitmap= Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}

publicMyView(Context c, int width, int height) {
        super(c);

        WindowManagerwm= (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
        Displaydisplay= wm.getDefaultDisplay();
        intw= display.getWidth(); // deprecatedinth= display.getHeight();
        setFocusable(true);
        setBackgroundResource(R.drawable.download);

        // setting paint
        mPaint = newPaint();
        mPaint.setAlpha(0);
        mPaint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.DST_IN));
        mPaint.setAntiAlias(true);
        mPaint.setMaskFilter(newBlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL));

        // getting image from resourcesResourcesr=this.getContext().getResources();

        Bitmapbm= BitmapFactory.decodeResource(getResources(), R.drawable.smoke);
        Bitmapbm2= getResizedBitmap(bm, h, w);

        // converting image bitmap into mutable bitmap

        bitmap = bm2.createBitmap(w, h, Config.ARGB_8888);
        mCanvas = newCanvas();
        mCanvas.setBitmap(bitmap); // drawXY will result on that Bitmap
        mCanvas.drawBitmap(bm2, 0, 0, null);

    }

Solution 2:

Looks like you're recycling your Bitmap somwhere in the code. There is a problem.

Yes, you should recycle it, but it's always a bit risky.

EDIT

Looks like you're drawing onto Bitmap. If it's not what you're trying to do, then you shouldn't call onDraw manually. Instead call invalidate() when you want to redraw your view.

Solution 3:

You need to add an Options data like this:

Optionsoptions=newOptions();
options.isMutable = true; // works from api 11

mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.smoke, options);

Post a Comment for "Problems Drawing A Bitmap On A Layout"