效果图:
主要代码:
package com.sun.framework.Utils;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.LabeledIntent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.provider.MediaStore;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by apple on 2017/4/19.
*/
public class ShareUtils {
public String mTitle;//分享的标题
public String mText;//分享的内容
public String[] mIncreaseNeedShareAppPlatform_SimplePackageNames;//增加需要分享的app平台的简单包名
private String type;//分享的类型
private Context context;
private ProgressDialog progressDialog;
private String[] needShareAppPlatform_SimplePackageNames;//需要分享的app平台的简单包名
public ShareUtils(Context context){
this.context = context;
mTitle = "分享";
mText = AppUtils.getAppName()+",您身边的好助手!";
}
private void initAppSimplePackageName(){
//腾讯QQ、微信、邮件、短信
String[] defaultAppSimplePackageNames = new String[]{"com.tencent.mobileqq","com.tencent.mm","com.sina.weibo","com.android.email","com.android.mms"};
if (null != mIncreaseNeedShareAppPlatform_SimplePackageNames && 0 != mIncreaseNeedShareAppPlatform_SimplePackageNames.length){
needShareAppPlatform_SimplePackageNames = concat(defaultAppSimplePackageNames,mIncreaseNeedShareAppPlatform_SimplePackageNames);
}else {
needShareAppPlatform_SimplePackageNames = defaultAppSimplePackageNames;
}
}
private String[] concat(String[] a, String[] b) {
String[] c= new String[a.length+b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}
/**
* 分享截图
* @param obj 支持Activity、ScrollView、ListView、RecyclerView、View
* ScrollView、ListView、RecyclerView为长截图
*/
public void shareScreenShot(final Object obj) {
progressDialog = ProgressDialog.show(context, "温馨提示", "正在截屏...", true, false);
type = "image/*";
final Intent shareIntent;
shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType(type);
new Thread(new Runnable() {
@Override
public void run() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Bitmap bitmap = null;
if (obj instanceof Activity){
bitmap = getActivityBitmap((Activity) obj);
}else if (obj instanceof ScrollView){
bitmap = getScrollViewBitmap((ScrollView) obj);
}else if (obj instanceof ListView){
bitmap = getListViewBitmap((ListView) obj);
}else if (obj instanceof RecyclerView){
bitmap = getRecyclerViewBitmap((RecyclerView) obj);
}else if (obj instanceof View){
bitmap = getViewBitmap((View) obj);
}
if (null != bitmap){
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, null,null));
filterShare(shareIntent,type,mTitle,mText,uri);
}else {
Toast.makeText(context, "截屏失败,请重试...", Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
progressDialog.cancel();
}
});
}
}).start();
}
public void shareStitchingScreenShot(final Object obj, final Object obj2) {
progressDialog = ProgressDialog.show(context, "温馨提示", "正在截屏...", true, false);
type = "image/*";
final Intent shareIntent;
shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType(type);
new Thread(new Runnable() {
@Override
public void run() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Bitmap bitmap = null;
if (obj instanceof Activity){
bitmap = getActivityBitmap((Activity) obj);
}else if (obj instanceof ScrollView){
bitmap = getScrollViewBitmap((ScrollView) obj);
}else if (obj instanceof ListView){
bitmap = getListViewBitmap((ListView) obj);
}else if (obj instanceof RecyclerView){
bitmap = getRecyclerViewBitmap((RecyclerView) obj);
}else if (obj instanceof View){
bitmap = getViewBitmap((View) obj);
}
Bitmap bitmap2 = null;
if (obj2 instanceof Activity){
bitmap2 = getActivityBitmap((Activity) obj2);
}else if (obj2 instanceof ScrollView){
bitmap2 = getScrollViewBitmap((ScrollView) obj2);
}else if (obj2 instanceof ListView){
bitmap2 = getListViewBitmap((ListView) obj2);
}else if (obj2 instanceof RecyclerView){
bitmap2 = getRecyclerViewBitmap((RecyclerView) obj2);
}else if (obj2 instanceof View){
bitmap2 = getViewBitmap((View) obj2);
}
Bitmap all = stitchingBitmap(bitmap,bitmap2);
if (null != all){
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(context.getContentResolver(), all, null,null));
filterShare(shareIntent,type,mTitle,mText,uri);
}else {
Toast.makeText(context, "截屏失败,请重试...", Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
progressDialog.cancel();
}
});
}
}).start();
}
public void shareImage(final int drawable){
new Thread(new Runnable() {
@Override
public void run() {
type = "image/*";
Intent shareIntent;
shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType(type);
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawable);
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, null,null));
filterShare(shareIntent,type,mTitle,mText,uri);
}
}).start();
}
public void shareText() {
new Thread(new Runnable() {
@Override
public void run() {
type = "text/plain";
Intent shareIntent;
shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType(type);
filterShare(shareIntent,type,mTitle,mText,null);
}
}).start();
}
public void shareText(final String Text) {
new Thread(new Runnable() {
@Override
public void run() {
type = "text/plain";
Intent shareIntent;
shareIntent = new Intent(Intent.ACTION_SEND);
String resultText = "";
if (null != Text){
resultText = Text;
}else {
resultText = mText;
}
filterShare(shareIntent,type,mTitle,resultText,null);
}
}).start();
}
private void filterShare(Intent shareIntent, String type, String title, String text, Uri uri) {
initAppSimplePackageName();
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resInfo = pm.queryIntentActivities(shareIntent, 0);
if (!resInfo.isEmpty()){
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
for (int j=0;j<needShareAppPlatform_SimplePackageNames.length;j++){
String SimplePackageName = needShareAppPlatform_SimplePackageNames[j];
if (packageName.contains(SimplePackageName)){
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType(type);
if (null != title){
intent.putExtra(Intent.EXTRA_SUBJECT, title);
}
if (null != text) {
intent.putExtra(Intent.EXTRA_TEXT, text);
}
if (null != uri) {
intent.putExtra(Intent.EXTRA_STREAM, uri);
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
}
if (intentList.size() == 0){
Toast.makeText(context, "不存在分享组件...", Toast.LENGTH_SHORT).show();
return;
}
Intent openInChooser = Intent.createChooser(intentList.remove(0), "请选择分享平台");
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
context.startActivity(openInChooser);
}else {
Toast.makeText(context, "不存在分享组件...", Toast.LENGTH_SHORT).show();
}
}
private static Bitmap getBitmapFromRootView(View view) {
Bitmap bmp = null;
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
bmp= Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
return bmp;
}
public static int getStatusH(Activity ctx) {
Rect s = new Rect();
ctx.getWindow().getDecorView().getWindowVisibleDisplayFrame(s);
return s.top;
}
public static Bitmap getActivityBitmap(Activity ctx) {
View view = ctx.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bp = Bitmap.createBitmap(view.getDrawingCache(), 0, 0, view.getMeasuredWidth(),
view.getMeasuredHeight());
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
return bp;
}
public static Bitmap getViewBitmap(View v) {
if (null == v) {
return null;
}
v.setDrawingCacheEnabled(true);
v.buildDrawingCache();
if (Build.VERSION.SDK_INT >= 11) {
v.measure(View.MeasureSpec.makeMeasureSpec(v.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(v.getHeight(), View.MeasureSpec.EXACTLY));
v.layout((int) v.getX(), (int) v.getY(), (int) v.getX() + v.getMeasuredWidth(), (int) v.getY() + v.getMeasuredHeight());
} else {
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
}
Bitmap b = Bitmap.createBitmap(v.getDrawingCache(), 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.setDrawingCacheEnabled(false);
v.destroyDrawingCache();
return b;
}
public static Bitmap getScrollViewBitmap(ScrollView scrollView) {
int h = 0;
Bitmap bitmap = null;
for (int i = 0; i < scrollView.getChildCount(); 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;
}
public static Bitmap getListViewBitmap(ListView listview) {
ListAdapter adapter = listview.getAdapter();
int itemscount = adapter.getCount();
int allitemsheight = 0;
List<Bitmap> bmps = new ArrayList<Bitmap>();
for (int i = 0; i < itemscount; i++) {
View childView = adapter.getView(i, null, listview);
childView.measure(View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
childView.setDrawingCacheEnabled(true);
childView.buildDrawingCache();
bmps.add(childView.getDrawingCache());
allitemsheight += childView.getMeasuredHeight();
}
Bitmap bigbitmap = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);
Canvas bigcanvas = new Canvas(bigbitmap);
Paint paint = new Paint();
int iHeight = 0;
for (int i = 0; i < bmps.size(); i++) {
Bitmap bmp = bmps.get(i);
bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
iHeight += bmp.getHeight();
bmp.recycle();
bmp = null;
}
return bigbitmap;
}
public static Bitmap getRecyclerViewBitmap(RecyclerView view) {
RecyclerView.Adapter adapter = view.getAdapter();
Bitmap bigBitmap = null;
if (adapter != null) {
int size = adapter.getItemCount();
int height = 0;
Paint paint = new Paint();
int iHeight = 0;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize);
for (int i = 0; i < size; i++) {
RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
adapter.onBindViewHolder(holder, i);
holder.itemView.measure(
View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(),
holder.itemView.getMeasuredHeight());
holder.itemView.setDrawingCacheEnabled(true);
holder.itemView.buildDrawingCache();
Bitmap drawingCache = holder.itemView.getDrawingCache();
if (drawingCache != null) {
bitmaCache.put(String.valueOf(i), drawingCache);
}
height += holder.itemView.getMeasuredHeight();
}
bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
Canvas bigCanvas = new Canvas(bigBitmap);
Drawable lBackground = view.getBackground();
if (lBackground instanceof ColorDrawable) {
ColorDrawable lColorDrawable = (ColorDrawable) lBackground;
int lColor = lColorDrawable.getColor();
bigCanvas.drawColor(lColor);
}
for (int i = 0; i < size; i++) {
Bitmap bitmap = bitmaCache.get(String.valueOf(i));
bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);
iHeight += bitmap.getHeight();
bitmap.recycle();
}
}
return bigBitmap;
}
// private Bitmap stitchingBitmap(Bitmap first, Bitmap second) {//拼接
// int width = first.getWidth() + second.getWidth();
// int height = Math.max(first.getHeight(), second.getHeight());
// Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// Canvas canvas = new Canvas(result);
// canvas.drawBitmap(first, 0, 0, null);
// canvas.drawBitmap(second, first.getWidth(), 0, null);
// return result;
// }
// private Bitmap stitchingBitmap(Bitmap first, Bitmap second) {
// int width = Math.max(first.getWidth(),second.getWidth());
// int height = first.getHeight() + second.getHeight();
// Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// Canvas canvas = new Canvas(result);
// canvas.drawBitmap(first, 0, 0, null);
// canvas.drawBitmap(second, first.getHeight(), 0, null);
// return result;
// }
private Bitmap stitchingBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
Bitmap bitmap = Bitmap.createBitmap(firstBitmap.getWidth(), firstBitmap.getHeight(),firstBitmap.getConfig());
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(firstBitmap, new Matrix(), null);
canvas.drawBitmap(secondBitmap, 0, 0, null);
return bitmap;
}
}
使用:
ShareUtils shareUtils = new ShareUtils(this.getContext());
shareUtils.shareScreenShot(ForecastFragment.this.getActivity());
我的业余技术微信公众号:YKJGZH,欢迎大家进入