侧滑里简单的切换头像方法-也就是二级采样了

//001
首先先获取到 imagerView 的ID 其次开启相册的管理 使用startActivityForResult

imageView = findViewById(R.id.hand);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent,YIBAI);
            }
        });

//002
重写一个onActivityResult 一进去就开始判断 你的两个 int值

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode==YIBAI&&resultCode==RESULT_OK){
            Uri uri = data.getData();
            //方法2                  方法1
            progressImage(url2Path(uri));
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

方法里调用了两个方法,首先第一个方法的目的在于操作图片
//方法1 :定义一个数组 接收方法里的Data 参数

//操作图片 002
    private String url2Path(Uri uri){
        //定义数组
        String[] proj ={MediaStore.Images.Media.DATA};
        //采用Cursorloader
        CursorLoader loader = new CursorLoader(this, uri, proj, null, null, null);
        //得到后台加载
        Cursor cursor = loader.loadInBackground();
        int columnIndexOrThrow = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(columnIndexOrThrow);

    }

//方法2:
当你对图片一顿操作之后 就要开始赋值了
这里的 size 是我自定义的 东西 在 Valus 定义出来的dimen.xml 文件

 //001
    private void progressImage(String imagrPath){

        //得到一个大小 size
        int size = getResources().getDimensionPixelSize(R.dimen.IConSize);

        Bitmap bitmap = IconUtil.scaleBitmap(imagrPath, size, size);

        imageView.setImageBitmap(bitmap);
    }

//用来对图片进行采样的工具类

public class IconUtil {
    //二次采样
    public static Bitmap scaleBitmap(String imagePath, int width, int height){
        //先得到 BitmapFactory.options
        BitmapFactory.Options options = new BitmapFactory.Options();

        //02让他只加载边框--说白的就是加载图片的所有信息 就是不加载图片
        options.inJustDecodeBounds=true;

        //03 把信息和图片给这个工厂
        BitmapFactory.decodeFile(imagePath,options);

        //04 我需要 规定一下宽高 所以需要传参宽高过来
        //就是--- 改变好宽高的一个架子
        options.inSampleSize=Math.max(options.outWidth/width,options.outHeight/height);

        //05加载出来这个图片
        options.inJustDecodeBounds = false;

        //06 把图片的路径imagpath 用工厂factory给它 加上之前定义的那个规则options
        return BitmapFactory.decodeFile(imagePath,options);

    }

猜你喜欢

转载自blog.csdn.net/qq_41972756/article/details/84145711