Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Canvas.drawBitmap:如何绘制图像的右侧部分?_Android_Bitmap_Drawing_Android Canvas_Rect - Fatal编程技术网

Android Canvas.drawBitmap:如何绘制图像的右侧部分?

Android Canvas.drawBitmap:如何绘制图像的右侧部分?,android,bitmap,drawing,android-canvas,rect,Android,Bitmap,Drawing,Android Canvas,Rect,我想画图像的右侧部分,但结果是整个位图的拉伸图像,或位图中心的拉伸部分。我做错了什么 int width; int percentToDraw; Random rand = new Random(); percentToDraw = rand.nextInt(100); width = bmp.getWidth() * (100 - percentToDraw) / 100; src = new Rect(); src.top = 0; src.bottom = bmp.getHeight()

我想画图像的右侧部分,但结果是整个位图的拉伸图像,或位图中心的拉伸部分。我做错了什么

int width;
int percentToDraw;
Random rand = new Random();
percentToDraw = rand.nextInt(100);
width = bmp.getWidth() * (100 - percentToDraw) / 100;

src = new Rect();
src.top = 0;
src.bottom = bmp.getHeight();
src.right = bmp.getWidth();
src.left = src.right - width;

dst = new Rect;
dst.top = getHeight() / 2 - bmp.getHeight() / 2;
dst.bottom = dst.top + (src.bottom - src.height);
dst.right = getWidth() / 2 + bmp.getWidth() / 2;
dst.left = dst.right - width;

canvas.drawBitmap(bmp, src, dst, paint);

两天来,我一直在摆弄和谷歌搜索这个问题,试图找到一个解决方案:-(

如果其他人在这个问题上绊倒,我无法使它“正确”工作,但我已经使用clipRect找到了一个有效的解决方案:

        int width;
        width = (int) Math.floor(bmp.getWidth() * percentToDraw / 100.0);
        src = new Rect();
        src.top = 0;
        src.bottom = bmp.getHeight();
        src.left = 0;
        src.right = bmp.getWidth();

        dst = new Rect();
        dst.top = getHeight() / 2 - bmp.getHeight() / 2;
        dst.bottom = dst.top + (src.bottom - src.top);
        dst.left = getWidth() / 2 - bmp.getWidth() / 2;
        dst.right = dst.left + (src.right - src.left);

        canvas.clipRect(dst.right - width, 0, getWidth(), getHeight(), Region.Op.REPLACE);
        canvas.drawBitmap(bmp, src, dst, paint);
        canvas.clipRect(0, 0, getWidth(), getHeight(), Region.Op.REPLACE);