Taliadon
Taliadon

Reputation: 438

Using Canvas.drawBitmap with matrix produces wrong size

Having loaded a bitmap image from a resource without scaling, I'm attempting to draw it directly to a canvas using the draw bitmap method in conjunction with a suitable scaling matrix. Unfortunately, the bitmap doesn't appear to exhibit the correct dimensions when drawn - an identity matrix which is post-scaled by (0.5, 0.5), doesn't render an image of half the bitmap dimensions. Is there something I'm overlooking here?

Here's my code:

import android.content.Context;
import android.view.View;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Color;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.RectF;
import android.graphics.Matrix;

public class LogoView extends View
{
 private final Matrix mf_logoMatrix;
 private final Paint mf_paint;
 private final Bitmap mf_logoBitmap;


 public LogoView(final Context p_context)
 { 
  super(p_context);
  mf_logoMatrix = new Matrix();
  mf_paint = new Paint();
  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inScaled = false;
  mf_logoBitmap = BitmapFactory.decodeResource(p_context.getResources(),
                                               R.drawable.logo_1500,
                                               options);
 }


 @Override public void onDraw(Canvas p_canvas)
 {
  mf_paint.setColor(Color.BLACK);
  p_canvas.drawPaint(mf_paint);
  mf_logoMatrix.reset();
  mf_logoMatrix.preScale(0.5f, 0.5f);
  p_canvas.setMatrix(mf_logoMatrix);
  p_canvas.drawBitmap(mf_logoBitmap, mf_logoMatrix, null);
 }


};

Upvotes: 2

Views: 3224

Answers (1)

Taliadon
Taliadon

Reputation: 438

OK, after looking at my code again, I've just noticed the schoolboy mistake I was looking for...I've applied the matrix to the canvas and via the draw bitmap method...I guess once is enough to get the job done :) I love matrices soooo much, I felt the need to use it twice. Thanks for your time everyone; I'm off to flogg myself with a wet kipper.

Upvotes: 1

Related Questions