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(); // } } } }