app/src/main/java/cn/flightfeather/thirdapp/task/SetImageTask.java
@@ -1,98 +1,41 @@
package cn.flightfeather.thirdapp.task;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.util.Util;
import java.io.File;
import cn.flightfeather.thirdapp.R;
import cn.flightfeather.thirdapp.module.inspectioninfo.ProblemChangeDetailActivity;
/**
 * Created by note_ff_1602 on 2017/10/26.
 * 设置图片的task
 *
 */
public class SetImageTask extends AsyncTask {
// FIXME: 2020/11/12 原来的实现方式可能会导致oom,因此修改为使用Glide来实现图片的显示。
public class SetImageTask {
    private File file;
    private ImageView miv_photo;
    private ProblemChangeDetailActivity activity;
    private Activity activity;
    public SetImageTask(File file, ImageView miv_photo, Activity activity) {
        super();
        this.file = file;
        this.miv_photo = miv_photo;
        try {
            this.activity = (ProblemChangeDetailActivity) activity;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public SetImageTask(File file, ImageView miv_photo) {
        super();
        this.file = file;
        this.miv_photo = miv_photo;
    }
    //压缩前先把图片设置为等待
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        miv_photo.setImageResource(R.drawable.icon_add_photo_waite);
    }
    //后台压缩图片
    @Override
    protected Object doInBackground(Object[] params) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        if (bm == null) return null;
        //压缩缩略图
        Matrix matrix = new Matrix();
        matrix.setScale(0.12f, 0.12f);
        //压缩缩略图
        Bitmap bmSmall = null;
        int normalSize = 100;
        int width,height =0;
        if (bm.getWidth()>=bm.getHeight()){
            width = normalSize;
            height = normalSize*bm.getHeight()/bm.getWidth();
        }else {
            height =normalSize;
            width = normalSize*bm.getWidth()/bm.getHeight();
        }
        if (bm!=null){
            bmSmall= Bitmap.createScaledBitmap(bm,width,height,true);
//            bmSmall= Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        this.activity = activity;
        }
        return bmSmall;
    public void execute() {
        if (Util.isOnMainThread() && activity != null && !activity.isDestroyed()) {
            Glide.with(miv_photo)
                    .load(file)
                    .placeholder(R.drawable.icon_add_photo_waite)
                    .into(miv_photo);
    }
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        Bitmap bmSmall = (Bitmap) o;
        if (bmSmall!=null){
            miv_photo.setImageBitmap(bmSmall);
//            if (activity != null) {
//                activity.postponeEnterTransition();
//            }
        }
    }