简体   繁体   English

Android使用画布绘制比例位图的一部分

[英]Android draw a part of scaled bitmap using canvas

I'm using canvas and have an image 1000x500, phone width 480 and height 800 我正在使用画布,并且图像尺寸为1000x500,电话宽度为480,高度为800

Scale sample: 规模样本:

        scale = (float) (height / image.getHeight());
        image_width = image.getWidth() * scale;
        image_height = image.getHeight() * scale;

How do I draw this scaled image using canvas, having only center(screen size) of the image visible while left and right sides are outside of screen? 如何使用画布绘制此缩放的图像,而左侧和右侧在屏幕外部,只有图像的中心(屏幕尺寸)可见?

do it this way 这样做

Rect rs = new Rect();
Rect rd = new Rect();
rs.left = rs.top = 0;
rs.right = 480;
rs.bottom = 800;

<calculate destination rectangle from device size>

canvas.drawBitmap(myBitmap, rs, rd, null);

You can also scale and translate (shift) the entire canvas 您还可以缩放和翻译(移动)整个画布

canvas.scale(float scaleX, float scaleY);
canvas.translate(float dx, float dy);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM