1.写xml文件
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/li_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bc_regenal_bg_five_fff"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_10">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_15"
android:scaleType="fitXY"
android:src="@mipmap/ic_logo_shared" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/dp_15"
android:text="天天有新货 时时抢特惠"
android:textColor="@color/gialen_commonly_888888"
android:textSize="10sp" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/common_dp10"
android:gravity="center"
android:orientation="vertical">
android:id="@+id/iv_goods_photo"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#ffffff"
android:scaleType="fitXY"
android:src="@mipmap/ic_default_logo" />
android:id="@+id/tv_goods_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="@dimen/dp_15"
android:layout_marginRight="@dimen/dp_15"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@color/gialen_major_24201f"
android:textSize="12sp" />
android:id="@+id/recycler_tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥"
android:textColor="@color/gialen_major_212121"
android:textSize="@dimen/common_text_size_l"
android:textStyle="bold" />
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/gialen_major_212121"
android:textSize="@dimen/common_text_size_xxl"
android:textStyle="bold" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_4"
android:text="原价:"
android:textColor="@color/gialen_commonly_888888"
android:textSize="@dimen/common_text_size_l11" />
android:id="@+id/tv_price_oringe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/gialen_commonly_888888"
android:textSize="@dimen/common_text_size_l11" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_4"
android:layout_marginBottom="@dimen/dp_15">
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginLeft="@dimen/dp_15"
android:layout_marginRight="@dimen/dp_15"
android:orientation="vertical">
android:id="@+id/iv_wx_code"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/common_dp5"
android:scaleType="fitXY" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/dp_10"
android:layout_toLeftOf="@+id/image_code"
android:gravity="center_vertical"
android:orientation="horizontal">
android:id="@+id/iv_head"
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="centerCrop" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:orientation="vertical">
android:id="@+id/tv_nick_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#212121"
android:textSize="12sp"
tools:text="朱沙" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="向你推荐了一件宝贝"
android:textColor="#888888"
android:textSize="12sp" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="57dp"
android:layout_marginRight="@dimen/common_dp8"
android:text="长按识别小程序"
android:textColor="@color/gialen_commonly_888888"
android:textSize="@dimen/common_text_size_l11" />
2.赋值
3.设置宽高
private static void layoutView(View v,int width,int height) {
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);
v.measure(measuredWidth, measuredHeight);
v.layout(0,0, v.getMeasuredWidth(), v.getMeasuredHeight());
}
4.生成bitmap
private static Bitmap loadBitmapFromView(View v) {
int w = v.getWidth();
int h = v.getHeight();
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c =new Canvas(bitmap);
c.drawColor(Color.WHITE);
v.layout(0,0, w, h);
v.draw(c);
return bitmap;
}
5.分享到朋友圈
if (userInfo ==null || TextUtils.isEmpty(userInfo.getPhone()) || TextUtils.isEmpty(goodsImgeUrl) || TextUtils.isEmpty(goodsName) || TextUtils.isEmpty(price) || TextUtils.isEmpty(orignPrice) || bitmap ==null) {
Toast.makeText(context,"请先登录,才可以分享!", Toast.LENGTH_LONG).show();
return;
}
View view = context.getLayoutInflater().inflate(R.layout.view_share_poster,null);
ImageView iv_goods_photo = view.findViewById(R.id.iv_goods_photo);
ImageView iv_wx_code = view.findViewById(R.id.iv_wx_code);
TextView tv_goods_name = view.findViewById(R.id.tv_goods_name);
TextView tv_price = view.findViewById(R.id.tv_price);
TextView tv_price_oringe = view.findViewById(R.id.tv_price_oringe);
RecyclerView recycler_tag = view.findViewById(R.id.recycler_tag);
TextView tv_nick_name = view.findViewById(R.id.tv_nick_name);
ImageView iv_head = view.findViewById(R.id.iv_head);
String phone = userInfo.getPhone();
String nickname = userInfo.getNickname();
if (TextUtils.isEmpty(nickname)) {
tv_nick_name.setText(phone);
}else {
tv_nick_name.setText(nickname);
}
ImageLoader.setImageResource(context, userInfo.getUserHeadPic(),new GlideCircleTransform(),true, iv_head);
tv_goods_name.setText(goodsName);
tv_price_oringe.setText(String.format(context.getResources().getString(R.string.price), orignPrice));
tv_price.setText(price);
TagUtils.setTagTwo(context, recycler_tag, tag,0);
tv_price_oringe.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
WindowManager manager = context.getWindowManager();
DisplayMetrics metrics =new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels - context.getResources().getDimensionPixelOffset(R.dimen.dp_120);
LinearLayout.LayoutParams params =new LinearLayout.LayoutParams(width, width);
params.gravity = Gravity.CENTER_HORIZONTAL;
iv_goods_photo.setLayoutParams(params);
RequestOptions options =new RequestOptions().priority(Priority.HIGH).diskCacheStrategy(DiskCacheStrategy.RESOURCE).placeholder(R.mipmap.ic_default_logo).error(R.mipmap.ic_default_logo);
Glide.with(context.getApplicationContext()).load(goodsImgeUrl).apply(options).into(iv_goods_photo);
Glide.with(context.getApplicationContext()).load(bitmap).apply(options).into(iv_wx_code);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
layoutView(view,900,1600);
Bitmap bm =loadBitmapFromView(view);//将view转成bitmap
WXShareUtils.shareImage(context, bm,1);
}
},500);
网友评论