android 微信分享gif图,android后台动态创建图片并实现微信分享
今天就記錄一下。
先說明一下,之前沒有做過類似的東西,百度了一兩天才知道,說來很慚愧、有點笨,只能這樣說。
在我的腦里只明白,如果要動態創建圖片:
一、就是new 嘛
二、就是LayoutInflater.from()這種。
而微信分享圖片到朋友圈,這種不可能new textview或者Imageview,所以用第二種,LayoutInflater,加載布局的父類引用view,索性就開始干,結果,跳轉到微信的界面圖片直接就是空白圖片,一直以為是沒有把數據加入進去,反復地測試數據是有點,但還是空白圖片,百度了一下,View.getDrawingCache() 只適用于分享的View已經完整展示在用戶的屏幕上,超出屏幕范圍內的內容是不在生成的Bitmap內的。
一、
大家可以看看這個解釋:android后臺通過View生成分享圖片
雖然有點燥,但還是和我經過幾個小時測試得出結論相差不多需要展示在界面才能根據view的寬高:
Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
這里用的ARGB_4444是16位的,可以用ARGB_8888 32位的,為了防止圖片太大,分享不成功,把圖片分辨率弄低一點,展示一下view下生成代碼:
View view = inflater.inflate(R.layout.dialog, null, false);
private void checkAndRequestSavePermission(View view, int type) {
//判斷當前系統的SDK版本是否大于23
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//如果當前申請的權限沒有授權
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//請求權限
requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE}, 1);
} else {//已經授權了就走這條分支
savePic(view, type);
}
} else {
savePic(view, type);
}
}
上面有個權限是,生成圖片需要把圖片保存在手機相冊里面。
private void savePic(final View view, final int type) {
view.post(new Runnable() {
@Override
public void run() {
//獲取view 長寬
int width = view.getWidth();
int height = view.getHeight();
//若傳入的view長或寬為小于等于0,則返回,不生成圖片
if (width <= 0 || height <= 0) {
return;
}
//生成一個ARGB8888的bitmap,寬度和高度為傳入view的寬高
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
//根據bitmap生成一個畫布
Canvas canvas = new Canvas(bitmap);
//注意:這里是解決圖片透明度問題,給底色上白色,不然是透明色背景可能會很難看,若存儲時保存的為png格式的圖,則無需此步驟
canvas.drawColor(Color.WHITE);
view.draw(canvas);
//不清晰,應該是壓縮的厲害,很模糊
// view.setDrawingCacheEnabled(true);
// view.buildDrawingCache();
// view.setBackgroundColor(Color.WHITE);
// bitmap = BitmapUtils.reduce(view.getDrawingCache(),500, 500, true);
//把bitmip保存到手機本地
path = BitmapUtils.saveImage(bitmap, context);
if (TextUtils.isEmpty(path)) {
showMessage("保存圖片失敗");
} else {
//這里是需要實現的圖片地址path是String
}
}
});
}
public static String saveImage(Bitmap bmp, Context context) {
File appDir = new File(Environment.getExternalStorageDirectory(), "自己定義");
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE).setData(Uri.fromFile(file)));//更新相冊廣播
return file.getPath();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
二、
上面這種方法對于已經顯示在屏幕上,是完全沒有問題的,但是對不需要在屏幕上顯示的view而不行,還是這個大神給的啟發:
Android 后臺生成長圖并分享(非長截圖)
這個是先自定義LinearLayout,在里面實現畫布局方法,相當于后臺操作,直接開始。
public class SaveImgLinearLayout extends LinearLayout {
private Listener listener;
private Context context;
// 長圖的寬度,默認為屏幕寬度
private int longPictureWidth;
// 長圖兩邊的間距
private int picMargin;
private View rootView;
// 被認定為長圖的長寬比
private int widthTop = 0;
private int heightTop = 0;
private int widthContent = 0;
private int heightContent = 0;
private int widthBottom = 0;
private int heightBottom = 0;
private ImageView imgTop, imgCenter;
private LinearLayout llBottom;
private GoodsDetail bean;
public interface Listener {
/**
* 生成長圖成功的回調
*
* @param path 長圖路徑
*/
void onSuccess(String path);
/**
* 生成長圖失敗的回調
*/
void onFail();
}
public void setListener(Listener listener) {
this.listener = listener;
}
public SaveImgLinearLayout(Context context, GoodsDetail bean, View view) {
super(context);
this.bean = bean;
this.rootView = view;
init(context);
}
public SaveImgLinearLayout(Context context) {
super(context);
init(context);
}
public SaveImgLinearLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SaveImgLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
this.context = context;
longPictureWidth = ScreenUtils.getScreenWidth(context);
picMargin = 40;
initView();
}
private void initView() {
imgTop = (ImageView) rootView.findViewById(R.id.image_top);
imgCenter = (ImageView) rootView.findViewById(R.id.imageview_center);
llBottom = (LinearLayout) rootView.findViewById(R.id.ll_buttom);
//測量頭、中、底部
layoutView(imgTop);
layoutView(imgCenter);
layoutView(llBottom);
widthTop = imgTop.getMeasuredWidth();
heightTop = imgTop.getMeasuredHeight();
widthContent = imgCenter.getMeasuredWidth();
// 由于高度是可變的,這里需要用post方法算出
imgCenter.post(new Runnable() {
@Override
public void run() {
heightContent = imgCenter.getHeight();
}
});
widthBottom = llBottom.getMeasuredWidth();
heightBottom = llBottom.getMeasuredHeight();
}
/**
* 手動測量view寬高
*/
private void layoutView(View v) {
//獲取屏幕寬高
int width = ScreenUtils.getScreenWidth(context);
int height = ScreenUtils.getScreenHeight(context);
v.layout(0, 0, width, height);
int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
v.measure(measuredWidth, measuredHeight);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
}
public void startDraw() {
// 需要先下載全部需要用到的圖片(用戶頭像、圖片等),下載完成后再進行長圖的繪制操作
downloadAllImage();
}
private void downloadAllImage() {
// 之類根據自己的邏輯進行圖片的下載,此Demo為了簡單,制作一個延時模擬下載過程
new Thread(new Runnable() {
@Override
public void run() {
// 模擬下載圖片的耗時操作,推薦使用:implementation 'com.liulishuo.filedownloader:library:1.7.3'
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 圖片下載完成后,進行view的繪制
// 模擬保存圖片url、路徑的鍵值對
// 開始繪制view
draw();
}
}).start();
}
private void draw() {
// 創建空白畫布
Bitmap.Config config = Bitmap.Config.ARGB_8888;
Bitmap bitmapAll;
// 計算出最終生成的長圖的高度 = 上、中、圖片總高度、下等個個部分加起來
int allBitmapHeight = heightTop + heightContent + heightBottom;
try {
bitmapAll = Bitmap.createBitmap(longPictureWidth, allBitmapHeight, config);
} catch (Exception e) {
e.printStackTrace();
config = Bitmap.Config.RGB_565;
bitmapAll = Bitmap.createBitmap(longPictureWidth, allBitmapHeight, config);
}
Canvas canvas = new Canvas(bitmapAll);
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);
// 繪制top view
if (widthTop <= 0 && heightTop <= 0 && widthContent <= 0 && heightContent <= 0 && widthBottom <= 0 && heightBottom <= 0) {
return;
}
//決定你布局顯示的位置,如果設置為0 ,就會如fragment一樣,都在0.0位置顯示重疊在一起
canvas.drawBitmap(getLinearLayoutBitmap(imgTop, widthTop, heightTop), 0, 0, paint);
canvas.drawBitmap(getLinearLayoutBitmap(imgCenter, widthContent, heightContent), 0, heightTop, paint);
canvas.drawBitmap(getLinearLayoutBitmap(llBottom, widthBottom, heightBottom), 0, heightTop + heightContent, paint);
canvas.save();
// 繪制content view
// canvas.translate(MyDensity.dp2px(context, 20), heightTop);
// staticLayout.draw(canvas);
// 生成最終的文件,并壓縮大小,這里使用的是:implementation
'com.github.nanchen2251:CompressHelper:1.0.5'
//這里可以壓縮,,我沒有壓縮,如果有需要可以壓縮
try {
//把圖片保存在手機底部。
String path = BitmapUtils.saveImage(bitmapAll, context);
if (listener != null) {
listener.onSuccess(path);
}
} catch (Exception e) {
e.printStackTrace();
if (listener != null) {
listener.onFail();
}
}
}
private Bitmap getLinearLayoutBitmap(View imgTop, int w, int h) {
Bitmap originBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(originBitmap);
imgTop.draw(canvas);
return resizeImage(originBitmap, longPictureWidth, h);
}
public Bitmap resizeImage(Bitmap origin, int newWidth, int newHeight) {
if (origin == null) {
return null;
}
int height = origin.getHeight();
int width = origin.getWidth();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
if (!origin.isRecycled()) {
origin.recycle();
}
return newBM;
}
public void removeListener() {
this.listener = null;
}
}
使用時候,直接new? 就行了,,然后調用startDraw()方法,就可以了。
至此,已經完了,希望大家給出意見,有什么更好的方法,提出來。
總結
以上是生活随笔為你收集整理的android 微信分享gif图,android后台动态创建图片并实现微信分享的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 传感器 balance
- 下一篇: android auto answer,