需求背景 : 我们的产品希望把主app中的页面分享到小程序中 , 大家都知道native分享到小程序是需要提供一张背景图的 , 这张图片可以服务端提供url也可以客户端自己写 . 我这边就是自己写 , 没办法.
demo地址:https://download.csdn.net/download/rely_on_yourself/10600942
看一下效果图:
主要参考来源:https://blog.csdn.net/a450479378/article/details/53081814
代码:
package com.example.li_yx.bitmapdemo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
public enum ShareBitmapUtil {
INSTANCE;
private TextView mCtsShareFlightNoTv;
private TextView mCtsShareFlihgtDeparAirportTv;
private TextView mCtsShareFlihgtArrivalAirportTv;
private TextView mCtsShareFlihgtDeparTimeTv;
private TextView mCtsShareFlihgtArrivalTimeTv;
public Bitmap getPic(Activity activity, ShareModel shareModel) {
DisplayMetrics metric = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
// int width = metric.widthPixels; // 屏幕宽度(像素)
// int height = metric.heightPixels; // 屏幕高度(像素)
View view = LayoutInflater.from(activity).inflate(R.layout.flight_share_bg, null, false);
mCtsShareFlightNoTv = view.findViewById(R.id.ctsShareFlightNoTv);
mCtsShareFlihgtDeparAirportTv = view.findViewById(R.id.ctsShareFlihgtDeparAirportTv);
mCtsShareFlihgtArrivalAirportTv = view.findViewById(R.id.ctsShareFlihgtArrivalAirportTv);
mCtsShareFlihgtDeparTimeTv = view.findViewById(R.id.ctsShareFlihgtDeparTimeTv);
mCtsShareFlihgtArrivalTimeTv = view.findViewById(R.id.ctsShareFlihgtArrivalTimeTv);
if (shareModel != null) {
mCtsShareFlightNoTv.setText(shareModel.getFlightNo());
mCtsShareFlihgtDeparAirportTv.setText(shareModel.getDepartureCountry());
mCtsShareFlihgtArrivalAirportTv.setText(shareModel.getArrivalCountry());
mCtsShareFlihgtDeparTimeTv.setText(shareModel.getDepartureTime());
mCtsShareFlihgtArrivalTimeTv.setText(shareModel.getArrivalTime());
}
// layoutView(view, width, width);
/**原文是采用的屏幕的宽度和高度 用在我这里不合适 因为我要的是固定大小的bitmap 所以固定写死了宽高*/
layoutView(view, 820, 656);
Bitmap cachebmp = loadBitmapFromView(view);
return cachebmp;
}
//然后View和其内部的子View都具有了实际大小,也就是完成了布局,相当与添加到了界面上。接着就可以创建位图并在上面绘制了:
private void layoutView(View view, int width, int height) {
// 整个View的大小 参数是左上角 和右下角的坐标
view.layout(0, 0, width, height);
int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(10000, View.MeasureSpec.AT_MOST);
/** 当然,measure完后,并不会实际改变View的尺寸,需要调用View.layout方法去进行布局。
* 按示例调用layout函数后,View的大小将会变成你想要设置成的大小。*/
view.measure(measuredWidth, measuredHeight);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
}
private Bitmap loadBitmapFromView(View view) {
int w = view.getWidth();
int h = view.getHeight();
Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawColor(Color.WHITE);
/** 如果不设置canvas画布为白色,则生成透明 */
view.layout(0, 0, w, h);
view.draw(canvas);
return bmp;
}
}