Drawing mirrored bitmaps in android

前端 未结 3 1107
一个人的身影
一个人的身影 2020-12-03 03:12

I\'m trying to learn how to make an animated sprite in android and couldn\'t figure out how to go about organising my bitmaps. I have a sprite sheet of my character walking

相关标签:
3条回答
  • 2020-12-03 03:33

    To draw a vertical mirrored bitmap bmp on a canvas:

    Matrix m = new Matrix();
    // Mirror is basically a rotation
    m.setScale( -1 , 1 );
    // so you got to move your bitmap back to it's place. otherwise you will not see it
    m.postTranslate(canvas.getWidth(), 0);
    canvas.drawBitmap(bmp, m, p);
    
    0 讨论(0)
  • 2020-12-03 03:35

    Method 2 would be way too expensive, and you don't need a canvas to flip a bitmap. Simply create another bitmap with a Matrix applied, like so:

    BitmapDrawable flip(BitmapDrawable d)
    {
        Matrix m = new Matrix();
        m.preScale(-1, 1);
        Bitmap src = d.getBitmap();
        Bitmap dst = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, false);
        dst.setDensity(DisplayMetrics.DENSITY_DEFAULT);
        return new BitmapDrawable(dst);
    }
    
    0 讨论(0)
  • 2020-12-03 03:44

    To mirror your sprite simply apply the following transform on the Canvas: scale(-1, 1). You will have to offset the sprite by its width too.

    0 讨论(0)
提交回复
热议问题