Android如何生成图片分享

最近项目有一个需求,是生成一个布局并将其转换成图片,布局里面有文字,有图片,并且分享

重点是要将布局的图片用Glide显示

/**
 * 初始化分享的布局
 */
private void initShare() {

    DisplayMetrics metric = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metric);
    int width = metric.widthPixels;     // 屏幕宽度(像素)
    int height = metric.heightPixels;   // 屏幕高度(像素)
    View view = LayoutInflater.from(this).inflate(R.layout.dialog_expert_home_page_share_wechat_view, null, false);
    //这一步是必须的,要不然找不到布局,会报宽高为0的异常
    layoutView(view, width, height);//去到指定view大小的函数

    //find控件
    final RelativeLayout mExpertShareWeChatLayout = (RelativeLayout)       view.findViewById(R.id.dialog_expert_home_page_share_wechat_layout);
    CircleImageView mExpertShareWeChatHeadImg = (CircleImageView) view.findViewById(R.id.dialog_expert_home_page_share_wechat_head_img);
    OverStriKingTextView mExpertShareWeChatName = (OverStriKingTextView) view.findViewById(R.id.dialog_expert_home_page_share_wechat_name);
    TextView mExpertShareWeChatFirstAptitude = (TextView) view.findViewById(R.id.dialog_expert_home_page_share_wechat_fist_aptitude);
    OverStriKingTextView mExpertShareWeChatDesc = (OverStriKingTextView) view.findViewById(R.id.dialog_expert_home_page_share_wechat_desc);
    ImageView mExpertShareWeChatQrImg = (ImageView) view.findViewById(R.id.dialog_expert_home_page_share_wechat_qr_img);

    //并且显示内容
    mExpertShareWeChatName.setText(mExpertInfoBean.getExpertName());
    mExpertShareWeChatFirstAptitude.setText(mExpertInfoBean.getExpertShareFisrtAptitude());
    mExpertShareWeChatDesc.setText(mExpertInfoBean.getExpertShareDescription());

    //显示头像
    Glide.with(ExpertHomePageActivity.this)
            .load(mExpertInfoBean.getExpertHeadImg())
            .dontAnimate()
            .error(R.drawable.expert_consult_list_head_default_img)
            .placeholder(R.drawable.expert_consult_list_head_default_img)
            .transform(new GlideCircleTransform(ExpertHomePageActivity.this))
            .into(new GlideDrawableImageViewTarget(mExpertShareWeChatHeadImg) {
                @Override
                public void onResourceReady(GlideDrawable resource, GlideAnimation animation) {
                    super.onResourceReady(resource, animation);
                    //加载成功后,创建图片,一定要在Glide加载图片完成后,再创建图片,要不然图片就显示不出来
                    mShareWeChatBitmap = createViewBitmap(mExpertShareWeChatLayout);
                }
            });
}

 

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(height, View.MeasureSpec.AT_MOST);
    /** 当然,measure完后,并不会实际改变View的尺寸,需要调用View.layout方法去进行布局。
     * 按示例调用layout函数后,View的大小将会变成你想要设置成的大小。
     */
    view.measure(measuredWidth, measuredHeight);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
}

 

 

/**
 * 生成一张图片
 *
 * @param v
 * @return
 */
//生成图片
public Bitmap createViewBitmap(View v) {
    //如果上面那个布局不在屏幕上找到的话,这里就会报,宽高为0的异常
    Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    v.draw(canvas);
    return bitmap;
}

你可能感兴趣的:(Android如何生成图片分享)