Android使用画布从位图或Imageview绘制区域

Android使用画布从位图或Imageview绘制区域,android,canvas,bitmap,imageview,draw,Android,Canvas,Bitmap,Imageview,Draw,我正在尝试绘制位图,但仅使用原始位图或Imageview中的选定区域。要求是最终图片必须仅显示原始位图顶部的1/3。我附上一份草稿。我想我应该使用canvas,但我不知道它是如何工作的 提前谢谢 Bitmap getTheReducedBitmap(Bitmap fullLengthBitnap) { Bitmap backDrop=Bitmap.createBitmap(fullLengthBitnap.getWidth(), fullLengthBitnap.ge

我正在尝试绘制位图,但仅使用原始位图或Imageview中的选定区域。要求是最终图片必须仅显示原始位图顶部的1/3。我附上一份草稿。我想我应该使用canvas,但我不知道它是如何工作的

提前谢谢

    Bitmap getTheReducedBitmap(Bitmap  fullLengthBitnap)
{    
    Bitmap backDrop=Bitmap.createBitmap(fullLengthBitnap.getWidth(), fullLengthBitnap.getHeight()/3, Bitmap.Config.RGB_565);
    Canvas can = new Canvas(backDrop);
    can.drawBitmap(fullLengthBitnap, 0, 0, null);
    return backDrop;
}
或作为一个班轮:

c.drawBitmap(bmp,new Rect(0,0,bmp.width,bmp.height/3),new Rect(0,0,c.width,c.height/3),null);
它允许您指定要在画布上的何处绘制,以及要从何处绘制线段


@欧盟博士如果您想在其下方的画布上绘制任何其他内容,则s的答案将不起作用。

谢谢您的答案,但您的代码不起作用,因为您没有从原始位图复制,最终的图片将在不需要的区域显示黑色,而不是删除间隙。具体取决于画布的大小。这是如果你想在上面画其他东西的话。在黑色空间。我的观点是最终的图像视图显示1/3pic,没有黑色空间,但我理解我没有更明确地定义这个目标。请不要只发布代码作为答案,而是包括解释您的代码的作用以及它如何解决问题。带有解释的答案通常质量更高,更容易吸引选票。
val newBitmap=Bimtap
.createBitmap(your_view.width,your_view.height,Bitmap.Config.ALPHA_8)
//note: for tablet mode your_view.width,height will increase drastically so 
//you might want 
//to fix the size of drawing area for optimization
//ALPHA_8 each pixel requires 1 byte of memory.
//RGB_565 Each pixel is stored on 2 bytes
//ARGB_8888 Each pixel is stored on 4 bytes
//after this you can further apply the compress like
[Refer more from google][1]





val stream = ByteArrayOutputStream();
newBitmap.compress(Bitmap.CompressFormat.PNG, 80, stream);
val byteArray = stream.toByteArray(); // convert drawing photo to byte array
// save it in your internal storage.
val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES 
+"attendance_sheet.png");
try{
 val fo = FileOutputStream(storageDir);
 fo.write(byteArray);
 fo.flush();
fo.close();
}catch(Exception ex){           
}
val newBitmap=Bimtap
.createBitmap(your_view.width,your_view.height,Bitmap.Config.ALPHA_8)
//note: for tablet mode your_view.width,height will increase drastically so 
//you might want 
//to fix the size of drawing area for optimization
//ALPHA_8 each pixel requires 1 byte of memory.
//RGB_565 Each pixel is stored on 2 bytes
//ARGB_8888 Each pixel is stored on 4 bytes
//after this you can further apply the compress like
[Refer more from google][1]





val stream = ByteArrayOutputStream();
newBitmap.compress(Bitmap.CompressFormat.PNG, 80, stream);
val byteArray = stream.toByteArray(); // convert drawing photo to byte array
// save it in your internal storage.
val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES 
+"attendance_sheet.png");
try{
 val fo = FileOutputStream(storageDir);
 fo.write(byteArray);
 fo.flush();
fo.close();
}catch(Exception ex){           
}