这是第一次写博文,只是想找个地方积累一下自己觉得有用的,以后再次遇见这些问题的时候就可以更快的解决。
描述一下这个问题的背景,一个日志,有文字和很多张图片,以RecyclerView为载体显示,可以上下滑动。分享的时候需要将整个界面都分享出去,包括文字和图片。整个屏幕还可以截图,那超出屏幕的又该怎么去做呢,在网上找了一些代码,整理了一下。先上一张效果图(还不会像其他博友那样做动图,先上静态图)。
分享之前的图:
100, output);
int options2 = 100;
while (output.toByteArray().length > IMAGE_SIZE && options2 != 10) {
output.reset(); //清空baos
bmp.compress(Bitmap.CompressFormat.JPEG, options2, output);//这里压缩options%,把压缩后的数据存放到baos中
options2 -= 10;
}
bmp= BitmapFactory.decodeByteArray(output.toByteArray(), 0, output.toByteArray().length);
image=new UMImage(this,bmp);
}
switch (i) {
case 0:
dialog.shareWeixin(NewsActivity.this, "HelloBaby", mNewsDetail.getContent(), image, umShareListener,false);
break;
case 1:
dialog.shareWeixinCircle(NewsActivity.this, "HelloBaby", mNewsDetail.getContent(), image, umShareListener,true);
break;
case 2:
dialog.shareQQ(NewsActivity.this, "HelloBaby", mNewsDetail.getContent(), image, umShareListener);
break;
case 3:
dialog.shareWeibo(NewsActivity.this, "HelloBaby", mNewsDetail.getContent(), image, umShareListener);
break;
default:
break;
}
}
这里是将ScollView所包含的界面生产一个bitmap,工具类这里也给出。
package com.abings.baby.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Picture;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.ScrollView;
@SuppressWarnings("deprecation")
public class ContentToPictureUtils {
public static String DCIMCamera_PATH = Environment
.getExternalStorageDirectory() + "/DCIM/Camera/";
/**
* webView
*
* @param context
* @param webView
*/
public static void webviewContent2Png(Context context, WebView webView) {
Bitmap bmp = null;
bmp = captureWebView(webView);
// new Thread(new WorkThread(bmp)).start();
saveBitmapToCamera(context, bmp, null);
}
/**
* mScrollView
*
* @param context
* @param scrollView
*/
public static void scrollviewContent2Png(Context context,
ScrollView scrollView) {
Bitmap bmp = null;
bmp = getBitmapByView(scrollView);
// new Thread(new WorkThread(bmp)).start();
saveBitmapToCamera(context, bmp, null);
}
private static Bitmap captureWebView(WebView webView) {
Picture snapShot = webView.capturePicture();
Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(),
snapShot.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
snapShot.draw(canvas);
return bmp;
}
public static Boolean saveBitmapToCamera(Context context, Bitmap bm,
String name) {
File file = null;
if (name == null || name.equals("")) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
Date curDate = new Date(System.currentTimeMillis());
name = formatter.format(curDate) + ".jpg";
}
file= new File(DCIMCamera_PATH);
if (!file.exists()) {
file.mkdirs();
}
file = new File(DCIMCamera_PATH, name);
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
context.sendBroadcast(intent);
return true;
}
public static Bitmap getBitmapByView(ScrollView scrollView) {
int h = 0;
Bitmap bitmap = null;
for (int i = 0; i h += scrollView.getChildAt(i).getHeight();
scrollView.getChildAt(i).setBackgroundColor(
Color.parseColor("#ffffff"));
}
bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
scrollView.draw(canvas);
return bitmap;
}
}
图片生产后,创建UMImage,分享出去。具体分享代码参考友盟配置文档。