1. 程式人生 > >圖片選擇器:GalleryFinal 的簡單使用

圖片選擇器:GalleryFinal 的簡單使用

Git

Demo

步驟

1.新增依賴

//GalleryFinal - 圖片選擇
compile 'cn.finalteam:galleryfinal:1.4.6'
//UIL
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

2.初始化

package com.qr.galleryfinaldemo;

import android.app.Application;

import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import cn.finalteam.galleryfinal.BuildConfig; import cn.finalteam.galleryfinal.CoreConfig; import cn.finalteam.galleryfinal.FunctionConfig; import cn.finalteam.galleryfinal.GalleryFinal; import cn.finalteam.galleryfinal.ImageLoader; import cn.finalteam.galleryfinal.ThemeConfig; /** * ====================== BaseApplication ======================== * 基類 完成一些 初始化 配置操作 * @author SGamble */
public class BaseApplication extends Application { @Override public void onCreate() { super.onCreate(); //========================== GalleryFinal - 圖片選擇 ========================== //初始化 ImageLoader com.nostra13.universalimageloader.core.ImageLoader imageLoader = com.
nostra13.universalimageloader.core.ImageLoader.getInstance(); imageLoader.init(ImageLoaderConfiguration.createDefault(this)); //配置主題 //ThemeConfig.CYAN ThemeConfig theme = new ThemeConfig.Builder() .build(); //配置功能 FunctionConfig functionConfig = new FunctionConfig.Builder() .setEnableCamera(true) .setEnableEdit(true) .setEnableCrop(true) .setEnableRotate(true) .setCropSquare(false) .setEnableRotate(true) .setEnablePreview(true) .build(); //配置imageloader ImageLoader imageloader = new UILImageLoader(); //設定核心配置資訊 CoreConfig coreConfig = new CoreConfig.Builder(this, imageloader, theme) .setDebug(BuildConfig.DEBUG) .setFunctionConfig(functionConfig) .build(); GalleryFinal.init(coreConfig); } }

3.清單檔案

<application
        android:name=".BaseApplication"

4.建立UILImageLoader.java

package com.qr.galleryfinaldemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;

import cn.finalteam.galleryfinal.widget.GFImageView;

/**
 * ====================== UILImageLoader ========================
 *
 * @author SGamble
 */
public class UILImageLoader implements cn.finalteam.galleryfinal.ImageLoader {

    private Bitmap.Config mImageConfig;

    public UILImageLoader() {
        this(Bitmap.Config.RGB_565);
    }

    public UILImageLoader(Bitmap.Config config) {
        this.mImageConfig = config;
    }

    @Override
    public void displayImage(Activity activity, String path, GFImageView imageView, Drawable defaultDrawable, int width, int height) {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheOnDisk(false)
                .cacheInMemory(false)
                .bitmapConfig(mImageConfig)
                .build();
        ImageSize imageSize = new ImageSize(width, height);
        ImageLoader.getInstance().displayImage("file://" + path, new ImageViewAware(imageView), options, imageSize, null, null);
    }

    @Override
    public void clearMemoryCache() {

    }
}

5.使用

package com.qr.galleryfinaldemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.util.List;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;

public class MainActivity extends AppCompatActivity {

    //圖片選擇器
    private final int REQUEST_CODE_GALLERY = 1001;
    private Button btnSelPic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnSelPic = (Button)findViewById(R.id.btnSelPic);
        btnSelPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GalleryFinal.openGallerySingle(REQUEST_CODE_GALLERY, mOnHanlderResultCallback);
            }
        });
    }

    /**
     * 選取圖片後的回撥
     */
    private GalleryFinal.OnHanlderResultCallback mOnHanlderResultCallback = new GalleryFinal.OnHanlderResultCallback() {
        @Override
        public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
            if (resultList != null) {
                Log.e("onHanlderSuccess: ", resultList.get(0).getPhotoPath());
            }
        }

        @Override
        public void onHanlderFailure(int requestCode, String errorMsg) {
            Log.e("onHanlderSuccess: ", errorMsg);
        }
    };
}