Scaling and translating a bitmap in android

我们两清 提交于 2019-11-30 22:10:34

You should just use the Canvas methods for scaling and translating, that way you can then take advantage of the save() and restore() APIs to do what you need. For example:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    //Save the current state of the canvas
    canvas.save();

    scale = (float) screenWidth / 201.0f;

    canvas.translate(-40, -40);
    canvas.scale(scale, scale);
    canvas.drawBitmap(bitMap, 0, 0, paint);

    //Restore back to the state it was when last saved
    canvas.restore();

    canvas.drawColor(0, Mode.CLEAR);
    canvas.drawBitmap(bitMap, 0, 0, paint);
}

I think the problem with your original code might be because of the way scale and translate use the point around which you scale/translate. IF you specify the correct pivot points in between/for the operations, things will be ok.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!