需求
有時候可能會有類似分享資產(chǎn)楼誓、戰(zhàn)績這樣的需求,產(chǎn)品需要你生成一張圖片抢肛,然后在上面某個位置加上App的二維碼狼钮,以達(dá)到引流的目的碳柱。而這個生成的圖片也許并不和當(dāng)前頁面完全一樣,這時候就需要我們單獨去進行拼接生成熬芜。
實現(xiàn)
先來模擬一個界面
monijiemian.png
這是顯示的界面莲镣,但是實際生成圖片的時候可能會讓你再加一點別的信息上去,這時候我們可以單獨定義一個類來做這件事情涎拉。
public class ShareView extends FrameLayout {
private final int IMAGE_WIDTH = 720;
private final int IMAGE_HEIGHT = 1280;
private TextView tvInfo;
public ShareView(@NonNull Context context) {
super(context);
init();
}
private void init() {
View layout = View.inflate(getContext(), R.layout.share_view_layout, this);
tvInfo = (TextView) layout.findViewById(R.id.tv_info);
}
/**
* 設(shè)置相關(guān)信息
*
* @param info
*/
public void setInfo(String info) {
tvInfo.setText(info);
}
/**
* 生成圖片
*
* @return
*/
public Bitmap createImage() {
//由于直接new出來的view是不會走測量瑞侮、布局的圆、繪制的方法的,所以需要我們手動去調(diào)這些方法半火,不然生成的圖片就是黑色的越妈。
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(IMAGE_WIDTH, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(IMAGE_HEIGHT, MeasureSpec.EXACTLY);
measure(widthMeasureSpec, heightMeasureSpec);
layout(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
Bitmap bitmap = Bitmap.createBitmap(IMAGE_WIDTH, IMAGE_HEIGHT, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
draw(canvas);
return bitmap;
}
}
定義了一個setInfo方法,可以從外部傳入相關(guān)的信息钮糖,createImage方法用于生成最終的圖片梅掠,由于直接new出來的view是不會走測量、布局店归、繪制的方法的阎抒,所以需要我們手動去調(diào)這些方法,不然生成的圖片就是黑色的消痛。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="720px"
android:layout_height="1280px"
android:orientation="vertical"
android:background="#ffffff">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/mayday" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20px"
android:gravity="center_horizontal"
android:text="我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容我是內(nèi)容"
android:textSize="40px" />
<TextView
android:id="@+id/tv_info"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="20px"
android:layout_marginTop="20px"
android:layout_weight="1"
android:gravity="center_horizontal"
android:textSize="40px" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="150px"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:gravity="center"
android:text="xxxApp就是nb"
android:textColor="#ffffff"
android:textSize="50px" />
<TextView
android:layout_width="150px"
android:layout_height="150px"
android:background="@android:color/darker_gray"
android:gravity="center"
android:text="我是二維碼"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
在布局里面我們可以完全用ui給的px尺寸來布局且叁,這樣生成的圖片就和效果圖幾乎完全一樣了。
最后來看下調(diào)用:
public void createShareImage(View view) {
ShareView shareView = new ShareView(MainActivity.this);
shareView.setInfo("其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息其他信息");
final Bitmap image = shareView.createImage();
final String path = saveImage(image);
Log.e("xxx", path);
if (image != null && !image.isRecycled()) {
image.recycle();
}
}
/**
* 保存bitmap到本地
*
* @param bitmap
* @return
*/
private String saveImage(Bitmap bitmap) {
File path = getCacheDir();
String fileName = "shareImage.png";
File file = new File(path, fileName);
if (file.exists()) {
file.delete();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return file.getAbsolutePath();
}
像QQ這樣的平臺分享是不支持直接分享bitmap的秩伞,所以需要我們先把bitmap保存到本地逞带,再分享本地的路徑地址。
可以看到打印出來最終的圖片地址(不同手機可能會不一樣):/data/data/com.vice.viewshotproject/cache/shareImage.png纱新,用adb命令把它拷貝出來掰担。
//root權(quán)限
adb root
//拷貝到桌面
adb pull /data/data/com.vice.viewshotproject/cache/shareImage.png ~/Desktop/
看下最終生成的圖片:
shareImage.png
尺寸剛好720*1280。