Respect for other people's work results, reprinted please indicate the source: http://blog.csdn.net/gengqiquan/article/details/65938021 This article is from: [gengqiquan's blog]
Leaders recently felt that Ctrip's screenshot generated growth map sharing effect is better, so we added one; the product felt that the shared long map needs to add company brand watermarking, so we added one; well, the cause of the incident is that.
Long graphs are generally ScrollView and ListView.
We need to get the full display of the two controls. The principle is simple. Make a canvas that matches the width and length of the control (that is, create a bitmap with the same height and width). Then call the draw method of the control to draw itself onto the canvas.
A Method of Obtaining Long Drawings by Posting Two Controls
/**
* Intercept scrollview screen
**/
public static Bitmap getScrollViewBitmap(ScrollView scrollView) {
int h = 0;
Bitmap bitmap;
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
}
// Create bitmap of corresponding size
bitmap = Bitmap.createBitmap(ScreenUtils.getScreenWidth(scrollView.getContext()), h,
Bitmap.Config.ARGB_4444);
final Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.parseColor("#f2f7fa"));
scrollView.draw(canvas);
return bitmap;
}
/**
* Screenshot list view
**/
public static Bitmap getListViewBitmap(ListView listView, String picpath) {
int h = 0;
Bitmap bitmap;
// Get the actual height of listView
for (int i = 0; i < listView.getChildCount(); i++) {
h += listView.getChildAt(i).getHeight();
}
listView.getHeight());
// Create bitmap of corresponding size
bitmap = Bitmap.createBitmap(listView.getWidth(), h,
Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
listView.draw(canvas);
// Test output
FileOutputStream out = null;
try {
out = new FileOutputStream(picpath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if (null != out) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
}
return bitmap;
}
Presenting a way to get a display of a specific view
/**
* Generate a picture of a view
*
* @author gengqiquan
* @date 2017/3/20 10:34 a.m.
*/
public static Bitmap getViewDrawingCacheBitmap(View view) {
view = view.getRootView();
if (!view.isDrawingCacheEnabled()) {
view.setDrawingCacheEnabled(true);
}
view.destroyDrawingCache();
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
view.setDrawingCacheEnabled(false);
return bm;
}
Give me another way to generate a Linear Layout image
/**
* Generate an image of a Linear Layout
*
* @author gengqiquan
* @date 2017/3/20 10:34 a.m.
*/
public static Bitmap getLinearLayoutBitmap(LinearLayout linearLayout) {
int h = 0;
// Get the actual height of LinearLayout
for (int i = 0; i < linearLayout.getChildCount(); i++) {
linearLayout.getChildAt(i).measure(0, 0);
h += linearLayout.getChildAt(i).getMeasuredHeight();
}
linearLayout.measure(0, 0);
// Create bitmap of corresponding size
Bitmap bitmap = Bitmap.createBitmap(linearLayout.getMeasuredWidth(), h,
Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
linearLayout.draw(canvas);
return bitmap;
}
After the product is finished, you will definitely add the logo image of the company below or above. Hmm. Good people do low, and then send a mosaic picture method
/**
*Stitching pictures
* @param first Shared Long Map
* @param second Company logo chart
*@author gengqiquan
*@date 2017/3/25 4:56 p.m.
*/
public static Bitmap add2Bitmap(Bitmap first, Bitmap second) {
float scale = ((float) first.getWidth()) / second.getWidth();
second = ImageUtil.scaleImg(second, scale);
int width = first.getWidth();
int height = first.getHeight() + second.getHeight();
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(first, 0, 0, null);
canvas.drawBitmap(second, 0, first.getHeight(), null);
return result;
}
Another method of adding full-image watermarking
/**
* @param first Primitive graph
* @param mark Watermarking
* @author gengqiquan
* @date 2017/3/25 4:58 p.m.
*/
public static Bitmap waterMark(Bitmap first, Bitmap mark) {
float scale = ((float) first.getWidth()) / mark.getWidth();
mark = ImageUtil.scaleImg(mark, scale);
int width = first.getWidth();
int height = first.getHeight();
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(first, 0, 0, null);
int h = 0;
while (h < height + mark.getHeight()) {
canvas.drawBitmap(mark, 0, h, null);
h = h + mark.getHeight();
}
return result;
}
My father's chrome has a bug. At this point, the line-break button will collapse. Writing a blog crashed seven or eight times. It's really bad luck lately.
In fact, I want to say: because recently injected into an object, the dependency is relatively strong, so this period of time rarely blog, after the supplement.
What suggestions can I leave a message?
If my blog is helpful to you, please leave a message to encourage or praise it.
I built a QQ group (group number: 121606151) to discuss and exchange Android technology issues. If you are interested, you can make progress together.