【发布时间】:2014-04-22 13:55:07
【问题描述】:
我正在尝试绘制位图,但仅使用原始位图或 Imageview 中的选定区域。要求是最终图片必须仅显示距离原始位图顶部 1/3。我附上草稿。我想我应该使用画布,但我不知道它是如何工作的。
提前致谢!!
【问题讨论】:
标签: android canvas bitmap imageview draw
我正在尝试绘制位图,但仅使用原始位图或 Imageview 中的选定区域。要求是最终图片必须仅显示距离原始位图顶部 1/3。我附上草稿。我想我应该使用画布,但我不知道它是如何工作的。
提前致谢!!
【问题讨论】:
标签: android canvas bitmap imageview draw
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;
}
【讨论】:
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.如果您想在其下方的画布上绘制任何其他内容,则答案将不起作用。
【讨论】:
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){
}
【讨论】: