Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 安卓開發 第八篇 我的安卓應用架構設計-----圖片選擇以及剪裁

安卓開發 第八篇 我的安卓應用架構設計-----圖片選擇以及剪裁

編輯:關於Android編程

Android開發中遇到要從相冊選擇圖片時,大多數人都會選擇調用Android自帶的相冊,畢竟這樣可以節約時間,又不用自己去處理圖片的問題,不過這樣也會產生一些問題,有些系統自帶的相冊真的是丑到沒朋友,有時調用系統相冊時不時的還可能發生崩潰問題。而我的安卓架構中選擇了自定義相冊的功能,其效果是仿照QQ的圖片選擇樣式,通過dialog展現出來的,還自定義了圖片的剪裁,使用了CropImageView 實現了多種剪裁效果。

圖片選擇的直接輔助類:

/**
 * 圖片選擇輔助類
 * Created by tianlai on 16-4-12.
 */
public class PickImageHelper {
    private static final String TAG = "PickImageHelper";

    private PickType pickType;
    private Type type;

    private Activity activtiy;

    private PicKImageDialog picKImageDialog;
    private CropImageDialog cropImageDialog;

    private String cachePath;

    private OnImageOutputListener onImageOutputListener;

    @Inject
    public PickImageHelper(PicKImageDialog picKImageDialog) {
        this.picKImageDialog = picKImageDialog;
    }


    /**
     * 初始化
     *
     * @param activtiy
     */
    public void initPickImageHelper(final Activity activtiy) {
        this.activtiy = activtiy;

        picKImageDialog.setCancelable(false);

        cachePath = SDCardUtil.getRootPath() + activtiy.getString(R.string.cache_path);


        picKImageDialog.setPickImageListener(new PicKImageDialog.PickImageListener() {
            @Override
            public void onPickFinish(List paths) {

                LogUtil.i(TAG, "已選的圖片---" + paths.toString());

                picKImageDialog.dismiss();

                if (onImageOutputListener != null) {
                    if (pickType == PickType.HEADICON) {
                        onImageOutputListener.outputHeadImage(paths.get(0));
                    } else if (pickType == PickType.MULTIIMAGE) {
                        onImageOutputListener.outputPickedImages(paths);
                    }

                }
            }

            @Override
            public void toCropImage(String path) {
                LogUtil.i(TAG, "要剪裁的圖片---" + path.toString());

                picKImageDialog.dismiss();
                initCropImageDialog(Uri.parse("file://" + path));

            }
        });


    }

    /**
     * 初始化剪裁的對話框
     *
     * @param path
     * @param activtiy
     */
    public void onCaptureResult(final String path, Activity activtiy) {
        this.activtiy = activtiy;

        new AlertDialog.Builder(activtiy)
                .setTitle("提示")
                .setMessage("是否剪裁圖片?")
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dealCropResult(path);
                    }
                }).setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                initCropImageDialog(Uri.parse("file://" + path));
            }
        }).create().show();

    }

    /**
     * 初始化剪裁的對話框
     *
     * @param uri
     */
    private void initCropImageDialog(Uri uri) {
        cropImageDialog = new CropImageDialog(activtiy);
        cropImageDialog.setCancelable(false);
        cropImageDialog.setCachePath(cachePath);

        cropImageDialog.setCropImageListener(new CropImageDialog.CropImageListener() {
            @Override
            public void onPickImage() {
                LogUtil.i(TAG, "---重新選擇圖片---");

                cropImageDialog.dismiss();
                picKImageDialog.setMaxNum(1);
                picKImageDialog.show();
            }

            @Override
            public void onCropSucceed(String path) {
                LogUtil.i(TAG, "剪裁後的圖片路徑---" + path);

                cropImageDialog.dismiss();
                dealCropResult(path);
            }
        });

        cropImageDialog.show(uri, true);
    }

    /**
     * 剪裁結果的處理
     *
     * @param path
     */
    private void dealCropResult(String path) {
        if (onImageOutputListener != null) {
            if (pickType == PickType.HEADICON) {
                onImageOutputListener.outputHeadImage(path);
            } else if (pickType == PickType.MULTIIMAGE) {
                List paths = new ArrayList();
                paths.add(path);
                onImageOutputListener.outputPickedImages(paths);
            }
        }
    }


    /**
     * 選擇頭像
     */
    public void pickHeadImage() {
        this.pickType = PickType.HEADICON;

        picKImageDialog.setMaxNum(1);

        showPickOrCapture();
    }

    /**
     * 選擇圖片
     */
    public void pickImages(int maxNum) {
        this.pickType = PickType.MULTIIMAGE;

        picKImageDialog.setMaxNum(maxNum);

        showPickOrCapture();

    }


    /**
     * 從相冊選擇或者拍照
     */
    private void showPickOrCapture() {
        new AlertDialog.Builder(activtiy)
                .setItems(R.array.pick_image, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:      //拍照
                                type = Type.CAPTURE;
                                if (onImageOutputListener != null) {
                                    onImageOutputListener.takePhoto();
                                }

                                break;
                            case 1:      //圖庫
                                type = Type.PICKIMAGE;

                                picKImageDialog.show();

                                break;
                            case 2:      //取消
                                break;
                        }
                    }
                }).create().show();
    }


    public OnImageOutputListener getOnImageOutputListener() {
        return onImageOutputListener;
    }

    public void setOnImageOutputListener(OnImageOutputListener onImageOutputListener) {
        this.onImageOutputListener = onImageOutputListener;
    }

    public PickType getPickType() {
        return pickType;
    }

    public void setPickType(PickType pickType) {
        this.pickType = pickType;
    }

    //選擇類型,單張頭像還是多張
    public enum PickType {
        HEADICON, MULTIIMAGE
    }

    //選擇類型,拍照還是圖庫
    public enum Type {
        CAPTURE, PICKIMAGE
    }

    public static interface OnImageOutputListener {

        public void takePhoto();

        /**
         * 輸出頭像
         *
         * @param path
         */
        public void outputHeadImage(String path);

        /**
         * 輸出選擇的圖片
         *
         * @param paths
         */
        public void outputPickedImages(List paths);
    }

}

圖片選擇dialog:

/**
 * 圖片選擇彈出對話框
 * 

* Created by tianlai on 16-4-12. */ public class PicKImageDialog extends Dialog { private Context context; private LayoutInflater inflater; private ImageView topHome; private TextView topTitle; private TextView topCenter; private TextView topAction; private GridView gvPictures; private ViewPager vpPictures; private ViewSwitcher vsPictures; private TextView bottomPreview; private TextView bottomCrop; private TextView bottomSure; private ListView lvPaths; private ViewSwitcher vsLayout; private List imagePaths; private Set parentPaths; private ParentPathsAdapter parentPathsAdapter; private PicturesAdapter picturesAdapter; private PicturesPagerAdapter picturesPagerAdapter; private int maxNum; private PickImageListener pickImageListener; public PicKImageDialog(Context context) { super(context, R.style.AppTheme_Dialog); init(context); } public PicKImageDialog(Context context, int theme) { super(context, theme); init(context); } private void init(Context context) { this.context = context; inflater = LayoutInflater.from(context); imagePaths = new ArrayList<>(); parentPaths = new HashSet<>(); picturesAdapter = new PicturesAdapter(context, inflater); parentPathsAdapter = new ParentPathsAdapter(context, inflater); scanPictures(context); } /** * 掃描手機中的圖片 * * @param context */ private void scanPictures(final Context context) { new Thread(new Runnable() { @Override public void run() { Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; ContentResolver mContentResolver = context.getContentResolver(); String[] projections = { MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.SIZE }; String selection = MediaStore.Images.Media.MIME_TYPE + "=?"; String[] selectionArgs = {"image/jpeg"}; String sortOrder = MediaStore.Images.Media.DATE_MODIFIED + " desc"; // 只查詢jpeg和png的圖片 Cursor mCursor = mContentResolver.query(mImageUri, projections, selection, selectionArgs, sortOrder); if (mCursor != null) { ImageCollection imageCollection; while (mCursor.moveToNext()) { imageCollection = new ImageCollection(); // 獲取圖片的uri路徑 String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA)); if (path != null) { File file = new File(path); if (file != null && file.exists()) { // 獲取該圖片的父路徑名 File parentFile = file.getParentFile(); String parentPath = parentFile.getAbsolutePath(); if (parentPaths.contains(parentPath)) { continue; } else { parentPaths.add(parentPath); imageCollection.setRootPath(parentPath); imageCollection.setRootName(parentFile.getName()); File[] files = parentFile.listFiles(); for (File f : files) { String fName = f.getName(); if (fName.endsWith(".jpg") || fName.endsWith(".png")) { imageCollection.addPicture(f.getAbsolutePath()); } } imagePaths.add(imageCollection); } } } } mCursor.close(); } } }).start(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_pickimage); initView(); } /** * 初始化view */ private void initView() { topHome = (ImageView) find(R.id.top_home); topTitle = (TextView) find(R.id.top_title); topCenter = (TextView) find(R.id.top_center); topAction = (TextView) find(R.id.top_action); gvPictures = (GridView) find(R.id.gv_pictures); vpPictures = (ViewPager) find(R.id.vp_pictures); vsPictures = (ViewSwitcher) find(R.id.vs_pictures); bottomPreview = (TextView) find(R.id.bottom_title); bottomCrop = (TextView) find(R.id.bottom_center); bottomSure = (TextView) find(R.id.bottom_action); lvPaths = (ListView) find(R.id.lv_paths); vsLayout = (ViewSwitcher) find(R.id.vs_layout); setListeners(); gvPictures.setAdapter(picturesAdapter); lvPaths.setAdapter(parentPathsAdapter); parentPathsAdapter.setItems(imagePaths); List items = transToFullPath(imagePaths); parentPathsAdapter.addItem(0, new ImageCollection(new HashSet(items), "所有圖片")); picturesAdapter.setItems(items); } /** * 設置監聽器 */ private void setListeners() { //返回 topHome.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); //取消 topAction.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); //預覽 bottomPreview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPreviews(new ArrayList(picturesAdapter.getSelectedPictures())); } }); //剪裁 bottomCrop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (pickImageListener != null) { pickImageListener.toCropImage(picturesAdapter.getCorpPicturePath()); picturesAdapter.clearSelectedPictures(); } } }); //確定 bottomSure.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (pickImageListener != null) { pickImageListener.onPickFinish(new ArrayList(picturesAdapter.getSelectedPictures())); picturesAdapter.clearSelectedPictures(); } } }); picturesAdapter.setOnPictrueSelectListener(new PicturesAdapter.OnPictrueSelectListener() { @Override public void selectedSingleItem(boolean selectedSingleItem) { if (selectedSingleItem) { bottomCrop.setEnabled(true); } else { bottomCrop.setEnabled(false); } } @Override public void hasItemSelected(boolean hasItemSelected, int selectNum) { if (hasItemSelected) { bottomPreview.setEnabled(true); bottomSure.setEnabled(true); bottomSure.setText("確定(" + selectNum + "/" + maxNum + ")"); } else { bottomPreview.setEnabled(false); bottomSure.setEnabled(false); bottomSure.setText("確定"); } } }); //點擊預覽單張圖片 gvPictures.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterViewparent, View view, int position, long id) { List paths = new ArrayList(); String item = picturesAdapter.getItem(position); paths.add(item); showPreviews(paths); //判斷是否可以剪裁 if (bottomCrop.isEnabled() && !picturesAdapter.isSamePicture(item)) { bottomCrop.setEnabled(false); } } }); //點擊查看某個目錄下面的圖片 lvPaths.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterViewparent, View view, int position, long id) { ImageCollection imageCollection = parentPathsAdapter.getItem(position); picturesAdapter.setItems(new ArrayList(imageCollection.getPictures())); topCenter.setText(imageCollection.getRootName()); vsLayout.showNext(); } }); } private void showPreviews(List paths) { PicturesPagerAdapter adapter = new PicturesPagerAdapter(paths, inflater); vpPictures.setAdapter(adapter); vsPictures.showNext(); //隱藏預覽按鈕 bottomPreview.setVisibility(View.GONE); } /** * @param imagePaths * @return */ private List transToFullPath(List imagePaths) { Observable observable = null; if (imagePaths != null) { int size = imagePaths.size(); if (size > 0) { Observable observableMerge; for (int i = 0; i < size; i++) { Set pictures = imagePaths.get(i).getPictures(); if (pictures == null) { continue; } else { observableMerge = Observable.from(pictures); if (observable == null) { observable = observableMerge; } else { observable = Observable.merge(observable, observableMerge); } } } return observable.distinct().toList().toBlocking().single(); } } return Collections.emptyList(); } /** * 初始化dialog * * @param maxNum * @param pickImageListener */ public void initPickDialog(int maxNum, PickImageListener pickImageListener) { this.maxNum = maxNum; this.pickImageListener = pickImageListener; } @Override public void show() { picturesAdapter.setMaxNum(maxNum); super.show(); setDialogWindowAttr(); } /** * 調整dialog的大小 */ public void setDialogWindowAttr() { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.gravity = Gravity.CENTER; lp.width = WindowManager.LayoutParams.MATCH_PARENT;//寬高可設置具體大小 lp.height = WindowManager.LayoutParams.MATCH_PARENT; getWindow().setAttributes(lp); } @Override public void onBackPressed() { super.onBackPressed(); if (lvPaths.isShown()) { dismiss(); } else if (vpPictures.isShown()) { vsPictures.showNext(); //顯示預覽按鈕 bottomPreview.setVisibility(View.VISIBLE); //判斷是否可以剪裁 if (!bottomCrop.isEnabled() && picturesAdapter.isSelectedSingleItem()) { bottomCrop.setEnabled(true); } } else { vsLayout.showNext(); } } public int getMaxNum() { return maxNum; } public void setMaxNum(int maxNum) { this.maxNum = maxNum; } public PickImageListener getPickImageListener() { return pickImageListener; } public void setPickImageListener(PickImageListener pickImageListener) { this.pickImageListener = pickImageListener; } /** * 獲取view * * @param viewId * @return */ public View find(int viewId) { return findViewById(viewId); } /** * 獲取view * * @param viewId * @return */ public View find(View parent, int viewId) { return parent.findViewById(viewId); } public static interface PickImageListener { /** * 選擇圖片完成 */ public void onPickFinish(List paths); /** * 剪裁圖片 */ public void toCropImage(String path); } }

圖片的封裝對象:

/**
 * 封裝一個目錄及其目錄下的圖片文件的路徑
 * Created by tianlai on 16-4-12.
 */
public class ImageCollection {
    private String rootPath;
    private String rootName;
    private Set pictures;

    public ImageCollection() {
    }

    public ImageCollection(Set pictures, String rootName) {
        this.pictures = pictures;
        this.rootName = rootName;
    }

    public String getRootPath() {
        return rootPath;
    }

    public void setRootPath(String rootPath) {
        this.rootPath = rootPath;
    }

    public Set getPictures() {
        return pictures;
    }

    public void setPictures(Set pictures) {
        this.pictures = pictures;
    }

    public void addPicture(String picture){
        if (pictures==null){
            pictures=new HashSet<>();
        }

        pictures.add(picture);
    }

    public void rmPicture(String picture){
        if (pictures==null){
            return;
        }

        pictures.remove(picture);
    }

    public String getRootName() {
        return rootName;
    }

    public void setRootName(String rootName) {
        this.rootName = rootName;
    }

    @Override
    public String toString() {
        return "ImageCollection{" +
                "rootPath='" + rootPath + '\'' +
                ", pictures=" + pictures +
                '}';
    }
}

圖片顯示適配器:

/**
 * 圖片顯示頁面的適配器
 * Created by tianlai on 16-4-12.
 */
public class PicturesAdapter extends BaseAbsListAdapter {
    private Set selectedPictures;

    private OnPictrueSelectListener  onPictrueSelectListener;

    private int maxNum;

    public PicturesAdapter(Context context, LayoutInflater inflater) {

        super(context, inflater);

        selectedPictures=new HashSet<>();
    }

    @Override
    public PicturesViewHolder onCreateViewHolder(ViewGroup parent, int viewType, LayoutInflater inflater) {
        return new PicturesViewHolder(inflater.inflate(R.layout.grid_item_select_image,null),this);
    }


    public Set getSelectedPictures() {
        return selectedPictures;
    }

    public void setSelectedPictures(Set selectedPictures) {
        this.selectedPictures = selectedPictures;
    }

    public void addSelectedPicture(String path){
        selectedPictures.add(path);

        notifySelectedItem();
    }

    public void removeSelectedPicture(String path){
        selectedPictures.remove(path);

        notifySelectedItem();
    }

    /**
     * 有item選中或者取消選中時的事件
     */
    private void notifySelectedItem() {
        if (onPictrueSelectListener!=null){
            int size = selectedPictures.size();
            onPictrueSelectListener.selectedSingleItem(size==1);
            onPictrueSelectListener.hasItemSelected(size >0,size);
        }
    }

    public boolean isSlected(String path){
        Log.i("picture",selectedPictures.toString());
        return selectedPictures.contains(path);
    }

    public OnPictrueSelectListener getOnPictrueSelectListener() {
        return onPictrueSelectListener;
    }

    public void setOnPictrueSelectListener(OnPictrueSelectListener onPictrueSelectListener) {
        this.onPictrueSelectListener = onPictrueSelectListener;
    }

    public int getMaxNum() {
        return maxNum;
    }

    public void setMaxNum(int maxNum) {
        this.maxNum = maxNum;
    }

    public boolean isCanSelectMore(){
        return selectedPictures.size()(selectedPictures).get(0).equals(path)){
                return true;
            }
        }

        return  false;
    }

    /**
     * 獲取要剪裁的圖片路徑
     *
     * @return
     */
    public String getCorpPicturePath(){
        if (selectedPictures.size()==1){
            return new ArrayList<>(selectedPictures).get(0);
        }

        return null;
    }

    /**
     * 清空選中的圖片
     */
    public void clearSelectedPictures(){
        selectedPictures.clear();
    }

    public static interface OnPictrueSelectListener{
        public void selectedSingleItem(boolean selectedSingleItem);

        public void hasItemSelected(boolean hasItemSelected, int selectNum);
    }
}

圖片顯示的Viewholder:

/**
 * 圖片顯示頁面的適配器的ViewHolder
 * Created by tianlai on 16-4-12.
 */
public class PicturesViewHolder extends BaseViewHolder implements  CompoundButton
        .OnCheckedChangeListener {

    SimpleDraweeView sdvPicture;

    CheckBox cbSelect;

    private ImageRequest imageRequest;
    private DraweeController draweeController;

    public PicturesViewHolder(View convertView, BaseAbsListAdapter absListAdapter) {
        super(convertView, absListAdapter);

        sdvPicture = (SimpleDraweeView) find(R.id.sdv_picture);
        cbSelect = (CheckBox) find(R.id.cb_select);

        cbSelect.setOnCheckedChangeListener(this);
    }

    @Override
    public void loadDataToView(int position, String data) {
        super.loadDataToView(position,data);

        imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse("file://" + data))
                .setResizeOptions(new ResizeOptions(128,128))
                .setLocalThumbnailPreviewsEnabled(true)
                .build();
        draweeController = Fresco.newDraweeControllerBuilder()
                .setImageRequest(imageRequest)
                .setOldController(sdvPicture.getController())
                .build();

        sdvPicture.setController(draweeController);

        if (((PicturesAdapter) absListAdapter).isSlected(data)) {
            cbSelect.setChecked(true);
            Log.i("picture","位置"+position+"--已選中");
        }else {
            cbSelect.setChecked(false);
            Log.i("picture","位置"+position+"--未選中");
        }

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                if (((PicturesAdapter) absListAdapter).isCanSelectMore()||((PicturesAdapter) absListAdapter).isSlected(data)){
                    ((PicturesAdapter) absListAdapter).addSelectedPicture(data);
                    Log.i("picture","位置"+position+"--正在點擊選中");
                }else {
                    Toast.makeText(context,"最多只能選擇"+((PicturesAdapter) absListAdapter).getMaxNum()+"張圖片",Toast
                            .LENGTH_SHORT).show();
                    buttonView.setChecked(false);
                }
            } else {
                ((PicturesAdapter) absListAdapter).removeSelectedPicture(data);
                Log.i("picture","位置"+position+"--正在取消選中");
            }
    }

}

圖片文件夾頁面適配器:

/**
 * 選擇圖片文件夾頁面的適配器 
 * Created by tianlai on 16-4-12.
 */
public class ParentPathsAdapter extends BaseAbsListAdapter {

    public ParentPathsAdapter(Context context, LayoutInflater inflater) {
        super(context, inflater);
    }

    @Override
    public ParentPathsViewHolder onCreateViewHolder(ViewGroup parent, int viewType, LayoutInflater inflater) {
        return new ParentPathsViewHolder(inflater.inflate(R.layout.list_item_parent_paths,null),this);
    }
}

圖片文件夾頁面ViewHolder:

/**
 * 選擇圖片文件夾頁面的適配器的ViewHolder
 * Created by tianlai on 16-4-12.
 */
public class ParentPathsViewHolder extends BaseViewHolder {

    SimpleDraweeView sdvImage;

    TextView tvPath;

    public ParentPathsViewHolder(View convertView, BaseAbsListAdapter absListAdapter) {
        super(convertView, absListAdapter);

        sdvImage= (SimpleDraweeView) find(R.id.sdv_image);
        tvPath= (TextView) find(R.id.tv_path);
    }

    @Override
    public void loadDataToView(int position, ImageCollection data) {
        List pictures = new ArrayList<>(data.getPictures());
        if (pictures != null) {
            int size = pictures.size();
            if (size > 0) {
                sdvImage.setImageURI(Uri.parse("file://" + pictures.get(0)));
                tvPath.setText(data.getRootName() + "(" + size + ")");
            }
        }
    }

}

選中的圖片預覽適配器:

/**
 * 浏覽選中的圖片的適配器(浏覽頁面是一個ViewPager)
 * Created by tianlai on 16-4-12.
 */
public class PicturesPagerAdapter extends PagerAdapter {
    private List images;

    private List picturePaths;

    private LayoutInflater inflater;

    public PicturesPagerAdapter(List picturePaths, LayoutInflater inflater) {
        this.inflater = inflater;
        this.picturePaths=picturePaths;

        images = new ArrayList();

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
                .LayoutParams.MATCH_PARENT);

        View view;

        for (String path : picturePaths) {
            view=inflater.inflate(R.layout.vp_item_picture,null);
            ((SimpleDraweeView)view.findViewById(R.id.vp_picture)).setImageURI(Uri.parse("file://"+path));
            images.add(view);
        }


    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        View view = images.get(position);
        container.addView(view);

        return view;
    }

    public List getPicturePaths() {
        return picturePaths;
    }

    public void setPicturePaths(List picturePaths) {
        this.picturePaths = picturePaths;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(images.get(position));
    }


}

剪裁圖片dialog:

/**
 * 剪裁圖片的對話框
 * Created by tianlai on 16-4-13.
 */
public class CropImageDialog extends Dialog {

    private Context context;

    private LayoutInflater inflater;

    // Views ///////////////////////////////////////////////////////////////////////////////////////
    private CropImageView mCropView;
    private LinearLayout mRootLayout;
    private ProgressBar progressBar;

    private String cachePath;
    private String cropPath;

    private boolean isFromPick;

    private CropImageListener cropImageListener;

    public CropImageDialog(Context context) {
        super(context, R.style.AppTheme_Dialog);

        init(context);

    }

    public CropImageDialog(Context context, int theme) {
        super(context, theme);


        init(context);
    }

    private void init(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        View contentView = inflater.inflate(R.layout.dialog_cropimage, null);

        setContentView(contentView);

        // bind Views
        bindViews(contentView);

        mCropView.setDebug(BuildConfig.DEBUG);
    }


    private void bindViews(View view) {
        progressBar = (ProgressBar) view.findViewById(R.id.progress);
        progressBar.getIndeterminateDrawable().setColorFilter(getContext().getResources().getColor(R.color.colorAccent),
                PorterDuff.Mode.SRC_IN);
        mRootLayout = (LinearLayout) view.findViewById(R.id.layout_root);

        mCropView = (CropImageView) view.findViewById(R.id.cropImageView);
        mCropView.setOutputMaxSize(300, 300);

        view.findViewById(R.id.top_home).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        view.findViewById(R.id.top_action).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        view.findViewById(R.id.buttonDone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showProgress();
                mCropView.startCrop(createSaveUri(), mCropCallback, mSaveCallback);
            }
        });
        view.findViewById(R.id.buttonFitImage).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCropView.setCropMode(CropImageView.CropMode.FIT_IMAGE);
            }
        });
        view.findViewById(R.id.button1_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCropView.setCropMode(CropImageView.CropMode.SQUARE);
            }
        });
        view.findViewById(R.id.button3_4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCropView.setCropMode(CropImageView.CropMode.RATIO_3_4);
            }
        });
        view.findViewById(R.id.button4_3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCropView.setCropMode(CropImageView.CropMode.RATIO_4_3);
            }
        });
        view.findViewById(R.id.button9_16).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCropView.setCropMode(CropImageView.CropMode.RATIO_9_16);
            }
        });
        view.findViewById(R.id.button16_9).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCropView.setCropMode(CropImageView.CropMode.RATIO_16_9);
            }
        });
        view.findViewById(R.id.buttonFree).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCropView.setCropMode(CropImageView.CropMode.FREE);
            }
        });
        view.findViewById(R.id.buttonPickImage).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cropImageListener != null) {
                    cropImageListener.onPickImage();
                }
            }
        });
        view.findViewById(R.id.buttonRotateLeft).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCropView.rotateImage(CropImageView.RotateDegrees.ROTATE_M90D);
            }
        });
        view.findViewById(R.id.buttonRotateRight).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCropView.rotateImage(CropImageView.RotateDegrees.ROTATE_90D);
            }
        });
        view.findViewById(R.id.buttonCustom).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCropView.setCustomRatio(7, 5);
            }
        });
        view.findViewById(R.id.buttonCircle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCropView.setCropMode(CropImageView.CropMode.CIRCLE);
            }
        });
        view.findViewById(R.id.buttonShowCircleButCropAsSquare).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCropView.setCropMode(CropImageView.CropMode.CIRCLE_SQUARE);
            }
        });

    }

    /**
     * 創建保存剪裁圖片的Uri
     *
     * @return
     */
    public Uri createSaveUri() {
        try {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                //檢查路徑是否存在
                File dir = new File(cachePath);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                //檢查文件是否存在
                File file = new File(cachePath, "cropped.jpg");
                if (!file.exists()) {
                    file.createNewFile();
                }

                cropPath = file.getAbsolutePath();

                return Uri.fromFile(file);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        if (isFromPick) {
            if (cropImageListener != null) {
                cropImageListener.onPickImage();

                return;
            }
        }

        dismiss();

    }

    private void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    private void dismissProgress() {
        progressBar.setVisibility(View.GONE);
    }

    /**
     * @param cachePath
     * @param pickImageListener
     */
    public void initCropDialog(String cachePath, CropImageListener pickImageListener) {
        this.cropImageListener = pickImageListener;
        this.cachePath = cachePath;
    }

    /**
     * 開始剪裁
     *
     * @param pictureUri 要剪裁的圖片的Uri
     */
    public void show(Uri pictureUri, boolean isFromPick) {

        Log.i("CropImageDialog", pictureUri.toString());

        this.isFromPick = isFromPick;

        super.show();
        setDialogWindowAttr();

        showProgress();
        mCropView.startLoad(pictureUri, mLoadCallback);

    }
//    /**
//     * 開始剪裁
//     *
//     * @param pictureUri
//     * @param isFromPick
//     */
//    public void show(Uri pictureUri, boolean isFromPick) {
//        super.show();
//
//        setDialogWindowAttr();
//
//        Log.i("CropImageDialog", pictureUri.toString());
//
//        this.isFromPick = isFromPick;
//
//        showProgress();
//        mCropView.setImageURI(pictureUri);
//
//    }

    /**
     * 調整dialog的大小
     */
    public void setDialogWindowAttr() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;//寬高可設置具體大小
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(lp);
    }

    public String getCachePath() {
        return cachePath;
    }

    public void setCachePath(String cachePath) {
        this.cachePath = cachePath;
    }

    public CropImageListener getCropImageListener() {
        return cropImageListener;
    }

    public void setCropImageListener(CropImageListener cropImageListener) {
        this.cropImageListener = cropImageListener;
    }

    // Callbacks ///////////////////////////////////////////////////////////////////////////////////

    private final LoadCallback mLoadCallback = new LoadCallback() {
        @Override
        public void onSuccess() {
            dismissProgress();
        }

        @Override
        public void onError() {
            dismissProgress();
        }
    };


    private final CropCallback mCropCallback = new CropCallback() {
        @Override
        public void onSuccess(Bitmap cropped) {
        }

        @Override
        public void onError() {
        }
    };

    private final SaveCallback mSaveCallback = new SaveCallback() {
        @Override
        public void onSuccess(Uri outputUri) {
            dismissProgress();

            if (cropImageListener != null) {
                cropImageListener.onCropSucceed(cropPath);
            }
        }

        @Override
        public void onError() {
            dismissProgress();
        }
    };

    public static interface CropImageListener {
        /**
         * 選擇圖片
         */
        public void onPickImage();

        /**
         * 剪裁成功
         *
         * @param path
         */
        public void onCropSucceed(String path);
    }
}

下面來看看效果圖:

1.圖片顯示頁面

圖片顯示頁面

2.圖片文件夾顯示頁面<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPjxpbWcgYWx0PQ=="圖片文件夾顯示頁面" src="/uploadfile/Collfiles/20160518/20160518092032471.jpg" title="\" />

3.選中的圖片的預覽頁面

選中的圖片的預覽頁面

4.圖片剪裁頁面

圖片剪裁頁面

好了,就到這裡吧!

更多資料:

我的github地址以及使用demo: https://github.com/naivor/naivorapp

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved