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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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 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 {
    private File file;
    private ImageView miv_photo;
    private ProblemChangeDetailActivity 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);
        }
 
 
        return bmSmall;
    }
 
 
 
    @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();
//            }
        }
 
    }
 
 
}