在子线程中进行二次采样

个人整理思路:

				1.获取相册中图片,得到图片的路径
                 2.获取保存图片设置的边框的宽高,
                        与原有宽高计算出缩放比例----第一次采样只读取图片,不加载到内存
                 3.将图片加载到内存中-----第二次采样
                 4.得到bitmap,放入到指定边框

基类:

	package com.example.twosampling;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
		 //抽象的
public abstract class BaseActivity extends AppCompatActivity {
   @Override
	   public void onCreate( @Nullable Bundle savedInstanceState) {
  		  super.onCreate(savedInstanceState);
 			   //设置内容视图
 			   setContentView(getLayoutResId());
		    //初始化view
 			   initView();
 		   //初始化数据
  		  initData();

  }

    //初始化内容视图
protected abstract int getLayoutResId();
//初始化视图
    protected abstract void initView();
   // 初始化数据
    protected abstract void initData();

}

MainActivity

package com.example.twosampling;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.content.CursorLoader;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends BaseActivity {
//定义变量
private Button choose,produce;
private EditText edit_wide,edit_high;
private ImageView image;
private Uri uri;

@Override
protected int getLayoutResId() {
    return R.layout.activity_main;
}

@Override   //视图
protected void initView() {
    //获取资源ID
    choose=findViewById(R.id.choose);
    produce=findViewById(R.id.produce);
    edit_wide=findViewById(R.id.edit_wide);
    edit_high=findViewById(R.id.edit_high);
    image=findViewById(R.id.image);

}

@Override       //数据加载
protected void initData() {
    //选择按钮点击获取图片
    choose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //打开系统相册
            Intent it=new Intent(Intent.ACTION_PICK);
            //设置类型
            it.setType("image/*");
            //回传值
            startActivityForResult(it,100);
        }
    });
    //二次采样
    produce.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //得到输入框的值
            String w = edit_wide.getText().toString();
            String h = edit_high.getText().toString();
            //得到真实的路径
            String url = getUrl(uri);
            NetUtile.getInstance().getBitmap2(url, Integer.parseInt(w), Integer.parseInt(h), new NetUtile.CallBack<Bitmap>() {
                @Override
                public void onsuccess(Bitmap bitmap) {
                    image.setImageBitmap(bitmap);
                }
            });
        }
    });

}
//获取文件对应的真实路径----固定格式
private String getUrl(Uri contentUri ){
    String[] pr = {MediaStore.Images.Media.DATA};
    CursorLoader loader = new CursorLoader(this, contentUri, pr, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(index);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==100&&resultCode==RESULT_OK){
      uri = data.getData();//得到图片的uri------二次采样用到图片的路径
    }
}

}

工具类

package com.example.twosampling;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

public class NetUtile {
private static NetUtile instance;
private NetUtile(){

}
public static NetUtile getInstance(){
    if (instance==null){
        instance=new NetUtile();
    }
    return instance;
}

//图片的二次采样               //要加载的图片路径   控件中图片的宽、高
    public Bitmap getBitmap(String filePath,int destWidth,int destHeight){
        //第一次************
        BitmapFactory.Options options = new BitmapFactory.Options();
       //指定只解析bitmap的宽高信息,只读取图片,不加载到内存
       options.inJustDecodeBounds=true;
        BitmapFactory.decodeFile(filePath,options);
        //获得原图的宽和高
        int outWidth = options.outWidth;
        int outHeight = options.outHeight;
        //定义缩放比例---2的n次方
       options.inSampleSize=Math.max(outWidth/destWidth,outHeight/destHeight);
       //第二次************    图片加载到内存
        options.inJustDecodeBounds=false;
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);

        return bitmap;
    }

  //定义接口

public interface CallBack<Bitmap>{
    void onsuccess(Bitmap bitmap);
}

//子线程
@SuppressLint("StaticFieldLeak")
public void getBitmap2(String filepath, final int destwidth, final int destheight, final  CallBack callBack){    //返回值Bitmap
    new AsyncTask<String, Void, Bitmap>() {
        @Override
        protected Bitmap doInBackground(String... strings) {
            Bitmap bitmap = getBitmap(strings[0], destwidth, destheight);
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            callBack.onsuccess(bitmap);
        }
    }.execute(filepath);//根据图片的路径
}

}

xml布局:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android
xmlns:app=“http://schemas.android.com/apk/res-auto
xmlns:tools=“http://schemas.android.com/tools
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">

扫描二维码关注公众号,回复: 4316960 查看本文章
<TextView
    android:id="@+id/wide"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="宽"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@id/choose"
    android:padding="40dp"
    android:textSize="20dp"
    />
<EditText
    android:id="@+id/edit_wide"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@id/choose"
    app:layout_constraintLeft_toRightOf="@id/wide"
    android:padding="20dp"
    />
<TextView
    android:id="@+id/high"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="高"
    android:textSize="20dp"
    app:layout_constraintLeft_toRightOf="@id/edit_wide"
    app:layout_constraintTop_toBottomOf="@id/choose"
    android:padding="40dp"
    />
<EditText
    android:id="@+id/edit_high"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="20dp"
    app:layout_constraintLeft_toRightOf="@id/high"
    app:layout_constraintTop_toBottomOf="@id/choose"
    />

<Button
    android:id="@+id/produce"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="生成"
    app:layout_constraintTop_toBottomOf="@id/edit_wide"
    android:layout_margin="20dp"
    />
<ImageView
    android:id="@+id/image"
    android:layout_width="150dp"
    android:layout_height="150dp"
    app:layout_constraintTop_toBottomOf="@id/produce"
    android:layout_margin="40dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    />

</android.support.constraint.ConstraintLayout>

权限----读和写:

猜你喜欢

转载自blog.csdn.net/weixin_43258668/article/details/84076149