سؤال

i'm trying to draw a Bitmap but only with a selected area from the original Bitmap or Imageview. The requirement is that the final picture must show only 1/3 from the top of the original Bitmap. I attach a draft. I suppose that i should use canvas, but i do not know exactly how it works.

Thanks in advance!!original pic

هل كانت مفيدة؟

المحلول

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

or as a one liner:

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

It allows you to specify where on the canvas you want to draw it too, and where you want the segment to be from.

@Eu.Dr. 's answer would not work if you wanted to draw anything else on the canvas below it.

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){           
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top