Skip to content

Commit

Permalink
增加了截图的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ihewro committed Aug 14, 2018
1 parent 4bb1150 commit 80577fa
Show file tree
Hide file tree
Showing 10 changed files with 495 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,41 @@
*/
public class MyDataBase {



/**
* 把一个表情信息加入到数据库
* @param expression
* @return
*/
public static boolean addExpressionRecord(Expression expression,File source){

byte[] bytes = fileToCompressedBytes(source);
if (bytes!=null){
return false;
}else {
return addExpressionRecord(expression,bytes);
}
}

private static byte[] fileToCompressedBytes(File source){
//先进行图片压缩,避免数据太大,导致读取问题
File compressToFile = null;
File compressToFile;
compressToFile = FileUtil.returnCompressExp(source);
ALog.d("压缩后的路径" + compressToFile.getAbsolutePath() + "大小" + compressToFile.length());
byte[] bytes = FileUtil.fileToBytes(compressToFile);
ALog.d("文件大小为" + bytes.length);
if (bytes.length>2060826){
return false;
return null;
}else {
//因为compressToFile 和 source是同一个文件,所以先判断下,再决定是否删除
if (compressToFile.exists() && !Objects.equals(compressToFile.getAbsolutePath(), source.getAbsolutePath())){
compressToFile.delete();
}
return addExpressionRecord(expression,bytes);
return bytes;
}

}



public static boolean addExpressionRecord(Expression expression,byte[] source) {
//1. 检查有没有表情对应的目录
List<ExpressionFolder> expressionFolderList = LitePal.where("name = ? and exist = ?",expression.getFolderName(), String.valueOf(1)).find(ExpressionFolder.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import com.ihewro.android_expression_package.fragment.ExpressionContentFragment;
import com.ihewro.android_expression_package.http.HttpUtil;
import com.ihewro.android_expression_package.task.CheckUpdateTask;
import com.ihewro.android_expression_package.task.GenerateScreenshotTask;
import com.ihewro.android_expression_package.task.RecoverDataTask;
import com.ihewro.android_expression_package.task.RemoveCacheTask;
import com.ihewro.android_expression_package.task.GetExpFolderTask;
Expand Down Expand Up @@ -691,13 +692,26 @@ public void onLoadFailed(@Nullable Drawable errorDrawable) {
topImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Expression expression = new Expression(2, oneDetailLists.getDate().substring(0, 10) + (currentItem) + ".jpg", oneDetailList.get(currentItem).getImgUrl(), "头图");
ExpImageDialog expImageDialog = new ExpImageDialog.Builder(MainActivity.this)
//生成截图
final Expression expression = new Expression(3, oneDetailLists.getDate().substring(0, 10) + (currentItem) + ".jpg", oneDetailList.get(currentItem).getImgUrl(), "头图");
final ExpImageDialog expImageDialog = new ExpImageDialog.Builder(MainActivity.this)
.setContext(MainActivity.this, null,3)
.build();
expImageDialog.setImageData(expression);
expImageDialog.show();

//判断是否已经生成过了
File file = new File(GlobalConfig.appDirPath + expression.getFolderName() + "/" + expression.getName());
if (file.exists()){
expImageDialog.show();
}else {
new GenerateScreenshotTask(MainActivity.this, oneText.getText().toString(), expression, new TaskListener() {
@Override
public void onFinish(Boolean result) {
expImageDialog.show();
}
}).execute();
}

}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class Expression extends LitePalSupport{

private int id;//主键
private int status;//标志位,图片来源:~~-1 apk内置图片~~ 1 sd卡图片 2 网络图片
private int status;//标志位,图片来源:~~-1 apk内置图片~~ 1 数据库图片 2 网络图片 3 本机图片(数据库中没有存,头图分享卡片就是这种类型)
private String name;//图片名称
private String url;//图片路径或者图片地址
private String folderName;//目录的名称
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.ihewro.android_expression_package.task;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.ALog;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.ihewro.android_expression_package.R;
import com.ihewro.android_expression_package.bean.Expression;
import com.ihewro.android_expression_package.callback.TaskListener;
import com.ihewro.android_expression_package.util.UIUtil;

import java.util.concurrent.ExecutionException;

import es.dmoral.toasty.Toasty;

/**
* <pre>
* author : hewro
* e-mail : [email protected]
* time : 2018/08/12
* desc :
* version: 1.0
* </pre>
*/
public class GenerateScreenshotTask extends AsyncTask<Void,Void,Boolean> {

private Activity activity;
private String content;
private MaterialDialog dialog;
private TaskListener listener;
private Expression expression;
private Bitmap.Config[] configs
= {Bitmap.Config.ALPHA_8, Bitmap.Config.ARGB_4444, Bitmap.Config.ARGB_8888, Bitmap.Config.RGB_565};

public GenerateScreenshotTask(Activity activity, String content ,Expression expression, TaskListener listener) {
this.activity = activity;
this.content = content;
this.listener = listener;
this.expression = expression;
}

@Override
protected void onPreExecute() {
dialog = new MaterialDialog.Builder(activity)
.content("生成分享图片中……")
.progress(true, 0)
.progressIndeterminateStyle(true)
.show();
}

@Override
protected Boolean doInBackground(Void... voids) {
View view = activity.getLayoutInflater().inflate(R.layout.share_one_sreenshot,null);
View v = view.findViewById(R.id.item_view);
ImageView headerImage = view.findViewById(R.id.headerImage);
TextView date = view.findViewById(R.id.dateText);
TextView content = view.findViewById(R.id.content);
ALog.d("名称为" + expression.getName().substring(0,10));
String text = expression.getName().substring(0,10);
//设置布局内容
Drawable imageFile = null;
try {
imageFile = Glide.with(activity).asDrawable()
.apply(RequestOptions.priorityOf(Priority.HIGH).onlyRetrieveFromCache(true))
.load(expression.getUrl())
.submit().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
if(imageFile != null){
headerImage.setImageDrawable(imageFile);
}else {
return false;
}
date.setText(text);
content.setText(this.content);
//生成截图
v.setDrawingCacheEnabled(true);
//measure()实际测量 自己显示在屏幕上的宽高 2个参数,int widthMeasureSpec 和 int heightMeasureSpec表示具体的测量规则。
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//确定View的大小和位置的,然后将其绘制出来
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
//调用getDrawingCache方法就可 以获得view的cache图片
Bitmap bb = Bitmap.createBitmap(v.getDrawingCache());
//系统把原来的cache销毁
v.setDrawingCacheEnabled(false);


// 保存bitmap到sd卡
UIUtil.saveBitmapToSdCard(activity,bb,"头图/" + expression.getName());
return true;
}

@Override
protected void onPostExecute(Boolean aBoolean) {
Toasty.success(activity, "保存成功", Toast.LENGTH_SHORT).show();
dialog.dismiss();
listener.onFinish(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ protected Boolean doInBackground(Expression... expressions) {
}

return result;
}else if (expression.getStatus() == 3){//本机图片
return true;
}else {//未知来源图片
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ public static byte[] fileToBytes(String file) {

return fileToBytes(new File(file));
}
public static byte[] fileToBytes(File file){

public static byte[] fileToBytes(File file){
InputStream is = null;
byte[] bytes = null;
try {
Expand Down Expand Up @@ -249,4 +250,6 @@ public static File returnCompressExp(File file){
return null;
}



}
Loading

0 comments on commit 80577fa

Please sign in to comment.