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

时间:2014-04-22 13:55:07

标签: android canvas bitmap imageview draw

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

提前致谢!! original pic

3 个答案:

答案 0 :(得分:1)

    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;
}

答案 1 :(得分:1)

This is the documentation for the method you should use.

private void draw(Canvas c, Bitmap bmp){
Rect r=new Rect(0,0,bmp.width,bmp.height/3);
Rect drawR=new Rect(0,0,c.width,c.height/3);
c.drawBitmap(bmp,r,drawR,null);
}

或作为一个班轮:

c.drawBitmap(bmp,new Rect(0,0,bmp.width,bmp.height/3),new Rect(0,0,c.width,c.height/3),null);

它允许您指定要在画布上绘制它的位置,以及您希望片段来自何处。

@ Eu.Dr。如果你想在它下面的画布上绘制任何其他内容,那么它的答案就不会有效。

答案 2 :(得分:-1)

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){           
}