try {
ShareImageUtils.layoutView(other, 750, 1334);
MyTask task = new MyTask(other);
task.execute();
} catch (Exception e) {
e.printStackTrace();
}
class MyTask extends AsyncTask<String, String, String> {
ConstraintLayout other;
public MyTask(ConstraintLayout other) {
this.other = other;
}
@Override
protected String doInBackground(String... strings) {
String IMAGES_PATH
= getExternalFilesDir(null).getPath()
+ "/Images";
return ShareImageUtils.viewSaveToImage(other, "beisongfenxiang", IMAGES_PATH);
}
@Override
protected void onPostExecute(String path) {
//分享
initRecitePopupWindow(rv_reciteresult, 4, path);
super.onPostExecute(path);
}
}
package com.youjing.yingyudiandu.utils;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.media.ThumbnailUtils;
import android.os.Environment;
import android.view.View;
import com.youjing.yingyudiandu.utils.constant.Constants;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by Administrator on 2018/1/13.
*/
public class ShareImageUtils {
//然后View和其内部的子View都具有了实际大小,也就是完成了布局,相当与添加到了界面上。接着就可以创建位图并在上面绘制了:
public static void layoutView(View v, int width, int height) {
// 整个View的大小 参数是左上角 和右下角的坐标
// v.layout(0, 0, width, height);
int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);
/** 当然,measure完后,并不会实际改变View的尺寸,需要调用View.layout方法去进行布局。
* 按示例调用layout函数后,View的大小将会变成你想要设置成的大小。
*/
v.measure(measuredWidth, measuredHeight);
v.layout(0, 0, width, height);
}
public static String viewSaveToImage(View view, String child,String IMAGES_PATH) {
// Log.e("ssh", "a");
/**
* View组件显示的内容可以通过cache机制保存为bitmap
* 我们要获取它的cache先要通过setDrawingCacheEnable方法把cache开启,
* 然后再调用getDrawingCache方法就可 以获得view的cache图片了
* 。buildDrawingCache方法可以不用调用,因为调用getDrawingCache方法时,
* 若果 cache没有建立,系统会自动调用buildDrawingCache方法生成cache。
* 若果要更新cache, 必须要调用destoryDrawingCache方法把旧的cache销毁,才能建立新的。
*/
// 把一个View转换成图片
Bitmap cachebmp = loadBitmapFromView(view);
view.destroyDrawingCache();
return sharePic(cachebmp, child,IMAGES_PATH);
}
private static Bitmap loadBitmapFromView(View v) {
int w = v.getWidth();
int h = v.getHeight();
Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmp);
/** 如果不设置canvas画布为白色,则生成透明 */
// c.drawColor(Color.WHITE);
v.layout(0, 0, w, h);
v.draw(c);
return bmp;
}
//保存在本地并一键分享
private static String sharePic(Bitmap cachebmp, String child,String IMAGES_PATH) {
String filename = IMAGES_PATH + "/" + Constants.FILE_NAME + "/speech/";
File sdRoot = new File(filename);
if (!sdRoot.exists()) {
sdRoot.mkdirs();
}
final File qrImage = new File(sdRoot, child + ".jpg");
if (qrImage.exists()) {
qrImage.delete();
}
try {
qrImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(qrImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (cachebmp == null) {
return "";
}
cachebmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
try {
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
return qrImage.getPath();
}
/**
* 创建缩略图
*/
public static Bitmap createThumbnail(Bitmap source, int width, int height) {
return ThumbnailUtils.extractThumbnail(source, width, height);
}
}