图片多选,压缩

最近公司做的项目要求可以多选图片,而且显示的速度要快,上传的时候要压缩。搜了很多,终于找到一个框架。挺好用的,感谢分享者郭孝星。

代码链接:https://download.csdn.net/download/qq_30299243/11485272

 

1,依赖:

//以下是添加的图片选择的依赖
implementation "com.android.support:multidex:${multiDexVersion}"
implementation "com.android.support.constraint:constraint-layout:${constraintLayoutVersion}"

implementation ("com.github.bumptech.glide:glide:${glideVersion}") {
    exclude group:'com.android.support'
}
implementation "com.github.guoxiaoxing:phoenix:$rootProject.ext.phoenixVerson"
implementation "com.github.guoxiaoxing:phoenix-compress-picture:$rootProject.ext.phoenixVerson"
implementation "com.github.guoxiaoxing:phoenix-compress-video:$rootProject.ext.phoenixVerson"

2,项目根目录build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    //根据自己项目的版本添加,如果版本不符,会有提示,直接更改就行
    ext.kotlin_version = '1.2.51'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.novoda:bintray-release:0.8.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenLocal()
        jcenter()
        maven { url 'https://jitpack.io' }
    }

    tasks.withType(Javadoc).all {
        enabled = false
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

ext {
    phoenixVerson = '1.0.15'
    phoenixCoreVersion = '1.0.15'

    //android
    compileSdkVersion = 27
    minSdkVersion = 16
    targetSdkVersion = 27
    versionCode = 1
    versionName = "1.0"
    supportLibraryVersion = "27.1.1"
    constraintLayoutVersion = '1.0.2'
    multiDexVersion = '1.0.3'

    rxJavaVersion = "2.1.6"
    rxAndroidVersion = "2.0.1"
    gsonVersion = '2.8.2'
    glideVersion = '4.6.1'

    junitVersion = '4.12'
    testRunnerVersion = '1.0.1'
    testEspressoVersion = '3.0.1'
}

清单文件

初始化:

package com.pic;

import android.content.Context;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.guoxiaoxing.phoenix.core.listener.ImageLoader;
import com.guoxiaoxing.phoenix.picker.Phoenix;

/**
 * Created by lake
 * 此类的功能:
 */
public class App extends MultiDexApplication {
    private static Context context;//全局上下文

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        Phoenix.config()
                .imageLoader(new ImageLoader() {
                    @Override
                    public void loadImage(Context context, ImageView imageView, String imagePath, int type) {
                        Glide.with(context)
                                .load(imagePath)
                                .into(imageView);
                    }
                });

    }

    public static Context getContext() {
        return context;
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

}

3,布局文件




    

4,activity:

package com.pic;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.guoxiaoxing.phoenix.core.PhoenixOption;
import com.guoxiaoxing.phoenix.core.model.MediaEntity;
import com.guoxiaoxing.phoenix.core.model.MimeType;
import com.guoxiaoxing.phoenix.picker.Phoenix;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView rv_selected_image;
    private Button btn_select;

    private MyAdaptor adapter;
    private int REQUEST_CODE = 0x000111;
    //存放数据的集合
    private List mData = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {
        rv_selected_image = findViewById(R.id.rv_selected_image);
        rv_selected_image.setLayoutManager(new LinearLayoutManager(this));
        rv_selected_image.setHasFixedSize(true);


        btn_select = findViewById(R.id.btn_select);
        btn_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choosePic();
            }
        });
    }

    private void choosePic() {
        Phoenix.with()
                .theme(PhoenixOption.THEME_DEFAULT)// 主题
                .fileType(MimeType.ofAll())//显示的文件类型图片、视频、图片和视频
                .maxPickNumber(10)// 最大选择数量
                .minPickNumber(0)// 最小选择数量
                .spanCount(4)// 每行显示个数
                .enablePreview(true)// 是否开启预览
                .enableCamera(true)// 是否开启拍照
                .enableAnimation(true)// 选择界面图片点击效果
                .enableCompress(true)// 是否开启压缩
                .compressPictureFilterSize(1024)//多少kb以下的图片不压缩
                .compressVideoFilterSize(2018)//多少kb以下的视频不压缩
                .thumbnailHeight(160)// 选择界面图片高度
                .thumbnailWidth(160)// 选择界面图片宽度
                .enableClickSound(false)// 是否开启点击声音
//                .pickedMediaList(mMediaAdapter.getData())// 已选图片数据
                .videoFilterTime(30)//显示多少秒以内的视频
                .mediaFilterSize(10000)//显示多少kb以下的图片/视频,默认为0,表示不限制
                .start(this, PhoenixOption.TYPE_PICK_MEDIA, REQUEST_CODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            //返回的数据
            List result = Phoenix.result(data);

            for (int i = 0; i < result.size(); i++) {
                mData.add(new ObjectModel(result.get(i).getFinalPath()));
            }
            adapter = new MyAdaptor(this, mData);
            rv_selected_image.setAdapter(adapter);
        }

    }

}

adapter:

package com.pic;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by lake
 * 此类的功能:
 */
public class MyAdaptor extends RecyclerView.Adapter {
    private List list;

    private Context context;

    public MyAdaptor(Context context, List list) {
        this.list = list;
        this.context = context;
    }


    @Override
    public mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_view, parent, false);
        return new mViewHolder(view);
    }

    @Override
    public void onBindViewHolder(mViewHolder holder, final int position) {
        Glide.with(context).load(list.get(position).getPic()).into(holder.pic);
//        Picasso.with(context).load("file://" + list.get(position).getPic()).error(R.drawable.ic_launcher_background).into(holder.pic);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class mViewHolder extends RecyclerView.ViewHolder {
        ImageView pic;

        mViewHolder(View itemView) {
            super(itemView);
            this.pic = itemView.findViewById(R.id.pic);
        }
    }

}

 

model:

package com.pic;

/**
 * Created by lake
 * 此类的功能:
 */
public class ObjectModel {
    String pic;

    public ObjectModel(String pic) {
        this.pic = pic;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }
}

item




    

你可能感兴趣的:(安卓)