Created
July 5, 2017 07:15
-
-
Save tejpratap46/afadf021e7d8eddbaae8f0f160112e56 to your computer and use it in GitHub Desktop.
Generate Bitmap from view in android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Bitmap; | |
import android.graphics.Canvas; | |
import android.util.Log; | |
import android.view.View; | |
import java.util.ArrayList; | |
public class ImageUtil { | |
/** | |
* Get bitmap of a view | |
* | |
* @param view source view | |
* @return generated bitmap object | |
*/ | |
public static Bitmap getBitmapFromView(View view) { | |
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); | |
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), | |
Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(bitmap); | |
view.layout(0, 0, view.getWidth(), view.getHeight()); | |
Log.d("", "combineImages: width: " + view.getWidth()); | |
Log.d("", "combineImages: height: " + view.getHeight()); | |
view.draw(canvas); | |
return bitmap; | |
} | |
/** | |
* Stitch two images one below another | |
* | |
* @param listOfBitmapsToStitch List of bitmaps to stitch | |
* @return resulting stitched bitmap | |
*/ | |
public static Bitmap combineImages(ArrayList<Bitmap> listOfBitmapsToStitch) { | |
Bitmap bitmapResult = null; | |
int width = 0, height = 0; | |
for (Bitmap bitmap : listOfBitmapsToStitch) { | |
width = Math.max(width, bitmap.getWidth()); | |
height = height + bitmap.getHeight(); | |
} | |
bitmapResult = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); | |
Canvas comboImageCanvas = new Canvas(bitmapResult); | |
int currentHeight = 0; | |
for (Bitmap bitmap : listOfBitmapsToStitch) { | |
comboImageCanvas.drawBitmap(bitmap, 0f, currentHeight, null); | |
currentHeight = currentHeight + bitmap.getHeight(); | |
} | |
return bitmapResult; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks