App分享 自定义页面

最近在耍分享功能,其实分享无非就是分享图片,文字,图片加文字,别的不常用,就不提及了。

产品脑洞大开想分享大图、文字描述、发表人。然而又想有个特殊标识是俺们公司分享的,你说屌不屌。

当然设计想象力还是挺牛逼的,最终想出了,分享一张图片,内容包含:大图、文字描述、发表人、二维码、公司logo。

说这么多,不如来张效果图

效果图

实现步骤:

一、XML布局

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
...
</LinearLayout>

这么简单的布局,还要你上代码,阔笑。

二、布局变身–> 图片

关键代码,其它都是布局填充内容而已

1
2
3
4
5
Bitmap result = Bitmap.createBitmap(view.getWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);
c.drawColor(Color.WHITE);
// Draw view to canvas
view.draw(c);

具体代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
*
* @param ctx 上下文
* @param bitmapContent 中间大图
* @param contentStr 二维码中的文字内容
* @param bitmapUserImg 用户头像
* @param userName 用户昵称
* @return
*/
public static Bitmap createShareBitmap(Context ctx, Bitmap bitmapContent, String contentStr,
Bitmap bitmapUserImg, String userName){
View view = LayoutInflater.from(ctx).inflate(R.layout.share_look_layout, null);
((ImageView) view.findViewById(R.id.iv)).setImageBitmap(bitmapContent);
QRCodeUtils qrCodeUtils = QRCodeUtils.getInstance();
Bitmap qrBitmap = qrCodeUtils.generateBitmap(contentStr, 400, 400);
Bitmap resultBitmap = qrCodeUtils.addLogo(qrBitmap, ctx, R.mipmap.ic_launcher);
((ImageView) view.findViewById(R.id.iv_qr)).setImageBitmap(resultBitmap);
((CustomCircleImage) view.findViewById(R.id.iv_userImg)).setImageBitmap(bitmapUserImg);
((TextView) view.findViewById(R.id.tv_userName)).setText(userName);
Point point = getRealScreenSize(ctx);
view.layout(0, 0, point.x, point.y);
view.measure(View.MeasureSpec.makeMeasureSpec(point.x, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(point.y, View.MeasureSpec.AT_MOST));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap result = Bitmap.createBitmap(view.getWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);
c.drawColor(Color.WHITE);
// Draw view to canvas
view.draw(c);
return result;
}
/**
* 获取屏幕分辨率
* @param ctx
* @return
*/
public static Point getRealScreenSize(Context ctx) {
Point screenSize = null;
try {
screenSize = new Point();
WindowManager windowManager = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display defaultDisplay = windowManager.getDefaultDisplay();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
defaultDisplay.getRealSize(screenSize);
} else {
try {
Method mGetRawW = Display.class.getMethod("getRawWidth");
Method mGetRawH = Display.class.getMethod("getRawHeight");
screenSize.set(
(Integer) mGetRawW.invoke(defaultDisplay),
(Integer) mGetRawH.invoke(defaultDisplay)
);
} catch (Exception e) {
screenSize.set(defaultDisplay.getWidth(), defaultDisplay.getHeight());
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return screenSize;
}

如果你分享的内容不满整屏,那么给你画个重点。将布局高度设置为最终测量的高度即可

1
2
3
4
5
view.layout(0, 0, point.x, point.y);
view.measure(View.MeasureSpec.makeMeasureSpec(point.x, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(point.y, View.MeasureSpec.AT_MOST));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap result = Bitmap.createBitmap(view.getWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

对了,差点忘了,文中提及的二维码,对,就是二维码。

那么是怎么生成的呢?

附上二维码生成地址生成二维码

Title - Artist
0:00