strike
strike

Reputation: 1039

Android Canvas drawBitmap function efficiency

I'm trying to display a bitmap on canvas using the matrix.

      canvas.drawBitmap(currentBitmap, m_matrix, tempPaint);

but the result appears, image seems weird. After that I have shown it using bounds

     canvas.drawBitmap(currentBitmap, 0, 0, tempPaint);

But here image looks good, in both the cases image is not scaled.

How should I set matrix properties for the initial display?

Does the matrix display uses something else for showing the image because of that image is getting changed?

Please suggest any tutorials for details explanation.

Upvotes: 1

Views: 2668

Answers (2)

strike
strike

Reputation: 1039

After searching lot a lot i have finally found the solution, some of the threads related to same issue helped me out.

Quality Issue

Quality problems when resizing an image at runtime

I have followed the google's Tutorials for loading the bitmap and written my own function for scaling the bitmap.

public static Bitmap scaleDownBitmap(Bitmap original, boolean recycleOriginal, int newmaxWidth , int newmaxHeight){

        if(original == null)
             return null;

        Bitmap rtr= null;
        try{

                int origWidth  = original.getWidth();
                int origHeight = original.getHeight();


                if(origWidth <= newmaxWidth && origHeight <= newmaxWidth){
                        Bitmap b = Bitmap.createBitmap(original);
                        if (recycleOriginal && (original != b))
                            original.recycle();
                    return b;
                }

                int newWidth = 0;
                int newHeight = 0;

                float ratio;

                if(origWidth > origHeight){
                        ratio = (float)origWidth/(float)origHeight;
                        newWidth = newmaxWidth;
                        newHeight = (int)((float)newWidth/ratio);

                } 
                else{
                        ratio = (float)origHeight/(float)origWidth;
                        newHeight = newmaxHeight;
                        newWidth = (int)((float)newHeight/ratio);
                }

                rtr = CreateScaledBitmap(original , newWidth , newHeight);

                if(recycleOriginal && original != rtr)
                        original.recycle();

        }
        catch (Exception e) {
            Log.d("Image Compress Error", e.getMessage());
        }

        return rtr;

 }

     public static Bitmap CreateScaledBitmap(Bitmap paramBitmap, int paramInt1, int paramInt2)
   {
        Bitmap localBitmap = Bitmap.createBitmap(paramInt1, paramInt2, paramBitmap.getConfig());
        Canvas localCanvas = new Canvas(localBitmap);
        localCanvas.setDrawFilter(new PaintFlagsDrawFilter(0, 2));
        localCanvas.drawBitmap(paramBitmap, new Rect(0, 0, paramBitmap.getWidth(), paramBitmap.getHeight()),
                                            new Rect(0, 0, paramInt1, paramInt2), null);
    return localBitmap;
}

Now after getting the image i have passed the following parameters to the paint.

tempPaint.setFilterBitmap(true); //Line 1
canvas.drawBitmap(currentBitmap, m_matrix, tempPaint);

Upvotes: 1

Yngwie89
Yngwie89

Reputation: 1217

You have to use Matrix . post scale to scale the image, like in the example below.

private void drawMatrix(){

Matrix matrix = new Matrix(); matrix.postScale(curScale, curScale);

 canvas.drawBitmap(currentBitmap, matrix, tempPaint);} 

Haven't tried it yet.... But should work...

Upvotes: 0

Related Questions