0
\$\begingroup\$

I have created an image with the resolution of 25px(width)*34px(height) (I know it's unusual, however it doesn't bother me now). The problem is that when I try to display it by creating a bitmap, it becomes really really small on my Galaxy S4. It should look a little pixelated, kinda like a flappy bird sprite. I checked those sprites as well and they are even smaller in comparison to mine however it is displayed much bigger than my sprite on the screen. I want to know if I can make it look larger without scaling it. Is there a way to make my sprite look bigger?

@Override
protected void onDraw(Canvas canvas) {
    Log.i(TAG, "onDraw called");
    BitmapFactory.Options op = new BitmapFactory.Options();
    op.inPreferredConfig = Bitmap.Config.ALPHA_8;
    bm.setDensity(DisplayMetrics.DENSITY_DEFAULT);
    bm = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.giraffe_default,op));
    canvas.drawBitmap(bm,getWidth()/2,getHeight()/2,null);  //get from res, set coords to 10,10
}

This is my onDraw method where I draw my canvas and bitmap. As I said, my image appears to be so small, I can barely see it.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Draw the bitmap using a Matrix.

Matrix matrix;
matrix.reset();
matrix.postScale(2.0f, 2.0f);
matrix.postTranslate(getWidth()/2.0f, getHeight()/2.0f);
canvas.drawBitmap(bm, matrix, null);

EDIT: you probably should tell it where to draw too. My bad.

EDIT2: In response to you blurry comment, you can try turning off anti-aliasing, and bitmap filtering.

Paint paint = new Paint();
paint.setAntiAlias(false);
paint.setFilterBitmap(false);

canvas.drawBitmap(bm, matrix, paint);

If that doesn't give you the results you are after you can create the larger image by using blocks of color (i.e. 3x3, or 10x10) to represent each pixel of the original image. Also, consider searching this site, there have been similar or related questions already asked and answered.

Angry Birds Scaling

\$\endgroup\$
7
  • \$\begingroup\$ I added that snippet in my onDraw, however everytime i enter this gamesurface, the app crashes \$\endgroup\$ Commented Jan 7, 2015 at 21:38
  • \$\begingroup\$ My logcat tell me it has something to do with the thread. public void run(){ Canvas canvas; long frames = 0; while(isRunning){ canvas = null; frames++; try{ canvas = this.sHolder.lockCanvas(); synchronized(sHolder){ this.gamesurface.draw(canvas); } }finally{ if(canvas != null){ sHolder.unlockCanvasAndPost(canvas); } } } Log.d(TAG,"Gameloop executed: "+frames+" times"); } } \$\endgroup\$ Commented Jan 7, 2015 at 21:50
  • \$\begingroup\$ I now added matrix.postTranslate(getWidth()/2.0f, getHeight()/2.0f); but it still crashes :/ \$\endgroup\$ Commented Jan 8, 2015 at 21:30
  • \$\begingroup\$ Try using the debugger and see if you can find exactly what line it is puking on. \$\endgroup\$
    – Aholio
    Commented Jan 9, 2015 at 0:45
  • \$\begingroup\$ Alright so i got it working. The problem is that it's really blurry, as if it was zoomed in. I can't figure how to display my image larger without increasing the resoultion since i want it to look pixelated just like in flappy bird for instance. \$\endgroup\$ Commented Jan 9, 2015 at 1:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .