Repository: glassLake/PhotoPicker Branch: master Commit: 45621240a331 Files: 87 Total size: 196.6 KB Directory structure: gitextract_jrq830pw/ ├── .gitattributes ├── .gitignore ├── .travis.yml ├── LICENSE ├── PhotoPicker/ │ ├── .gitignore │ ├── build.gradle │ └── src/ │ └── main/ │ ├── AndroidManifest.xml │ ├── java/ │ │ └── me/ │ │ └── iwf/ │ │ └── photopicker/ │ │ ├── PhotoPagerActivity.java │ │ ├── PhotoPickUtils.java │ │ ├── PhotoPicker.java │ │ ├── PhotoPickerActivity.java │ │ ├── PhotoPreview.java │ │ ├── adapter/ │ │ │ ├── PhotoGridAdapter.java │ │ │ ├── PhotoPagerAdapter.java │ │ │ ├── PopupDirectoryListAdapter.java │ │ │ └── SelectableAdapter.java │ │ ├── entity/ │ │ │ ├── Photo.java │ │ │ └── PhotoDirectory.java │ │ ├── event/ │ │ │ ├── OnItemCheckListener.java │ │ │ ├── OnPhotoClickListener.java │ │ │ └── Selectable.java │ │ ├── fragment/ │ │ │ ├── ImagePagerFragment.java │ │ │ └── PhotoPickerFragment.java │ │ ├── utils/ │ │ │ ├── ExifUtils.java │ │ │ ├── ImageCaptureManager.java │ │ │ ├── MediaStoreHelper.java │ │ │ ├── PhotoDirectoryLoader.java │ │ │ └── PhotoPickerIntent.java │ │ └── widget/ │ │ ├── MultiPickResultView.java │ │ ├── PhotoAdapter.java │ │ ├── SquareItemLayout.java │ │ ├── Titlebar.java │ │ └── TouchImageView.java │ └── res/ │ ├── anim/ │ │ ├── dialog_enter.xml │ │ └── dialog_exit.xml │ ├── drawable/ │ │ ├── __picker_bg_material_item.xml │ │ ├── __picker_camera.xml │ │ ├── __picker_checkbox_bg.xml │ │ ├── __picker_default_weixin.xml │ │ ├── __picker_delete.xml │ │ └── __picker_photo_bg.xml │ ├── drawable-v21/ │ │ └── __picker_bg_material_item.xml │ ├── layout/ │ │ ├── __picker_activity_photo_pager.xml │ │ ├── __picker_activity_photo_picker.xml │ │ ├── __picker_fragment_photo_picker.xml │ │ ├── __picker_item_directory.xml │ │ ├── __picker_item_photo.xml │ │ ├── __picker_picker_fragment_image_pager.xml │ │ ├── __picker_picker_item_pager.xml │ │ ├── __picker_toolbar.xml │ │ └── view_titlebar.xml │ ├── menu/ │ │ ├── __picker_menu_picker.xml │ │ └── __picker_menu_preview.xml │ ├── values/ │ │ ├── colors.xml │ │ ├── dimen.xml │ │ ├── strings.xml │ │ ├── styles.xml │ │ └── values.xml │ ├── values-pt/ │ │ └── strings.xml │ ├── values-v21/ │ │ └── colors.xml │ └── values-zh/ │ └── strings.xml ├── PhotoPicker-release.aar ├── README.md ├── build.gradle ├── circle.sh ├── circle.yml ├── demo.apk ├── gradle/ │ ├── gradle-mvn-push.gradle │ └── wrapper/ │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── gradlew ├── gradlew.bat ├── photopickerdemo/ │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src/ │ └── main/ │ ├── AndroidManifest.xml │ ├── java/ │ │ └── me/ │ │ └── iwf/ │ │ └── PhotoPickerDemo/ │ │ ├── MainActivity.java │ │ ├── PicChosenListInterface.java │ │ └── RecyclerItemClickListener.java │ └── res/ │ ├── layout/ │ │ └── activity_main.xml │ ├── values/ │ │ ├── color.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── values-zh/ │ └── strings.xml └── settings.gradle ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitattributes ================================================ # Auto detect text files and perform LF normalization * text=auto ================================================ FILE: .gitignore ================================================ .idea/** .gradle /local.properties .DS_Store /build /captures # Built application files # Files for the Dalvik VM *.dex # Java class files *.class # Generated files bin/ gen/ # Gradle files .gradle/ /PhotoPicker/PhotoPicker-PhotoPicker.iml build/ /*/build/ # Local configuration file (sdk path, etc) local.properties # Proguard folder generated by Eclipse proguard/ # Log Files *.log *.iml ================================================ FILE: .travis.yml ================================================ language: android before_install: - chmod +x gradlew android: components: - tools - platform-tools - build-tools-23.0.3 - android-23 - extra-android-m2repository notifications: email: false sudo: false script: ./gradlew build ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2015 Huang Donglu Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: PhotoPicker/.gitignore ================================================ /build PhotoPicker-PhotoPicker.iml ================================================ FILE: PhotoPicker/build.gradle ================================================ apply plugin: 'com.android.library' android { compileSdkVersion 23 buildToolsVersion "23.0.3" resourcePrefix "__picker_" defaultConfig { minSdkVersion 11 targetSdkVersion 23 versionCode 1 versionName "0.8.5" } } apply from: '../gradle/gradle-mvn-push.gradle' repositories { jcenter() mavenCentral() // GPUImage for Android } dependencies { compile 'com.android.support:support-v4:23.4.0' compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:design:23.4.0' compile 'com.android.support:recyclerview-v7:23.4.0' compile 'com.android.support:support-annotations:23.4.0' compile 'com.nineoldandroids:library:2.4.0' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'jp.wasabeef:glide-transformations:2.0.1' } ================================================ FILE: PhotoPicker/src/main/AndroidManifest.xml ================================================ ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/PhotoPagerActivity.java ================================================ package me.iwf.photopicker; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.Snackbar; import android.support.v4.view.ViewPager; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import java.util.List; import me.iwf.photopicker.fragment.ImagePagerFragment; import me.iwf.photopicker.widget.MultiPickResultView; import me.iwf.photopicker.widget.Titlebar; import static me.iwf.photopicker.PhotoPicker.KEY_SELECTED_PHOTOS; import static me.iwf.photopicker.PhotoPreview.EXTRA_CURRENT_ITEM; import static me.iwf.photopicker.PhotoPreview.EXTRA_PHOTOS; import static me.iwf.photopicker.PhotoPreview.EXTRA_SHOW_DELETE; import static me.iwf.photopicker.PhotoPreview.EXTRA_ACTION; /** * Created by donglua on 15/6/24. */ public class PhotoPagerActivity extends AppCompatActivity { private ImagePagerFragment pagerFragment; //private ActionBar actionBar; private boolean showDelete; private Titlebar titlebar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.__picker_activity_photo_pager); int currentItem = getIntent().getIntExtra(EXTRA_CURRENT_ITEM, 0); List paths = getIntent().getStringArrayListExtra(EXTRA_PHOTOS); showDelete = getIntent().getBooleanExtra(EXTRA_SHOW_DELETE, true); int action = getIntent().getIntExtra(EXTRA_ACTION, MultiPickResultView.ACTION_ONLY_SHOW); if (pagerFragment == null) { pagerFragment = (ImagePagerFragment) getSupportFragmentManager().findFragmentById(R.id.photoPagerFragment); } pagerFragment.setPhotos(paths, currentItem); titlebar = (Titlebar) findViewById(R.id.titlebar); titlebar.init(this); if (action == MultiPickResultView.ACTION_SELECT){ titlebar.setRitht(getApplicationContext().getResources().getDrawable(R.drawable.__picker_delete), "", new View.OnClickListener() { @Override public void onClick(View v) { int position = pagerFragment.getViewPager().getCurrentItem(); if (pagerFragment.getPaths().size() >0){ pagerFragment.getPaths().remove(position); pagerFragment.getViewPager().getAdapter().notifyDataSetChanged(); if (pagerFragment.getPaths().size() ==0){ titlebar.setTitle(getString(R.string.__picker_preview) +" "+getString(R.string.__picker_image_index, 0, pagerFragment.getPaths().size())); } } } }); } titlebar.setTitle(getString(R.string.__picker_preview)); /*Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); actionBar = getSupportActionBar(); // centerActionBarTitle(this); if (actionBar != null) { actionBar.setDisplayHomeAsUpEnabled(true); updateActionBarTitle(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { actionBar.setElevation(25); } }*/ pagerFragment.getViewPager().addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { titlebar.setTitle(getString(R.string.__picker_preview) +" "+getString(R.string.__picker_image_index, pagerFragment.getViewPager().getCurrentItem() + 1, pagerFragment.getPaths().size())); // updateActionBarTitle(); } }); } //把actionBar的文字标题居中 public static void centerActionBarTitle(Activity activity) { int titleId = activity.getResources().getIdentifier("action_bar_title", "id", "android"); if (titleId<=0)return; TextView titleTextView = (TextView)activity.findViewById(titleId); DisplayMetrics metrics = activity.getResources().getDisplayMetrics(); LinearLayout.LayoutParams txvPars = (LinearLayout.LayoutParams) titleTextView.getLayoutParams(); txvPars.gravity = Gravity.CENTER_HORIZONTAL; txvPars.width = metrics.widthPixels; titleTextView.setLayoutParams(txvPars); titleTextView.setGravity(Gravity.CENTER); } @Override public boolean onCreateOptionsMenu(Menu menu) { if (showDelete){ getMenuInflater().inflate(R.menu.__picker_menu_preview, menu); } return true; } @Override public void onBackPressed() { Intent intent = new Intent(); intent.putExtra(KEY_SELECTED_PHOTOS, pagerFragment.getPaths()); setResult(RESULT_OK, intent); finish(); super.onBackPressed(); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { onBackPressed(); return true; } if (item.getItemId() == R.id.delete) { final int index = pagerFragment.getCurrentItem(); final String deletedPath = pagerFragment.getPaths().get(index); Snackbar snackbar = Snackbar.make(pagerFragment.getView(), R.string.__picker_deleted_a_photo, Snackbar.LENGTH_LONG); if (pagerFragment.getPaths().size() <= 1) { // show confirm dialog new AlertDialog.Builder(this) .setTitle(R.string.__picker_confirm_to_delete) .setPositiveButton(R.string.__picker_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); pagerFragment.getPaths().remove(index); pagerFragment.getViewPager().getAdapter().notifyDataSetChanged(); onBackPressed(); } }) .setNegativeButton(R.string.__picker_cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }) .show(); } else { snackbar.show(); pagerFragment.getPaths().remove(index); pagerFragment.getViewPager().getAdapter().notifyDataSetChanged(); } snackbar.setAction(R.string.__picker_undo, new View.OnClickListener() { @Override public void onClick(View view) { if (pagerFragment.getPaths().size() > 0) { pagerFragment.getPaths().add(index, deletedPath); } else { pagerFragment.getPaths().add(deletedPath); } pagerFragment.getViewPager().getAdapter().notifyDataSetChanged(); pagerFragment.getViewPager().setCurrentItem(index, true); } }); return true; } return super.onOptionsItemSelected(item); } /* public void updateActionBarTitle() { if (actionBar != null) actionBar.setTitle( getString(R.string.__picker_image_index, pagerFragment.getViewPager().getCurrentItem() + 1, pagerFragment.getPaths().size())); }*/ } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/PhotoPickUtils.java ================================================ package me.iwf.photopicker; import android.app.Activity; import android.content.Intent; import java.util.ArrayList; /** * Created by Administrator on 2016/8/5 0005. */ public class PhotoPickUtils { public static void onActivityResult(int requestCode, int resultCode, Intent data,PickHandler pickHandler ) { if (resultCode == Activity.RESULT_OK) { if (requestCode == PhotoPicker.REQUEST_CODE) {//第一次,选择图片后返回 if (data != null) { ArrayList photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS); pickHandler.onPickSuccess(photos); /* if (photos != null){ if (photos.size() >0){ }else { pickHandler.onPickFail("未选择图片1"); } }else { pickHandler.onPickFail("未选择图片2"); }*/ } else { pickHandler.onPickFail("选择图片失败"); } }else if (requestCode == PhotoPreview.REQUEST_CODE){//如果是预览与删除后返回 if (data != null) { ArrayList photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS); pickHandler.onPreviewBack(photos); } else { // pickHandler.onPickFail("选择图片失败"); } } }else { if (requestCode == PhotoPicker.REQUEST_CODE){ pickHandler.onPickCancle(); } } } public static void startPick(Activity context,boolean showGif,int photoCount,ArrayList photos){ PhotoPicker.builder() .setPhotoCount(photoCount) .setShowCamera(false) .setShowGif(showGif) .setSelected(photos) .setPreviewEnabled(true) .start(context, PhotoPicker.REQUEST_CODE); } public interface PickHandler{ void onPickSuccess(ArrayList photos); void onPreviewBack(ArrayList photos); void onPickFail(String error); void onPickCancle(); } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/PhotoPicker.java ================================================ package me.iwf.photopicker; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import java.util.ArrayList; /** * Created by Donglua on 16/6/25. * Builder class to ease Intent setup. */ public class PhotoPicker { public static final int REQUEST_CODE = 233; public final static int DEFAULT_MAX_COUNT = 9; public final static int DEFAULT_COLUMN_NUMBER = 3; public final static String KEY_SELECTED_PHOTOS = "SELECTED_PHOTOS"; public final static String EXTRA_MAX_COUNT = "MAX_COUNT"; public final static String EXTRA_SHOW_CAMERA = "SHOW_CAMERA"; public final static String EXTRA_SHOW_GIF = "SHOW_GIF"; public final static String EXTRA_GRID_COLUMN = "column"; public final static String EXTRA_ORIGINAL_PHOTOS = "ORIGINAL_PHOTOS"; public final static String EXTRA_PREVIEW_ENABLED = "PREVIEW_ENABLED"; public static PhotoPickerBuilder builder() { return new PhotoPickerBuilder(); } public static class PhotoPickerBuilder { private Bundle mPickerOptionsBundle; private Intent mPickerIntent; public PhotoPickerBuilder() { mPickerOptionsBundle = new Bundle(); mPickerIntent = new Intent(); } /** * Send the Intent from an Activity with a custom request code * * @param activity Activity to receive result * @param requestCode requestCode for result */ public void start(@NonNull Activity activity, int requestCode) { activity.startActivityForResult(getIntent(activity), requestCode); } /** * Send the Intent with a custom request code * * @param fragment Fragment to receive result * @param requestCode requestCode for result */ public void start(@NonNull Context context, @NonNull android.support.v4.app.Fragment fragment, int requestCode) { fragment.startActivityForResult(getIntent(context), requestCode); } /** * Send the Intent with a custom request code * * @param fragment Fragment to receive result */ public void start(@NonNull Context context, @NonNull android.support.v4.app.Fragment fragment) { fragment.startActivityForResult(getIntent(context), REQUEST_CODE); } /** * Get Intent to start {@link PhotoPickerActivity} * * @return Intent for {@link PhotoPickerActivity} */ public Intent getIntent(@NonNull Context context) { mPickerIntent.setClass(context, PhotoPickerActivity.class); mPickerIntent.putExtras(mPickerOptionsBundle); return mPickerIntent; } /** * Send the crop Intent from an Activity * * @param activity Activity to receive result */ public void start(@NonNull Activity activity) { start(activity, REQUEST_CODE); } public PhotoPickerBuilder setPhotoCount(int photoCount) { mPickerOptionsBundle.putInt(EXTRA_MAX_COUNT, photoCount); return this; } public PhotoPickerBuilder setGridColumnCount(int columnCount) { mPickerOptionsBundle.putInt(EXTRA_GRID_COLUMN, columnCount); return this; } public PhotoPickerBuilder setShowGif(boolean showGif) { mPickerOptionsBundle.putBoolean(EXTRA_SHOW_GIF, showGif); return this; } public PhotoPickerBuilder setShowCamera(boolean showCamera) { mPickerOptionsBundle.putBoolean(EXTRA_SHOW_CAMERA, showCamera); return this; } public PhotoPickerBuilder setSelected(ArrayList imagesUri) { mPickerOptionsBundle.putStringArrayList(EXTRA_ORIGINAL_PHOTOS, imagesUri); return this; } public PhotoPickerBuilder setPreviewEnabled(boolean previewEnabled) { mPickerOptionsBundle.putBoolean(EXTRA_PREVIEW_ENABLED, previewEnabled); return this; } } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/PhotoPickerActivity.java ================================================ package me.iwf.photopicker; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import me.iwf.photopicker.entity.Photo; import me.iwf.photopicker.event.OnItemCheckListener; import me.iwf.photopicker.fragment.ImagePagerFragment; import me.iwf.photopicker.fragment.PhotoPickerFragment; import me.iwf.photopicker.widget.Titlebar; import static android.widget.Toast.LENGTH_LONG; import static me.iwf.photopicker.PhotoPicker.DEFAULT_COLUMN_NUMBER; import static me.iwf.photopicker.PhotoPicker.DEFAULT_MAX_COUNT; import static me.iwf.photopicker.PhotoPicker.EXTRA_GRID_COLUMN; import static me.iwf.photopicker.PhotoPicker.EXTRA_MAX_COUNT; import static me.iwf.photopicker.PhotoPicker.EXTRA_ORIGINAL_PHOTOS; import static me.iwf.photopicker.PhotoPicker.EXTRA_PREVIEW_ENABLED; import static me.iwf.photopicker.PhotoPicker.EXTRA_SHOW_CAMERA; import static me.iwf.photopicker.PhotoPicker.EXTRA_SHOW_GIF; import static me.iwf.photopicker.PhotoPicker.KEY_SELECTED_PHOTOS; public class PhotoPickerActivity extends AppCompatActivity { private PhotoPickerFragment pickerFragment; private ImagePagerFragment imagePagerFragment; //private MenuItem menuDoneItem; private int maxCount = DEFAULT_MAX_COUNT; /** to prevent multiple calls to inflate menu */ // private boolean menuIsInflated = false; private boolean showGif = false; private int columnNumber = DEFAULT_COLUMN_NUMBER; private ArrayList originalPhotos = null; private Titlebar titlebar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); boolean showCamera = getIntent().getBooleanExtra(EXTRA_SHOW_CAMERA, true); boolean showGif = getIntent().getBooleanExtra(EXTRA_SHOW_GIF, false); boolean previewEnabled = getIntent().getBooleanExtra(EXTRA_PREVIEW_ENABLED, true); setShowGif(showGif); setContentView(R.layout.__picker_activity_photo_picker); titlebar = (Titlebar) findViewById(R.id.titlebar); titlebar.init(this); /* Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); setTitle("");//去掉原生的标题 //将原生的返回图标换掉 mToolbar.setNavigationIcon(R.drawable.__picker_delete); mToolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PhotoPickerActivity.this.finish(); } }); ActionBar actionBar = getSupportActionBar(); assert actionBar != null; actionBar.setDisplayHomeAsUpEnabled(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { actionBar.setElevation(25); }*/ maxCount = getIntent().getIntExtra(EXTRA_MAX_COUNT, DEFAULT_MAX_COUNT); columnNumber = getIntent().getIntExtra(EXTRA_GRID_COLUMN, DEFAULT_COLUMN_NUMBER); originalPhotos = getIntent().getStringArrayListExtra(EXTRA_ORIGINAL_PHOTOS); pickerFragment = (PhotoPickerFragment) getSupportFragmentManager().findFragmentByTag("tag"); if (pickerFragment == null) { pickerFragment = PhotoPickerFragment .newInstance(showCamera, showGif, previewEnabled, columnNumber, maxCount, originalPhotos); getSupportFragmentManager() .beginTransaction() .replace(R.id.container, pickerFragment, "tag") .commit(); getSupportFragmentManager().executePendingTransactions(); } //右边的点击事件 titlebar.getTvRight().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ArrayList photos = pickerFragment.getPhotoGridAdapter().getSelectedPhotoPaths(); if (photos != null && photos.size() > 0){ Intent intent = new Intent(); intent.putStringArrayListExtra(KEY_SELECTED_PHOTOS, photos); setResult(RESULT_OK, intent); finish(); }else { Toast.makeText(getApplicationContext(),"还没有选择图片",Toast.LENGTH_SHORT).show(); } } }); pickerFragment.getPhotoGridAdapter().setOnItemCheckListener(new OnItemCheckListener() { @Override public boolean OnItemCheck(int position, Photo photo, final boolean isCheck, int selectedItemCount) { int total = selectedItemCount + (isCheck ? -1 : 1); // menuDoneItem.setEnabled(total > 0); if (maxCount <= 1) { List photos = pickerFragment.getPhotoGridAdapter().getSelectedPhotos(); if (!photos.contains(photo)) { photos.clear(); pickerFragment.getPhotoGridAdapter().notifyDataSetChanged(); } return true; } if (total > maxCount) { Toast.makeText(getActivity(), getString(R.string.__picker_over_max_count_tips, maxCount), LENGTH_LONG).show(); return false; } titlebar.getTvRight().setText(getString(R.string.__picker_done_with_count, total, maxCount)); return true; } }); } /** * Overriding this method allows us to run our exit animation first, then exiting * the activity when it complete. */ @Override public void onBackPressed() { if (imagePagerFragment != null && imagePagerFragment.isVisible()) { imagePagerFragment.runExitAnimation(new Runnable() { public void run() { if (getSupportFragmentManager().getBackStackEntryCount() > 0) { getSupportFragmentManager().popBackStack(); } } }); } else { super.onBackPressed(); } } public void addImagePagerFragment(ImagePagerFragment imagePagerFragment) { this.imagePagerFragment = imagePagerFragment; getSupportFragmentManager() .beginTransaction() .replace(R.id.container, this.imagePagerFragment) .addToBackStack(null) .commit(); } /* @Override public boolean onCreateOptionsMenu(Menu menu) { if (!menuIsInflated) { getMenuInflater().inflate(R.menu.__picker_menu_picker, menu); menuDoneItem = menu.findItem(R.id.done); if (originalPhotos != null && originalPhotos.size() > 0) { menuDoneItem.setEnabled(true); menuDoneItem.setTitle( getString(R.string.__picker_done_with_count, originalPhotos.size(), maxCount)); } else { menuDoneItem.setEnabled(false); } menuIsInflated = true; return true; } return false; }*/ /* @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { super.onBackPressed(); return true; } if (item.getItemId() == R.id.done) { Intent intent = new Intent(); ArrayList selectedPhotos = pickerFragment.getPhotoGridAdapter().getSelectedPhotoPaths(); intent.putStringArrayListExtra(KEY_SELECTED_PHOTOS, selectedPhotos); setResult(RESULT_OK, intent); finish(); return true; } return super.onOptionsItemSelected(item); }*/ public PhotoPickerActivity getActivity() { return this; } public boolean isShowGif() { return showGif; } public void setShowGif(boolean showGif) { this.showGif = showGif; } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/PhotoPreview.java ================================================ package me.iwf.photopicker; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import java.util.ArrayList; /** * Created by Donglua on 16/6/25. * Builder class to ease Intent setup. */ public class PhotoPreview { public final static int REQUEST_CODE = 666; public final static String EXTRA_CURRENT_ITEM = "current_item"; public final static String EXTRA_PHOTOS = "photos"; public final static String EXTRA_SHOW_DELETE = "show_delete"; public final static String EXTRA_ACTION = "action"; public static PhotoPreviewBuilder builder() { return new PhotoPreviewBuilder(); } public static class PhotoPreviewBuilder { private Bundle mPreviewOptionsBundle; private Intent mPreviewIntent; public PhotoPreviewBuilder() { mPreviewOptionsBundle = new Bundle(); mPreviewIntent = new Intent(); } /** * Send the Intent from an Activity with a custom request code * * @param activity Activity to receive result * @param requestCode requestCode for result */ public void start(@NonNull Activity activity, int requestCode) { activity.startActivityForResult(getIntent(activity), requestCode); } /** * Send the Intent with a custom request code * * @param fragment Fragment to receive result * @param requestCode requestCode for result */ public void start(@NonNull Context context, @NonNull android.support.v4.app.Fragment fragment, int requestCode) { fragment.startActivityForResult(getIntent(context), requestCode); } /** * Send the Intent with a custom request code * * @param fragment Fragment to receive result */ public void start(@NonNull Context context, @NonNull android.support.v4.app.Fragment fragment) { fragment.startActivityForResult(getIntent(context), REQUEST_CODE); } /** * Send the crop Intent from an Activity * * @param activity Activity to receive result */ public void start(@NonNull Activity activity) { start(activity, REQUEST_CODE); } /** * Get Intent to start {@link PhotoPickerActivity} * * @return Intent for {@link PhotoPickerActivity} */ public Intent getIntent(@NonNull Context context) { mPreviewIntent.setClass(context, PhotoPagerActivity.class); mPreviewIntent.putExtras(mPreviewOptionsBundle); return mPreviewIntent; } public PhotoPreviewBuilder setPhotos(ArrayList photoPaths) { mPreviewOptionsBundle.putStringArrayList(EXTRA_PHOTOS, photoPaths); return this; } public PhotoPreviewBuilder setAction(int action) { mPreviewOptionsBundle.putInt(EXTRA_ACTION, action); return this; } public PhotoPreviewBuilder setCurrentItem(int currentItem) { mPreviewOptionsBundle.putInt(EXTRA_CURRENT_ITEM, currentItem); return this; } public PhotoPreviewBuilder setShowDeleteButton(boolean showDeleteButton) { mPreviewOptionsBundle.putBoolean(EXTRA_SHOW_DELETE, showDeleteButton); return this; } } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/adapter/PhotoGridAdapter.java ================================================ package me.iwf.photopicker.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.ImageView; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import java.io.File; import java.util.ArrayList; import java.util.List; import me.iwf.photopicker.R; import me.iwf.photopicker.entity.Photo; import me.iwf.photopicker.entity.PhotoDirectory; import me.iwf.photopicker.event.OnItemCheckListener; import me.iwf.photopicker.event.OnPhotoClickListener; import me.iwf.photopicker.utils.MediaStoreHelper; /** * Created by donglua on 15/5/31. */ public class PhotoGridAdapter extends SelectableAdapter { private LayoutInflater inflater; private RequestManager glide; private OnItemCheckListener onItemCheckListener = null; private OnPhotoClickListener onPhotoClickListener = null; private View.OnClickListener onCameraClickListener = null; public final static int ITEM_TYPE_CAMERA = 100; public final static int ITEM_TYPE_PHOTO = 101; private final static int COL_NUMBER_DEFAULT = 3; private boolean hasCamera = true; private boolean previewEnable = true; private int imageSize; private int columnNumber = COL_NUMBER_DEFAULT; public PhotoGridAdapter(Context context, RequestManager requestManager, List photoDirectories) { this.photoDirectories = photoDirectories; this.glide = requestManager; inflater = LayoutInflater.from(context); setColumnNumber(context, columnNumber); } public PhotoGridAdapter(Context context, RequestManager requestManager, List photoDirectories, ArrayList orginalPhotos, int colNum) { this(context, requestManager, photoDirectories); setColumnNumber(context, colNum); setOriginalPhotos(orginalPhotos); } private void setOriginalPhotos(ArrayList originalPhotos){ this.originalPhotos = originalPhotos; } private void setColumnNumber(Context context, int columnNumber) { this.columnNumber = columnNumber; WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics metrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(metrics); int widthPixels = metrics.widthPixels; imageSize = widthPixels / columnNumber; } @Override public int getItemViewType(int position) { return (showCamera() && position == 0) ? ITEM_TYPE_CAMERA : ITEM_TYPE_PHOTO; } @Override public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = inflater.inflate(R.layout.__picker_item_photo, parent, false); PhotoViewHolder holder = new PhotoViewHolder(itemView); if (viewType == ITEM_TYPE_CAMERA) { holder.vSelected.setVisibility(View.GONE); holder.ivPhoto.setScaleType(ImageView.ScaleType.CENTER); holder.ivPhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (onCameraClickListener != null) { onCameraClickListener.onClick(view); } } }); } return holder; } @Override public void onBindViewHolder(final PhotoViewHolder holder, int position) { if (getItemViewType(position) == ITEM_TYPE_PHOTO) { List photos = getCurrentPhotos(); final Photo photo; if (showCamera()) { photo = photos.get(position - 1); } else { photo = photos.get(position); } glide .load(new File(photo.getPath())) .centerCrop() .dontAnimate() .thumbnail(0.5f) .override(imageSize, imageSize) .placeholder(R.drawable.__picker_default_weixin) .error(R.drawable.__picker_ic_broken_image_black_48dp) .into(holder.ivPhoto); final boolean isChecked = isSelected(photo); holder.vSelected.setSelected(isChecked); holder.cover.setSelected(isChecked); holder.ivPhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (onPhotoClickListener != null) { int pos = holder.getAdapterPosition(); if (previewEnable) { onPhotoClickListener.onClick(view, pos, showCamera()); } else { holder.vSelected.performClick(); } } } }); holder.vSelected.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int pos = holder.getAdapterPosition(); boolean isEnable = true; if (onItemCheckListener != null) { isEnable = onItemCheckListener.OnItemCheck(pos, photo, isChecked, getSelectedPhotos().size()); } if (isEnable) { toggleSelection(photo); notifyItemChanged(pos); } } }); } else { holder.ivPhoto.setImageResource(R.drawable.__picker_camera); } } @Override public int getItemCount() { int photosCount = photoDirectories.size() == 0 ? 0 : getCurrentPhotos().size(); if (showCamera()) { return photosCount + 1; } return photosCount; } public static class PhotoViewHolder extends RecyclerView.ViewHolder { private ImageView ivPhoto; private View vSelected; private View cover; public PhotoViewHolder(View itemView) { super(itemView); ivPhoto = (ImageView) itemView.findViewById(R.id.iv_photo); vSelected = itemView.findViewById(R.id.v_selected); cover = itemView.findViewById(R.id.cover); } } public void setOnItemCheckListener(OnItemCheckListener onItemCheckListener) { this.onItemCheckListener = onItemCheckListener; } public void setOnPhotoClickListener(OnPhotoClickListener onPhotoClickListener) { this.onPhotoClickListener = onPhotoClickListener; } public void setOnCameraClickListener(View.OnClickListener onCameraClickListener) { this.onCameraClickListener = onCameraClickListener; } public ArrayList getSelectedPhotoPaths() { ArrayList selectedPhotoPaths = new ArrayList<>(getSelectedItemCount()); for (Photo photo : selectedPhotos) { selectedPhotoPaths.add(photo.getPath()); } return selectedPhotoPaths; } public void setShowCamera(boolean hasCamera) { this.hasCamera = hasCamera; } public void setPreviewEnable(boolean previewEnable) { this.previewEnable = previewEnable; } public boolean showCamera() { return (hasCamera && currentDirectoryIndex == MediaStoreHelper.INDEX_ALL_PHOTOS); } @Override public void onViewRecycled(PhotoViewHolder holder) { Glide.clear(holder.ivPhoto); super.onViewRecycled(holder); } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/adapter/PhotoPagerAdapter.java ================================================ package me.iwf.photopicker.adapter; import android.app.Activity; import android.content.Context; import android.net.Uri; import android.support.v4.view.PagerAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import java.io.File; import java.util.ArrayList; import java.util.List; import me.iwf.photopicker.R; /** * Created by donglua on 15/6/21. */ public class PhotoPagerAdapter extends PagerAdapter { private List paths = new ArrayList<>(); private RequestManager mGlide; public PhotoPagerAdapter(RequestManager glide, List paths) { this.paths = paths; this.mGlide = glide; } @Override public Object instantiateItem(ViewGroup container, int position) { final Context context = container.getContext(); View itemView = LayoutInflater.from(context) .inflate(R.layout.__picker_picker_item_pager, container, false); final ImageView imageView = (ImageView) itemView.findViewById(R.id.iv_pager); final String path = paths.get(position); final Uri uri; if (path.startsWith("http")) { uri = Uri.parse(path); } else { uri = Uri.fromFile(new File(path)); } mGlide.load(uri) .thumbnail(0.1f) .dontAnimate() .dontTransform() .override(800, 800) .placeholder(R.drawable.__picker_ic_photo_black_48dp) .error(R.drawable.__picker_ic_broken_image_black_48dp) .into(imageView); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (context instanceof Activity) { if (!((Activity) context).isFinishing()) { ((Activity) context).onBackPressed(); } } } }); container.addView(itemView); return itemView; } @Override public int getCount() { return paths.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); Glide.clear((View) object); } @Override public int getItemPosition (Object object) { return POSITION_NONE; } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/adapter/PopupDirectoryListAdapter.java ================================================ package me.iwf.photopicker.adapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.RequestManager; import java.util.ArrayList; import java.util.List; import me.iwf.photopicker.R; import me.iwf.photopicker.entity.PhotoDirectory; /** * Created by donglua on 15/6/28. */ public class PopupDirectoryListAdapter extends BaseAdapter { private List directories = new ArrayList<>(); private RequestManager glide; public PopupDirectoryListAdapter(RequestManager glide, List directories) { this.directories = directories; this.glide = glide; } @Override public int getCount() { return directories.size(); } @Override public PhotoDirectory getItem(int position) { return directories.get(position); } @Override public long getItemId(int position) { return directories.get(position).hashCode(); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { LayoutInflater mLayoutInflater = LayoutInflater.from(parent.getContext()); convertView = mLayoutInflater.inflate(R.layout.__picker_item_directory, parent, false); holder = new ViewHolder(convertView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.bindData(directories.get(position)); return convertView; } private class ViewHolder { public ImageView ivCover; public TextView tvName; public TextView tvCount; public ViewHolder(View rootView) { ivCover = (ImageView) rootView.findViewById(R.id.iv_dir_cover); tvName = (TextView) rootView.findViewById(R.id.tv_dir_name); tvCount = (TextView) rootView.findViewById(R.id.tv_dir_count); } public void bindData(PhotoDirectory directory) { glide.load(directory.getCoverPath()) .dontAnimate() .thumbnail(0.1f) .into(ivCover); tvName.setText(directory.getName()); tvCount.setText(tvCount.getContext().getString(R.string.__picker_image_count, directory.getPhotos().size())); } } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/adapter/SelectableAdapter.java ================================================ package me.iwf.photopicker.adapter; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.List; import me.iwf.photopicker.entity.Photo; import me.iwf.photopicker.entity.PhotoDirectory; import me.iwf.photopicker.event.Selectable; public abstract class SelectableAdapter extends RecyclerView.Adapter implements Selectable { private static final String TAG = SelectableAdapter.class.getSimpleName(); protected List photoDirectories; protected List selectedPhotos; //初始进入时已选的照片 original selected photos protected ArrayList originalPhotos = null; public int currentDirectoryIndex = 0; public SelectableAdapter() { photoDirectories = new ArrayList<>(); selectedPhotos = new ArrayList<>(); } /** * Indicates if the item at position where is selected * * @param photo Photo of the item to check * @return true if the item is selected, false otherwise */ @Override public boolean isSelected(Photo photo) { if (originalPhotos != null && originalPhotos.contains(photo.getPath()) && !selectedPhotos.contains(photo)) { selectedPhotos.add(photo); } return getSelectedPhotos().contains(photo); } /** * Toggle the selection status of the item at a given position * * @param photo Photo of the item to toggle the selection status for */ @Override public void toggleSelection(Photo photo) { if (selectedPhotos.contains(photo)) { selectedPhotos.remove(photo); if(originalPhotos!=null &&originalPhotos.contains(photo.getPath())){ originalPhotos.remove(photo.getPath()); } } else { selectedPhotos.add(photo); } } /** * Clear the selection status for all items */ @Override public void clearSelection() { selectedPhotos.clear(); } /** * Count the selected items * * @return Selected items count */ @Override public int getSelectedItemCount() { return selectedPhotos.size(); } public void setCurrentDirectoryIndex(int currentDirectoryIndex) { this.currentDirectoryIndex = currentDirectoryIndex; } public List getCurrentPhotos() { return photoDirectories.get(currentDirectoryIndex).getPhotos(); } public List getCurrentPhotoPaths() { List currentPhotoPaths = new ArrayList<>(getCurrentPhotos().size()); for (Photo photo : getCurrentPhotos()) { currentPhotoPaths.add(photo.getPath()); } return currentPhotoPaths; } public List getSelectedPhotos() { return selectedPhotos; } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/entity/Photo.java ================================================ package me.iwf.photopicker.entity; /** * Created by donglua on 15/6/30. */ public class Photo { private int id; private String path; public Photo(int id, String path) { this.id = id; this.path = path; } public Photo() { } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Photo)) return false; Photo photo = (Photo) o; return id == photo.id; } @Override public int hashCode() { return id; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public int getId() { return id; } public void setId(int id) { this.id = id; } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/entity/PhotoDirectory.java ================================================ package me.iwf.photopicker.entity; import android.text.TextUtils; import java.util.ArrayList; import java.util.List; /** * Created by donglua on 15/6/28. */ public class PhotoDirectory { private String id; private String coverPath; private String name; private long dateAdded; private List photos = new ArrayList<>(); @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof PhotoDirectory)) return false; PhotoDirectory directory = (PhotoDirectory) o; boolean hasId = !TextUtils.isEmpty(id); boolean otherHasId = !TextUtils.isEmpty(directory.id); if (hasId && otherHasId) { if (!TextUtils.equals(id, directory.id)) { return false; } return TextUtils.equals(name, directory.name); } return false; } @Override public int hashCode() { if (TextUtils.isEmpty(id)) { if (TextUtils.isEmpty(name)) { return 0; } return name.hashCode(); } int result = id.hashCode(); if (TextUtils.isEmpty(name)) { return result; } result = 31 * result + name.hashCode(); return result; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCoverPath() { return coverPath; } public void setCoverPath(String coverPath) { this.coverPath = coverPath; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getDateAdded() { return dateAdded; } public void setDateAdded(long dateAdded) { this.dateAdded = dateAdded; } public List getPhotos() { return photos; } public void setPhotos(List photos) { this.photos = photos; } public List getPhotoPaths() { List paths = new ArrayList<>(photos.size()); for (Photo photo : photos) { paths.add(photo.getPath()); } return paths; } public void addPhoto(int id, String path) { photos.add(new Photo(id, path)); } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/event/OnItemCheckListener.java ================================================ package me.iwf.photopicker.event; import me.iwf.photopicker.entity.Photo; /** * Created by donglua on 15/6/20. */ public interface OnItemCheckListener { /*** * * @param position 所选图片的位置 * @param path 所选的图片 * @param isCheck 当前状态 * @param selectedItemCount 已选数量 * @return enable check */ boolean OnItemCheck(int position, Photo path, boolean isCheck, int selectedItemCount); } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/event/OnPhotoClickListener.java ================================================ /* * Copyright (c) 2015. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. * Vestibulum commodo. Ut rhoncus gravida arcu. */ package me.iwf.photopicker.event; import android.view.View; /** * Created by donglua on 15/6/20. */ public interface OnPhotoClickListener { void onClick(View v, int position, boolean showCamera); } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/event/Selectable.java ================================================ package me.iwf.photopicker.event; import me.iwf.photopicker.entity.Photo; /** * Created by donglua on 15/6/30. */ public interface Selectable { /** * Indicates if the item at position position is selected * * @param photo Photo of the item to check * @return true if the item is selected, false otherwise */ boolean isSelected(Photo photo); /** * Toggle the selection status of the item at a given position * * @param photo Photo of the item to toggle the selection status for */ void toggleSelection(Photo photo); /** * Clear the selection status for all items */ void clearSelection(); /** * Count the selected items * * @return Selected items count */ int getSelectedItemCount(); } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/fragment/ImagePagerFragment.java ================================================ package me.iwf.photopicker.fragment; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.view.animation.AccelerateInterpolator; import android.view.animation.DecelerateInterpolator; import com.bumptech.glide.Glide; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.ObjectAnimator; import com.nineoldandroids.view.ViewHelper; import com.nineoldandroids.view.ViewPropertyAnimator; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import me.iwf.photopicker.R; import me.iwf.photopicker.adapter.PhotoPagerAdapter; /** * Created by donglua on 15/6/21. */ public class ImagePagerFragment extends Fragment { public final static String ARG_PATH = "PATHS"; public final static String ARG_CURRENT_ITEM = "ARG_CURRENT_ITEM"; private ArrayList paths; private ViewPager mViewPager; private PhotoPagerAdapter mPagerAdapter; public final static long ANIM_DURATION = 200L; public final static String ARG_THUMBNAIL_TOP = "THUMBNAIL_TOP"; public final static String ARG_THUMBNAIL_LEFT = "THUMBNAIL_LEFT"; public final static String ARG_THUMBNAIL_WIDTH = "THUMBNAIL_WIDTH"; public final static String ARG_THUMBNAIL_HEIGHT = "THUMBNAIL_HEIGHT"; public final static String ARG_HAS_ANIM = "HAS_ANIM"; private int thumbnailTop = 0; private int thumbnailLeft = 0; private int thumbnailWidth = 0; private int thumbnailHeight = 0; private boolean hasAnim = false; private final ColorMatrix colorizerMatrix = new ColorMatrix(); private int currentItem = 0; public static ImagePagerFragment newInstance(List paths, int currentItem) { ImagePagerFragment f = new ImagePagerFragment(); Bundle args = new Bundle(); args.putStringArray(ARG_PATH, paths.toArray(new String[paths.size()])); args.putInt(ARG_CURRENT_ITEM, currentItem); args.putBoolean(ARG_HAS_ANIM, false); f.setArguments(args); return f; } public static ImagePagerFragment newInstance(List paths, int currentItem, int[] screenLocation, int thumbnailWidth, int thumbnailHeight) { ImagePagerFragment f = newInstance(paths, currentItem); f.getArguments().putInt(ARG_THUMBNAIL_LEFT, screenLocation[0]); f.getArguments().putInt(ARG_THUMBNAIL_TOP, screenLocation[1]); f.getArguments().putInt(ARG_THUMBNAIL_WIDTH, thumbnailWidth); f.getArguments().putInt(ARG_THUMBNAIL_HEIGHT, thumbnailHeight); f.getArguments().putBoolean(ARG_HAS_ANIM, true); return f; } public void setPhotos(List paths, int currentItem) { this.paths.clear(); this.paths.addAll(paths); this.currentItem = currentItem; mViewPager.setCurrentItem(currentItem); mViewPager.getAdapter().notifyDataSetChanged(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); paths = new ArrayList<>(); Bundle bundle = getArguments(); if (bundle != null) { String[] pathArr = bundle.getStringArray(ARG_PATH); paths.clear(); if (pathArr != null) { paths = new ArrayList<>(Arrays.asList(pathArr)); } hasAnim = bundle.getBoolean(ARG_HAS_ANIM); currentItem = bundle.getInt(ARG_CURRENT_ITEM); thumbnailTop = bundle.getInt(ARG_THUMBNAIL_TOP); thumbnailLeft = bundle.getInt(ARG_THUMBNAIL_LEFT); thumbnailWidth = bundle.getInt(ARG_THUMBNAIL_WIDTH); thumbnailHeight = bundle.getInt(ARG_THUMBNAIL_HEIGHT); } mPagerAdapter = new PhotoPagerAdapter(Glide.with(this), paths); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.__picker_picker_fragment_image_pager, container, false); mViewPager = (ViewPager) rootView.findViewById(R.id.vp_photos); mViewPager.setAdapter(mPagerAdapter); mViewPager.setCurrentItem(currentItem); mViewPager.setOffscreenPageLimit(5); // Only run the animation if we're coming from the parent activity, not if // we're recreated automatically by the window manager (e.g., device rotation) if (savedInstanceState == null && hasAnim) { ViewTreeObserver observer = mViewPager.getViewTreeObserver(); observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { mViewPager.getViewTreeObserver().removeOnPreDrawListener(this); // Figure out where the thumbnail and full size versions are, relative // to the screen and each other int[] screenLocation = new int[2]; mViewPager.getLocationOnScreen(screenLocation); thumbnailLeft = thumbnailLeft - screenLocation[0]; thumbnailTop = thumbnailTop - screenLocation[1]; runEnterAnimation(); return true; } }); } mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { hasAnim = currentItem == position; } @Override public void onPageScrollStateChanged(int state) { } }); return rootView; } /** * The enter animation scales the picture in from its previous thumbnail * size/location, colorizing it in parallel. In parallel, the background of the * activity is fading in. When the pictue is in place, the text description * drops down. */ private void runEnterAnimation() { final long duration = ANIM_DURATION; // Set starting values for properties we're going to animate. These // values scale and position the full size version down to the thumbnail // size/location, from which we'll animate it back up ViewHelper.setPivotX(mViewPager, 0); ViewHelper.setPivotY(mViewPager, 0); ViewHelper.setScaleX(mViewPager, (float) thumbnailWidth / mViewPager.getWidth()); ViewHelper.setScaleY(mViewPager, (float) thumbnailHeight / mViewPager.getHeight()); ViewHelper.setTranslationX(mViewPager, thumbnailLeft); ViewHelper.setTranslationY(mViewPager, thumbnailTop); // Animate scale and translation to go from thumbnail to full size ViewPropertyAnimator.animate(mViewPager) .setDuration(duration) .scaleX(1) .scaleY(1) .translationX(0) .translationY(0) .setInterpolator(new DecelerateInterpolator()); // Fade in the black background ObjectAnimator bgAnim = ObjectAnimator.ofInt(mViewPager.getBackground(), "alpha", 0, 255); bgAnim.setDuration(duration); bgAnim.start(); // Animate a color filter to take the image from grayscale to full color. // This happens in parallel with the image scaling and moving into place. ObjectAnimator colorizer = ObjectAnimator.ofFloat(ImagePagerFragment.this, "saturation", 0, 1); colorizer.setDuration(duration); colorizer.start(); } /** * The exit animation is basically a reverse of the enter animation, except that if * the orientation has changed we simply scale the picture back into the center of * the screen. * * @param endAction This action gets run after the animation completes (this is * when we actually switch activities) */ public void runExitAnimation(final Runnable endAction) { if (!getArguments().getBoolean(ARG_HAS_ANIM, false) || !hasAnim) { endAction.run(); return; } final long duration = ANIM_DURATION; // Animate image back to thumbnail size/location ViewPropertyAnimator.animate(mViewPager) .setDuration(duration) .setInterpolator(new AccelerateInterpolator()) .scaleX((float) thumbnailWidth / mViewPager.getWidth()) .scaleY((float) thumbnailHeight / mViewPager.getHeight()) .translationX(thumbnailLeft) .translationY(thumbnailTop) .setListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { endAction.run(); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); // Fade out background ObjectAnimator bgAnim = ObjectAnimator.ofInt(mViewPager.getBackground(), "alpha", 0); bgAnim.setDuration(duration); bgAnim.start(); // Animate a color filter to take the image back to grayscale, // in parallel with the image scaling and moving into place. ObjectAnimator colorizer = ObjectAnimator.ofFloat(ImagePagerFragment.this, "saturation", 1, 0); colorizer.setDuration(duration); colorizer.start(); } /** * This is called by the colorizing animator. It sets a saturation factor that is then * passed onto a filter on the picture's drawable. * @param value saturation */ public void setSaturation(float value) { colorizerMatrix.setSaturation(value); ColorMatrixColorFilter colorizerFilter = new ColorMatrixColorFilter(colorizerMatrix); mViewPager.getBackground().setColorFilter(colorizerFilter); } public ViewPager getViewPager() { return mViewPager; } public ArrayList getPaths() { return paths; } public int getCurrentItem() { return mViewPager.getCurrentItem(); } @Override public void onDestroy() { super.onDestroy(); paths.clear(); paths = null; if (mViewPager != null) { mViewPager.setAdapter(null); } } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/fragment/PhotoPickerFragment.java ================================================ package me.iwf.photopicker.fragment; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.ListPopupWindow; import android.support.v7.widget.OrientationHelper; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.Button; import android.widget.Toast; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import java.io.IOException; import java.util.ArrayList; import java.util.List; import me.iwf.photopicker.PhotoPickerActivity; import me.iwf.photopicker.PhotoPreview; import me.iwf.photopicker.R; import me.iwf.photopicker.adapter.PhotoGridAdapter; import me.iwf.photopicker.adapter.PopupDirectoryListAdapter; import me.iwf.photopicker.entity.Photo; import me.iwf.photopicker.entity.PhotoDirectory; import me.iwf.photopicker.event.OnPhotoClickListener; import me.iwf.photopicker.utils.ImageCaptureManager; import me.iwf.photopicker.utils.MediaStoreHelper; import me.iwf.photopicker.widget.Titlebar; import static android.app.Activity.RESULT_OK; import static me.iwf.photopicker.PhotoPicker.DEFAULT_COLUMN_NUMBER; import static me.iwf.photopicker.PhotoPicker.EXTRA_PREVIEW_ENABLED; import static me.iwf.photopicker.PhotoPicker.EXTRA_SHOW_GIF; import static me.iwf.photopicker.utils.MediaStoreHelper.INDEX_ALL_PHOTOS; /** * Created by donglua on 15/5/31. */ public class PhotoPickerFragment extends Fragment { private ImageCaptureManager captureManager; private PhotoGridAdapter photoGridAdapter; private PopupDirectoryListAdapter listAdapter; //所有photos的路径 private List directories; //传入的已选照片 private ArrayList originalPhotos; private int SCROLL_THRESHOLD = 30; int column; //目录弹出框的一次最多显示的目录数目 public static int COUNT_MAX = 4; private final static String EXTRA_CAMERA = "camera"; private final static String EXTRA_COLUMN = "column"; private final static String EXTRA_COUNT = "count"; private final static String EXTRA_GIF = "gif"; private final static String EXTRA_ORIGIN = "origin"; private ListPopupWindow listPopupWindow; private RequestManager mGlideRequestManager; private Context mContext; private Titlebar titlebar; public static PhotoPickerFragment newInstance(boolean showCamera, boolean showGif, boolean previewEnable, int column, int maxCount, ArrayList originalPhotos) { Bundle args = new Bundle(); args.putBoolean(EXTRA_CAMERA, showCamera); args.putBoolean(EXTRA_GIF, showGif); args.putBoolean(EXTRA_PREVIEW_ENABLED, previewEnable); args.putInt(EXTRA_COLUMN, column); args.putInt(EXTRA_COUNT, maxCount); args.putStringArrayList(EXTRA_ORIGIN, originalPhotos); PhotoPickerFragment fragment = new PhotoPickerFragment(); fragment.setArguments(args); return fragment; } @Override public void onAttach(Context context) { super.onAttach(context); mContext = context; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); mGlideRequestManager = Glide.with(this); directories = new ArrayList<>(); originalPhotos = getArguments().getStringArrayList(EXTRA_ORIGIN); column = getArguments().getInt(EXTRA_COLUMN, DEFAULT_COLUMN_NUMBER); boolean showCamera = getArguments().getBoolean(EXTRA_CAMERA, true); boolean previewEnable = getArguments().getBoolean(EXTRA_PREVIEW_ENABLED, true); photoGridAdapter = new PhotoGridAdapter(mContext, mGlideRequestManager, directories, originalPhotos, column); photoGridAdapter.setShowCamera(showCamera); photoGridAdapter.setPreviewEnable(previewEnable); Bundle mediaStoreArgs = new Bundle(); boolean showGif = getArguments().getBoolean(EXTRA_GIF); mediaStoreArgs.putBoolean(EXTRA_SHOW_GIF, showGif); MediaStoreHelper.getPhotoDirs(getActivity(), mediaStoreArgs, new MediaStoreHelper.PhotosResultCallback() { @Override public void onResultCallback(List dirs) { directories.clear(); directories.addAll(dirs); photoGridAdapter.notifyDataSetChanged(); listAdapter.notifyDataSetChanged(); adjustHeight(); } }); captureManager = new ImageCaptureManager(getActivity()); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.__picker_fragment_photo_picker, container, false); titlebar = (Titlebar) rootView.findViewById(R.id.titlebar); listAdapter = new PopupDirectoryListAdapter(mGlideRequestManager, directories); RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.rv_photos); StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(column, OrientationHelper.VERTICAL); layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(photoGridAdapter); recyclerView.setItemAnimator(new DefaultItemAnimator()); final Button btSwitchDirectory = (Button) rootView.findViewById(R.id.button); Button btnPreview = (Button) rootView.findViewById(R.id.btn_preview); listPopupWindow = new ListPopupWindow(getActivity()); listPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//替换背景 WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); int widths = wm.getDefaultDisplay().getWidth(); listPopupWindow.setWidth(widths);//ListPopupWindow.MATCH_PARENT还是会有边距,直接拿到屏幕宽度来设置也不行,因为默认的background有左右padding值。 /* int height = wm.getDefaultDisplay().getHeight(); listPopupWindow.setHeight((int) (height *0.7));*/ listPopupWindow.setAnchorView(btSwitchDirectory); listPopupWindow.setAdapter(listAdapter); listPopupWindow.setModal(true); listPopupWindow.setDropDownGravity(Gravity.BOTTOM); listPopupWindow.setAnimationStyle(R.style.__picker_mystyle); listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { listPopupWindow.dismiss(); PhotoDirectory directory = directories.get(position); btSwitchDirectory.setText(directory.getName().toLowerCase());//默认会大写,这里要改成小写 photoGridAdapter.setCurrentDirectoryIndex(position); photoGridAdapter.notifyDataSetChanged(); } }); photoGridAdapter.setOnPhotoClickListener(new OnPhotoClickListener() { @Override public void onClick(View v, int position, boolean showCamera) { final int index = showCamera ? position - 1 : position; List photos = photoGridAdapter.getCurrentPhotoPaths(); int[] screenLocation = new int[2]; v.getLocationOnScreen(screenLocation); ImagePagerFragment imagePagerFragment = ImagePagerFragment.newInstance(photos, index, screenLocation, v.getWidth(), v.getHeight()); ((PhotoPickerActivity) getActivity()).addImagePagerFragment(imagePagerFragment); } }); photoGridAdapter.setOnCameraClickListener(new OnClickListener() { @Override public void onClick(View view) { try { Intent intent = captureManager.dispatchTakePictureIntent(); startActivityForResult(intent, ImageCaptureManager.REQUEST_TAKE_PHOTO); } catch (IOException e) { e.printStackTrace(); } } }); btSwitchDirectory.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (listPopupWindow.isShowing()) { listPopupWindow.dismiss(); } else if (!getActivity().isFinishing()) { adjustHeight(); listPopupWindow.show(); listPopupWindow.getListView().setVerticalScrollBarEnabled(false); //去掉滑动条,listview 在show之后才建立,所以需要该方法在show之后调用,否则会空指针 } } }); //预览按钮 btnPreview.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (photoGridAdapter.getSelectedPhotoPaths().size() > 0){ PhotoPreview.builder() .setPhotos(photoGridAdapter.getSelectedPhotoPaths()) .setCurrentItem(0) .start(getActivity()); }else { Toast.makeText(getActivity(),"还没有选择图片",Toast.LENGTH_SHORT).show(); } } }); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); // Log.d(">>> Picker >>>", "dy = " + dy); if (Math.abs(dy) > SCROLL_THRESHOLD) { mGlideRequestManager.pauseRequests(); } else { mGlideRequestManager.resumeRequests(); } } @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { if (newState == RecyclerView.SCROLL_STATE_IDLE) { mGlideRequestManager.resumeRequests(); } } }); return rootView; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == ImageCaptureManager.REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) { captureManager.galleryAddPic(); if (directories.size() > 0) { String path = captureManager.getCurrentPhotoPath(); PhotoDirectory directory = directories.get(INDEX_ALL_PHOTOS); directory.getPhotos().add(INDEX_ALL_PHOTOS, new Photo(path.hashCode(), path)); directory.setCoverPath(path); photoGridAdapter.notifyDataSetChanged(); } } } public PhotoGridAdapter getPhotoGridAdapter() { return photoGridAdapter; } @Override public void onSaveInstanceState(Bundle outState) { captureManager.onSaveInstanceState(outState); super.onSaveInstanceState(outState); } @Override public void onViewStateRestored(Bundle savedInstanceState) { captureManager.onRestoreInstanceState(savedInstanceState); super.onViewStateRestored(savedInstanceState); } public ArrayList getSelectedPhotoPaths() { return photoGridAdapter.getSelectedPhotoPaths(); } public void adjustHeight() { if (listAdapter == null) return; int count = listAdapter.getCount(); count = count < COUNT_MAX ? count : COUNT_MAX; if (listPopupWindow != null) { listPopupWindow.setHeight(count * getResources().getDimensionPixelOffset(R.dimen.__picker_item_directory_height)); } } @Override public void onDestroy() { super.onDestroy(); if (directories == null) { return; } for (PhotoDirectory directory : directories) { directory.getPhotoPaths().clear(); directory.getPhotos().clear(); directory.setPhotos(null); } directories.clear(); directories = null; } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/utils/ExifUtils.java ================================================ package me.iwf.photopicker.utils; import android.media.ExifInterface; import java.io.IOException; /** * Created by Administrator on 2016/8/15 0015. */ public class ExifUtils { public static void clearSensitiveInfo(String filePath){ ExifInterface exifInterface = null; try { exifInterface = new ExifInterface(filePath); exifInterface.setAttribute(ExifInterface.TAG_DATETIME ,"0"); exifInterface.setAttribute(ExifInterface.TAG_MAKE ,"0"); exifInterface.setAttribute(ExifInterface.TAG_MODEL ,"0"); exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE ,"0"); exifInterface.setAttribute(ExifInterface.TAG_GPS_LONGITUDE ,"0"); exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF ,"0"); exifInterface.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF ,"0"); exifInterface.setAttribute(ExifInterface.TAG_APERTURE ,"0"); exifInterface.setAttribute(ExifInterface.TAG_ISO ,"0"); exifInterface.setAttribute(ExifInterface.TAG_SUBSEC_TIME ,"0"); exifInterface.setAttribute(ExifInterface.TAG_SUBSEC_TIME_ORIG ,"0"); exifInterface.setAttribute(ExifInterface.TAG_SUBSEC_TIME_DIG ,"0"); exifInterface.setAttribute(ExifInterface.TAG_GPS_ALTITUDE ,"0"); exifInterface.setAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF ,"0"); exifInterface.setAttribute(ExifInterface.TAG_GPS_TIMESTAMP ,"0"); exifInterface.setAttribute(ExifInterface.TAG_GPS_DATESTAMP ,"0"); exifInterface.setAttribute(ExifInterface.TAG_WHITE_BALANCE ,"0"); exifInterface.setAttribute(ExifInterface.TAG_FOCAL_LENGTH ,"0"); exifInterface.setAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD ,"0"); exifInterface.saveAttributes(); } catch (IOException e) { e.printStackTrace(); } } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/utils/ImageCaptureManager.java ================================================ package me.iwf.photopicker.utils; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.text.TextUtils; import android.util.Log; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; /** * Created by donglua on 15/6/23. * * * http://developer.android.com/training/camera/photobasics.html */ public class ImageCaptureManager { private final static String CAPTURED_PHOTO_PATH_KEY = "mCurrentPhotoPath"; public static final int REQUEST_TAKE_PHOTO = 1; private String mCurrentPhotoPath; private Context mContext; public ImageCaptureManager(Context mContext) { this.mContext = mContext; } private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date()); String imageFileName = "JPEG_" + timeStamp + ".jpg"; File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); if (!storageDir.exists()) { if (!storageDir.mkdir()) { Log.e("TAG", "Throwing Errors...."); throw new IOException(); } } File image = new File(storageDir, imageFileName); // File.createTempFile( // imageFileName, /* prefix */ // ".jpg", /* suffix */ // storageDir /* directory */ // ); // Save a file: path for use with ACTION_VIEW intents mCurrentPhotoPath = image.getAbsolutePath(); return image; } public Intent dispatchTakePictureIntent() throws IOException { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Ensure that there's a camera activity to handle the intent if (takePictureIntent.resolveActivity(mContext.getPackageManager()) != null) { // Create the File where the photo should go File photoFile = createImageFile(); // Continue only if the File was successfully created if (photoFile != null) { takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); } } return takePictureIntent; } public void galleryAddPic() { Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); if (TextUtils.isEmpty(mCurrentPhotoPath)) { return; } File f = new File(mCurrentPhotoPath); Uri contentUri = Uri.fromFile(f); mediaScanIntent.setData(contentUri); mContext.sendBroadcast(mediaScanIntent); } public String getCurrentPhotoPath() { return mCurrentPhotoPath; } public void onSaveInstanceState(Bundle savedInstanceState) { if (savedInstanceState != null && mCurrentPhotoPath != null) { savedInstanceState.putString(CAPTURED_PHOTO_PATH_KEY, mCurrentPhotoPath); } } public void onRestoreInstanceState(Bundle savedInstanceState) { if (savedInstanceState != null && savedInstanceState.containsKey(CAPTURED_PHOTO_PATH_KEY)) { mCurrentPhotoPath = savedInstanceState.getString(CAPTURED_PHOTO_PATH_KEY); } } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/utils/MediaStoreHelper.java ================================================ package me.iwf.photopicker.utils; import android.content.Context; import android.database.Cursor; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; import me.iwf.photopicker.PhotoPicker; import me.iwf.photopicker.R; import me.iwf.photopicker.entity.PhotoDirectory; import static android.provider.BaseColumns._ID; import static android.provider.MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME; import static android.provider.MediaStore.Images.ImageColumns.BUCKET_ID; import static android.provider.MediaStore.MediaColumns.DATA; import static android.provider.MediaStore.MediaColumns.DATE_ADDED; /** * Created by donglua on 15/5/31. */ public class MediaStoreHelper { public final static int INDEX_ALL_PHOTOS = 0; public static void getPhotoDirs(FragmentActivity activity, Bundle args, PhotosResultCallback resultCallback) { activity.getSupportLoaderManager() .initLoader(0, args, new PhotoDirLoaderCallbacks(activity, resultCallback)); } static class PhotoDirLoaderCallbacks implements LoaderManager.LoaderCallbacks { private WeakReference context; private PhotosResultCallback resultCallback; public PhotoDirLoaderCallbacks(Context context, PhotosResultCallback resultCallback) { this.context = new WeakReference<>(context); this.resultCallback = resultCallback; } @Override public Loader onCreateLoader(int id, Bundle args) { return new PhotoDirectoryLoader(context.get(), args.getBoolean(PhotoPicker.EXTRA_SHOW_GIF, false)); } @Override public void onLoadFinished(Loader loader, Cursor data) { if (data == null) return; List directories = new ArrayList<>(); PhotoDirectory photoDirectoryAll = new PhotoDirectory(); photoDirectoryAll.setName(context.get().getString(R.string.__picker_all_image)); photoDirectoryAll.setId("ALL"); while (data.moveToNext()) { int imageId = data.getInt(data.getColumnIndexOrThrow(_ID)); String bucketId = data.getString(data.getColumnIndexOrThrow(BUCKET_ID)); String name = data.getString(data.getColumnIndexOrThrow(BUCKET_DISPLAY_NAME)); String path = data.getString(data.getColumnIndexOrThrow(DATA)); PhotoDirectory photoDirectory = new PhotoDirectory(); photoDirectory.setId(bucketId); photoDirectory.setName(name); if (!directories.contains(photoDirectory)) { photoDirectory.setCoverPath(path); photoDirectory.addPhoto(imageId, path); photoDirectory.setDateAdded(data.getLong(data.getColumnIndexOrThrow(DATE_ADDED))); directories.add(photoDirectory); } else { directories.get(directories.indexOf(photoDirectory)).addPhoto(imageId, path); } photoDirectoryAll.addPhoto(imageId, path); } if (photoDirectoryAll.getPhotoPaths().size() > 0) { photoDirectoryAll.setCoverPath(photoDirectoryAll.getPhotoPaths().get(0)); } directories.add(INDEX_ALL_PHOTOS, photoDirectoryAll); if (resultCallback != null) { resultCallback.onResultCallback(directories); } } @Override public void onLoaderReset(Loader loader) { } } public interface PhotosResultCallback { void onResultCallback(List directories); } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/utils/PhotoDirectoryLoader.java ================================================ package me.iwf.photopicker.utils; import android.content.Context; import android.net.Uri; import android.provider.MediaStore.Images.Media; import android.support.v4.content.CursorLoader; import static android.provider.MediaStore.MediaColumns.MIME_TYPE; /** * Created by 黄东鲁 on 15/6/28. */ public class PhotoDirectoryLoader extends CursorLoader { final String[] IMAGE_PROJECTION = { Media._ID, Media.DATA, Media.BUCKET_ID, Media.BUCKET_DISPLAY_NAME, Media.DATE_ADDED }; public PhotoDirectoryLoader(Context context, boolean showGif) { super(context); setProjection(IMAGE_PROJECTION); setUri(Media.EXTERNAL_CONTENT_URI); setSortOrder(Media.DATE_ADDED + " DESC"); setSelection( MIME_TYPE + "=? or " + MIME_TYPE + "=? or "+ MIME_TYPE + "=? " + (showGif ? ("or " + MIME_TYPE + "=?") : "")); String[] selectionArgs; if (showGif) { selectionArgs = new String[] { "image/jpeg", "image/png", "image/jpg","image/gif" }; } else { selectionArgs = new String[] { "image/jpeg", "image/png", "image/jpg" }; } setSelectionArgs(selectionArgs); } private PhotoDirectoryLoader(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { super(context, uri, projection, selection, selectionArgs, sortOrder); } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/utils/PhotoPickerIntent.java ================================================ package me.iwf.photopicker.utils; import android.content.Intent; import java.util.ArrayList; import static me.iwf.photopicker.PhotoPicker.EXTRA_GRID_COLUMN; import static me.iwf.photopicker.PhotoPicker.EXTRA_MAX_COUNT; import static me.iwf.photopicker.PhotoPicker.EXTRA_ORIGINAL_PHOTOS; import static me.iwf.photopicker.PhotoPicker.EXTRA_SHOW_CAMERA; import static me.iwf.photopicker.PhotoPicker.EXTRA_SHOW_GIF; /** * Created by donglua on 15/7/2. */ @Deprecated public class PhotoPickerIntent { public static void setPhotoCount(Intent intent, int photoCount) { intent.putExtra(EXTRA_MAX_COUNT, photoCount); } public static void setShowCamera(Intent intent, boolean showCamera) { intent.putExtra(EXTRA_SHOW_CAMERA, showCamera); } public static void setShowGif(Intent intent, boolean showGif) { intent.putExtra(EXTRA_SHOW_GIF, showGif); } public static void setColumn(Intent intent, int column) { intent.putExtra(EXTRA_GRID_COLUMN, column); } /** * To set some photos that have been selected before * @param intent * @param imagesUri Selected photos */ public static void setSelected(Intent intent, ArrayList imagesUri) { intent.putExtra(EXTRA_ORIGINAL_PHOTOS, imagesUri); } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/widget/MultiPickResultView.java ================================================ package me.iwf.photopicker.widget; import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Build; import android.support.annotation.IntDef; import android.support.v7.widget.OrientationHelper; import android.support.v7.widget.StaggeredGridLayoutManager; import android.util.AttributeSet; import android.widget.FrameLayout; import android.widget.Toast; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.List; import me.iwf.photopicker.PhotoPickUtils; /** * Created by Administrator on 2016/8/15 0015. */ public class MultiPickResultView extends FrameLayout { @IntDef({ACTION_SELECT, ACTION_ONLY_SHOW}) //Tell the compiler not to store annotation data in the .class file @Retention(RetentionPolicy.SOURCE) //Declare the NavigationMode annotation public @interface MultiPicAction {} public static final int ACTION_SELECT = 1;//该组件用于图片选择 public static final int ACTION_ONLY_SHOW = 2;//该组件仅用于图片显示 private int action; private int maxCount; android.support.v7.widget.RecyclerView recyclerView; PhotoAdapter photoAdapter; ArrayList selectedPhotos; public MultiPickResultView(Context context) { this(context,null,0); } public MultiPickResultView(Context context, AttributeSet attrs) { this(context, attrs,0); } public MultiPickResultView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context,attrs); initData(context,attrs); initEvent(context,attrs); } private void initEvent(Context context, AttributeSet attrs) { } private void initData(Context context, AttributeSet attrs) { } private void initView(Context context, AttributeSet attrs) { recyclerView = new android.support.v7.widget.RecyclerView(context,attrs); recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, OrientationHelper.VERTICAL)); this.addView(recyclerView); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public MultiPickResultView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { this(context, attrs, defStyleAttr); } public void init(Activity context,@MultiPicAction int action, ArrayList photos){ this.action = action; if (action == MultiPickResultView.ACTION_ONLY_SHOW){//当只用作显示图片时,一行显示3张 recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL)); } selectedPhotos = new ArrayList<>(); this.action = action; if (photos != null && photos.size() >0){ selectedPhotos.addAll(photos); } photoAdapter = new PhotoAdapter(context, selectedPhotos); photoAdapter.setAction(action); recyclerView.setAdapter(photoAdapter); //recyclerView.setLayoutFrozen(true); } public void showPics(List paths){ if (paths != null){ selectedPhotos.clear(); selectedPhotos.addAll(paths); photoAdapter.notifyDataSetChanged(); } } public void onActivityResult(int requestCode, int resultCode, Intent data){ if (action == ACTION_SELECT){ PhotoPickUtils.onActivityResult(requestCode, resultCode, data, new PhotoPickUtils.PickHandler() { @Override public void onPickSuccess(ArrayList photos) { photoAdapter.refresh(photos); } @Override public void onPreviewBack(ArrayList photos) { photoAdapter.refresh(photos); } @Override public void onPickFail(String error) { Toast.makeText(getContext(),error,Toast.LENGTH_LONG).show(); selectedPhotos.clear(); photoAdapter.notifyDataSetChanged(); } @Override public void onPickCancle() { //Toast.makeText(getContext(),"取消选择",Toast.LENGTH_LONG).show(); } }); } } public ArrayList getPhotos() { return selectedPhotos; } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/widget/PhotoAdapter.java ================================================ package me.iwf.photopicker.widget; import android.app.Activity; import android.content.Context; import android.net.Uri; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.Toast; import com.bumptech.glide.Glide; import java.io.File; import java.util.ArrayList; import me.iwf.photopicker.PhotoPickUtils; import me.iwf.photopicker.PhotoPreview; import me.iwf.photopicker.R; /** * Created by donglua on 15/5/31. */ public class PhotoAdapter extends RecyclerView.Adapter { private ArrayList photoPaths ; private LayoutInflater inflater; private Context mContext; public void setAction(@MultiPickResultView.MultiPicAction int action) { this.action = action; } private int action; public PhotoAdapter(Context mContext, ArrayList photoPaths) { this.photoPaths = photoPaths; this.mContext = mContext; inflater = LayoutInflater.from(mContext); padding = dip2Px(8); } public void add(ArrayList photoPaths){ if (photoPaths != null && photoPaths.size() >0){ this.photoPaths.addAll(photoPaths); notifyDataSetChanged(); } } public void refresh(ArrayList photoPaths){ this.photoPaths.clear(); if (photoPaths != null && photoPaths.size() >0){ this.photoPaths.addAll(photoPaths); } notifyDataSetChanged(); } @Override public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = inflater.inflate(R.layout.__picker_item_photo, parent, false); return new PhotoViewHolder(itemView); } public int dip2Px(int dip) { // px/dip = density; float density = mContext.getResources().getDisplayMetrics().density; int px = (int) (dip * density + .5f); return px; } int padding; @Override public void onBindViewHolder(final PhotoViewHolder holder, final int position) { if (action == MultiPickResultView.ACTION_SELECT){ // RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.ivPhoto.getLayoutParams(); holder.ivPhoto.setPadding(padding,padding,padding,padding); if (position == getItemCount() -1){//最后一个始终是+号,点击能够跳去添加图片 Glide.with(mContext) .load("") .centerCrop() .thumbnail(0.1f) .placeholder(R.drawable.icon_pic_default) .error(R.drawable.icon_pic_default) .into(holder.ivPhoto); holder.ivPhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (photoPaths != null && photoPaths.size() ==9){ Toast.makeText(mContext,"已选了9张图片",Toast.LENGTH_SHORT).show(); }else { PhotoPickUtils.startPick((Activity) mContext,false,9,photoPaths); } } }); holder.deleteBtn.setVisibility(View.GONE); }else { String str = photoPaths.get(position); Log.e("file",str); Uri uri = Uri.fromFile(new File(photoPaths.get(position))); Glide.with(mContext) .load(uri) .centerCrop() .thumbnail(0.1f) // .bitmapTransform(new RoundedCornersTransformation(mContext,6,0)) .placeholder(R.drawable.__picker_default_weixin) .error(R.drawable.__picker_ic_broken_image_black_48dp) .into(holder.ivPhoto); holder.deleteBtn.setVisibility(View.VISIBLE); holder.deleteBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { photoPaths.remove(position); notifyDataSetChanged(); } }); holder.ivPhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PhotoPreview.builder() .setPhotos(photoPaths) .setAction(action) .setCurrentItem(position) .start((Activity) mContext); } }); } }else if (action == MultiPickResultView.ACTION_ONLY_SHOW){ //Uri uri = Uri.fromFile(new File(photoPaths.get(position))); //Uri uri = Uri.parse(photoPaths.get(position)); Log.e("pic",photoPaths.get(position)); Glide.with(mContext) .load(photoPaths.get(position)) .centerCrop() .thumbnail(0.1f) // .bitmapTransform(new RoundedCornersTransformation(mContext,4,0)) .placeholder(R.drawable.__picker_default_weixin) .error(R.drawable.__picker_ic_broken_image_black_48dp) .into(holder.ivPhoto); holder.ivPhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PhotoPreview.builder() .setPhotos(photoPaths) .setAction(action) .setCurrentItem(position) .start((Activity) mContext); } }); } } @Override public int getItemCount() { return action == MultiPickResultView.ACTION_SELECT ? photoPaths.size()+1 : photoPaths.size(); } public static class PhotoViewHolder extends RecyclerView.ViewHolder { private ImageView ivPhoto; private View vSelected; public View cover; public View deleteBtn; public PhotoViewHolder(View itemView) { super(itemView); ivPhoto = (ImageView) itemView.findViewById(R.id.iv_photo); vSelected = itemView.findViewById(R.id.v_selected); vSelected.setVisibility(View.GONE); cover = itemView.findViewById(R.id.cover); cover.setVisibility(View.GONE); deleteBtn = itemView.findViewById(R.id.v_delete); deleteBtn.setVisibility(View.GONE); } } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/widget/SquareItemLayout.java ================================================ package me.iwf.photopicker.widget; import android.content.Context; import android.util.AttributeSet; import android.widget.RelativeLayout; /** * Created by donglua on 15/6/21. */ public class SquareItemLayout extends RelativeLayout { public SquareItemLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public SquareItemLayout(Context context, AttributeSet attrs) { super(context, attrs); } public SquareItemLayout(Context context) { super(context); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec)); int childWidthSize = getMeasuredWidth(); heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/widget/Titlebar.java ================================================ package me.iwf.photopicker.widget; import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.os.Build; import android.text.TextUtils; import android.util.AttributeSet; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import me.iwf.photopicker.R; /** * Created by Administrator on 2016/8/5. */ public class Titlebar extends FrameLayout { private RelativeLayout rootView; private TextView tvLeft; private ImageView ivLeft; public TextView getTvTitle() { return tvTitle; } public ImageView getIvLeft() { return ivLeft; } public TextView getTvLeft() { return tvLeft; } public TextView getTvRight() { return tvRight; } public ImageView getIvRight() { return ivRight; } @Override public RelativeLayout getRootView() { return rootView; } private TextView tvTitle; private TextView tvRight; private ImageView ivRight; private OnClickListener leftOnclickListener; private OnClickListener rightOnclickListener; private Activity mActivity; public Titlebar(Context context) { this(context,null); } public Titlebar(Context context, AttributeSet attrs) { this(context, attrs,0); } public Titlebar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); initData(context, attrs,defStyleAttr); initEvent(context,attrs,defStyleAttr); } public void init(Activity activity){ mActivity = activity; leftOnclickListener = new OnClickListener() { @Override public void onClick(View view) { mActivity.finish(); } }; ivLeft.setOnClickListener(leftOnclickListener); } private void initEvent(Context context, AttributeSet attrs, int defStyleAttr) { if (context instanceof Activity){ final Activity activity = (Activity) context; leftOnclickListener = new OnClickListener() { @Override public void onClick(View view) { activity.finish(); } }; } } public void setLeftOnclickListener(OnClickListener listener){ if (listener != null){ leftOnclickListener = listener; ivLeft.setOnClickListener(leftOnclickListener); tvLeft.setOnClickListener(leftOnclickListener); } } public void setRightOnclickListener(OnClickListener listener){ if (listener != null){ rightOnclickListener = listener; ivRight.setOnClickListener(rightOnclickListener); tvRight.setOnClickListener(rightOnclickListener); } } public void setTitle(String title){ if (!TextUtils.isEmpty(title)){ tvTitle.setText(title); tvTitle.setVisibility(VISIBLE); } } public void setLeft(Drawable leftDrawable,String leftTxt,OnClickListener listener){ if (leftDrawable != null){ ivLeft.setVisibility(VISIBLE); ivLeft.setImageDrawable(leftDrawable); tvLeft.setVisibility(GONE); }else if (!TextUtils.isEmpty(leftTxt)){ tvLeft.setVisibility(VISIBLE); tvLeft.setText(leftTxt); ivLeft.setVisibility(GONE); }else {//all not set,default } if (listener != null){ leftOnclickListener = listener; } } public void setRitht(Drawable rightDrawable,String rightTxt,OnClickListener listener){ if (!TextUtils.isEmpty(rightTxt)){ tvRight.setVisibility(VISIBLE); tvRight.setText(rightTxt); ivRight.setVisibility(GONE); if (listener != null){ rightOnclickListener = listener; tvRight.setOnClickListener(rightOnclickListener); } }else if (rightDrawable != null){ ivRight.setVisibility(VISIBLE); tvRight.setVisibility(GONE); ivRight.setImageDrawable(rightDrawable); if (listener != null){ rightOnclickListener = listener; ivRight.setOnClickListener(rightOnclickListener); } }else { } if (listener != null){ rightOnclickListener = listener; ivRight.setOnClickListener(rightOnclickListener); } } private void initData(Context context, AttributeSet attrs, int defStyleAttr) { TypedArray typedArray = null; try { typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTitlebar); String leftTxt = typedArray.getString(R.styleable.MyTitlebar_mtb_leftTxt); String title = typedArray.getString(R.styleable.MyTitlebar_mtb_title); String rightTxt = typedArray.getString(R.styleable.MyTitlebar_mtb_rightTxt); Drawable leftDrawable = typedArray.getDrawable(R.styleable.MyTitlebar_mtb_left_icon); Drawable rightDrawable = typedArray.getDrawable(R.styleable.MyTitlebar_mtb_right_icon); //left:drawable first setLeft(leftDrawable,leftTxt,null); //center setTitle(title); //right: text first setRitht(rightDrawable,rightTxt,null); } finally { if (typedArray != null) { typedArray.recycle(); } } } private void initView(Context context) { rootView = (RelativeLayout) View.inflate(context,R.layout.view_titlebar,null); ivLeft = (ImageView) rootView.findViewById(R.id.iv_left); tvLeft = (TextView) rootView.findViewById(R.id.tv_left); tvTitle = (TextView) rootView.findViewById(R.id.tv_title); ivRight = (ImageView) rootView.findViewById(R.id.iv_right); tvRight = (TextView) rootView.findViewById(R.id.tv_right); this.addView(rootView); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public Titlebar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { this(context, attrs, defStyleAttr); } } ================================================ FILE: PhotoPicker/src/main/java/me/iwf/photopicker/widget/TouchImageView.java ================================================ /* * TouchImageView.java * By: Michael Ortiz * Updated By: Patrick Lackemacher * Updated By: Babay88 * Updated By: @ipsilondev * Updated By: hank-cp * Updated By: singpolyma * ------------------- * Extends Android ImageView to include pinch zooming, panning, fling and double tap zoom. */ package me.iwf.photopicker.widget; import android.annotation.TargetApi; import android.content.Context; import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.PointF; import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; import android.os.Bundle; import android.os.Parcelable; import android.util.AttributeSet; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.view.View; import android.view.animation.AccelerateDecelerateInterpolator; import android.widget.ImageView; import android.widget.OverScroller; import android.widget.Scroller; public class TouchImageView extends ImageView { private static final String DEBUG = "DEBUG"; // // SuperMin and SuperMax multipliers. Determine how much the image can be // zoomed below or above the zoom boundaries, before animating back to the // min/max zoom boundary. // private static final float SUPER_MIN_MULTIPLIER = .75f; private static final float SUPER_MAX_MULTIPLIER = 1.25f; // // Scale of image ranges from minScale to maxScale, where minScale == 1 // when the image is stretched to fit view. // private float normalizedScale; // // Matrix applied to image. MSCALE_X and MSCALE_Y should always be equal. // MTRANS_X and MTRANS_Y are the other values used. prevMatrix is the matrix // saved prior to the screen rotating. // private Matrix matrix, prevMatrix; private enum State {NONE, DRAG, ZOOM, FLING, ANIMATE_ZOOM} ; private State state; private float minScale; private float maxScale; private float superMinScale; private float superMaxScale; private float[] m; private Context context; private Fling fling; private ScaleType mScaleType; private boolean imageRenderedAtLeastOnce; private boolean onDrawReady; private ZoomVariables delayedZoomVariables; // // Size of view and previous view size (ie before rotation) // private int viewWidth, viewHeight, prevViewWidth, prevViewHeight; // // Size of image when it is stretched to fit view. Before and After rotation. // private float matchViewWidth, matchViewHeight, prevMatchViewWidth, prevMatchViewHeight; private ScaleGestureDetector mScaleDetector; private GestureDetector mGestureDetector; private GestureDetector.OnDoubleTapListener doubleTapListener = null; private OnTouchListener userTouchListener = null; private OnTouchImageViewListener touchImageViewListener = null; public TouchImageView(Context context) { super(context); sharedConstructing(context); } public TouchImageView(Context context, AttributeSet attrs) { super(context, attrs); sharedConstructing(context); } public TouchImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); sharedConstructing(context); } private void sharedConstructing(Context context) { super.setClickable(true); this.context = context; mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); mGestureDetector = new GestureDetector(context, new GestureListener()); matrix = new Matrix(); prevMatrix = new Matrix(); m = new float[9]; normalizedScale = 1; if (mScaleType == null) { mScaleType = ScaleType.FIT_CENTER; } minScale = 1; maxScale = 3; superMinScale = SUPER_MIN_MULTIPLIER * minScale; superMaxScale = SUPER_MAX_MULTIPLIER * maxScale; setImageMatrix(matrix); setScaleType(ScaleType.MATRIX); setState(State.NONE); onDrawReady = false; super.setOnTouchListener(new PrivateOnTouchListener()); } @Override public void setOnTouchListener(View.OnTouchListener l) { userTouchListener = l; } public void setOnTouchImageViewListener(OnTouchImageViewListener l) { touchImageViewListener = l; } public void setOnDoubleTapListener(GestureDetector.OnDoubleTapListener l) { doubleTapListener = l; } @Override public void setImageResource(int resId) { super.setImageResource(resId); savePreviousImageValues(); fitImageToView(); } @Override public void setImageBitmap(Bitmap bm) { super.setImageBitmap(bm); savePreviousImageValues(); fitImageToView(); } @Override public void setImageDrawable(Drawable drawable) { super.setImageDrawable(drawable); savePreviousImageValues(); fitImageToView(); } @Override public void setImageURI(Uri uri) { super.setImageURI(uri); savePreviousImageValues(); fitImageToView(); } @Override public void setScaleType(ScaleType type) { if (type == ScaleType.FIT_START || type == ScaleType.FIT_END) { throw new UnsupportedOperationException( "TouchImageView does not support FIT_START or FIT_END"); } if (type == ScaleType.MATRIX) { super.setScaleType(ScaleType.MATRIX); } else { mScaleType = type; if (onDrawReady) { // // If the image is already rendered, scaleType has been called programmatically // and the TouchImageView should be updated with the new scaleType. // setZoom(this); } } } @Override public ScaleType getScaleType() { return mScaleType; } /** * Returns false if image is in initial, unzoomed state. False, otherwise. * * @return true if image is zoomed */ public boolean isZoomed() { return normalizedScale != 1; } /** * Return a Rect representing the zoomed image. * * @return rect representing zoomed image */ public RectF getZoomedRect() { if (mScaleType == ScaleType.FIT_XY) { throw new UnsupportedOperationException("getZoomedRect() not supported with FIT_XY"); } PointF topLeft = transformCoordTouchToBitmap(0, 0, true); PointF bottomRight = transformCoordTouchToBitmap(viewWidth, viewHeight, true); float w = getDrawable().getIntrinsicWidth(); float h = getDrawable().getIntrinsicHeight(); return new RectF(topLeft.x / w, topLeft.y / h, bottomRight.x / w, bottomRight.y / h); } /** * Save the current matrix and view dimensions * in the prevMatrix and prevView variables. */ private void savePreviousImageValues() { if (matrix != null && viewHeight != 0 && viewWidth != 0) { matrix.getValues(m); prevMatrix.setValues(m); prevMatchViewHeight = matchViewHeight; prevMatchViewWidth = matchViewWidth; prevViewHeight = viewHeight; prevViewWidth = viewWidth; } } @Override public Parcelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putParcelable("instanceState", super.onSaveInstanceState()); bundle.putFloat("saveScale", normalizedScale); bundle.putFloat("matchViewHeight", matchViewHeight); bundle.putFloat("matchViewWidth", matchViewWidth); bundle.putInt("viewWidth", viewWidth); bundle.putInt("viewHeight", viewHeight); matrix.getValues(m); bundle.putFloatArray("matrix", m); bundle.putBoolean("imageRendered", imageRenderedAtLeastOnce); return bundle; } @Override public void onRestoreInstanceState(Parcelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; normalizedScale = bundle.getFloat("saveScale"); m = bundle.getFloatArray("matrix"); prevMatrix.setValues(m); prevMatchViewHeight = bundle.getFloat("matchViewHeight"); prevMatchViewWidth = bundle.getFloat("matchViewWidth"); prevViewHeight = bundle.getInt("viewHeight"); prevViewWidth = bundle.getInt("viewWidth"); imageRenderedAtLeastOnce = bundle.getBoolean("imageRendered"); super.onRestoreInstanceState(bundle.getParcelable("instanceState")); return; } super.onRestoreInstanceState(state); } @Override protected void onDraw(Canvas canvas) { onDrawReady = true; imageRenderedAtLeastOnce = true; if (delayedZoomVariables != null) { setZoom(delayedZoomVariables.scale, delayedZoomVariables.focusX, delayedZoomVariables.focusY, delayedZoomVariables.scaleType); delayedZoomVariables = null; } super.onDraw(canvas); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); savePreviousImageValues(); } /** * Get the max zoom multiplier. * * @return max zoom multiplier. */ public float getMaxZoom() { return maxScale; } /** * Set the max zoom multiplier. Default value: 3. * * @param max max zoom multiplier. */ public void setMaxZoom(float max) { maxScale = max; superMaxScale = SUPER_MAX_MULTIPLIER * maxScale; } /** * Get the min zoom multiplier. * * @return min zoom multiplier. */ public float getMinZoom() { return minScale; } /** * Get the current zoom. This is the zoom relative to the initial * scale, not the original resource. * * @return current zoom multiplier. */ public float getCurrentZoom() { return normalizedScale; } /** * Set the min zoom multiplier. Default value: 1. * * @param min min zoom multiplier. */ public void setMinZoom(float min) { minScale = min; superMinScale = SUPER_MIN_MULTIPLIER * minScale; } /** * Reset zoom and translation to initial state. */ public void resetZoom() { normalizedScale = 1; fitImageToView(); } /** * Set zoom to the specified scale. Image will be centered by default. */ public void setZoom(float scale) { setZoom(scale, 0.5f, 0.5f); } /** * Set zoom to the specified scale. Image will be centered around the point * (focusX, focusY). These floats range from 0 to 1 and denote the focus point * as a fraction from the left and top of the view. For example, the top left * corner of the image would be (0, 0). And the bottom right corner would be (1, 1). */ public void setZoom(float scale, float focusX, float focusY) { setZoom(scale, focusX, focusY, mScaleType); } /** * Set zoom to the specified scale. Image will be centered around the point * (focusX, focusY). These floats range from 0 to 1 and denote the focus point * as a fraction from the left and top of the view. For example, the top left * corner of the image would be (0, 0). And the bottom right corner would be (1, 1). */ public void setZoom(float scale, float focusX, float focusY, ScaleType scaleType) { // // setZoom can be called before the image is on the screen, but at this point, // image and view sizes have not yet been calculated in onMeasure. Thus, we should // delay calling setZoom until the view has been measured. // if (!onDrawReady) { delayedZoomVariables = new ZoomVariables(scale, focusX, focusY, scaleType); return; } if (scaleType != mScaleType) { setScaleType(scaleType); } resetZoom(); scaleImage(scale, viewWidth / 2, viewHeight / 2, true); matrix.getValues(m); m[Matrix.MTRANS_X] = -((focusX * getImageWidth()) - (viewWidth * 0.5f)); m[Matrix.MTRANS_Y] = -((focusY * getImageHeight()) - (viewHeight * 0.5f)); matrix.setValues(m); fixTrans(); setImageMatrix(matrix); } /** * Set zoom parameters equal to another TouchImageView. Including scale, position, * and ScaleType. */ public void setZoom(TouchImageView img) { PointF center = img.getScrollPosition(); setZoom(img.getCurrentZoom(), center.x, center.y, img.getScaleType()); } /** * Return the point at the center of the zoomed image. The PointF coordinates range * in value between 0 and 1 and the focus point is denoted as a fraction from the left * and top of the view. For example, the top left corner of the image would be (0, 0). * And the bottom right corner would be (1, 1). * * @return PointF representing the scroll position of the zoomed image. */ public PointF getScrollPosition() { Drawable drawable = getDrawable(); if (drawable == null) { return null; } int drawableWidth = drawable.getIntrinsicWidth(); int drawableHeight = drawable.getIntrinsicHeight(); PointF point = transformCoordTouchToBitmap(viewWidth / 2, viewHeight / 2, true); point.x /= drawableWidth; point.y /= drawableHeight; return point; } /** * Set the focus point of the zoomed image. The focus points are denoted as a fraction from the * left and top of the view. The focus points can range in value between 0 and 1. */ public void setScrollPosition(float focusX, float focusY) { setZoom(normalizedScale, focusX, focusY); } /** * Performs boundary checking and fixes the image matrix if it * is out of bounds. */ private void fixTrans() { matrix.getValues(m); float transX = m[Matrix.MTRANS_X]; float transY = m[Matrix.MTRANS_Y]; float fixTransX = getFixTrans(transX, viewWidth, getImageWidth()); float fixTransY = getFixTrans(transY, viewHeight, getImageHeight()); if (fixTransX != 0 || fixTransY != 0) { matrix.postTranslate(fixTransX, fixTransY); } } /** * When transitioning from zooming from focus to zoom from center (or vice versa) * the image can become unaligned within the view. This is apparent when zooming * quickly. When the content size is less than the view size, the content will often * be centered incorrectly within the view. fixScaleTrans first calls fixTrans() and * then makes sure the image is centered correctly within the view. */ private void fixScaleTrans() { fixTrans(); matrix.getValues(m); if (getImageWidth() < viewWidth) { m[Matrix.MTRANS_X] = (viewWidth - getImageWidth()) / 2; } if (getImageHeight() < viewHeight) { m[Matrix.MTRANS_Y] = (viewHeight - getImageHeight()) / 2; } matrix.setValues(m); } private float getFixTrans(float trans, float viewSize, float contentSize) { float minTrans, maxTrans; if (contentSize <= viewSize) { minTrans = 0; maxTrans = viewSize - contentSize; } else { minTrans = viewSize - contentSize; maxTrans = 0; } if (trans < minTrans) return -trans + minTrans; if (trans > maxTrans) return -trans + maxTrans; return 0; } private float getFixDragTrans(float delta, float viewSize, float contentSize) { if (contentSize <= viewSize) { return 0; } return delta; } private float getImageWidth() { return matchViewWidth * normalizedScale; } private float getImageHeight() { return matchViewHeight * normalizedScale; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Drawable drawable = getDrawable(); if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) { setMeasuredDimension(0, 0); return; } int drawableWidth = drawable.getIntrinsicWidth(); int drawableHeight = drawable.getIntrinsicHeight(); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); viewWidth = setViewSize(widthMode, widthSize, drawableWidth); viewHeight = setViewSize(heightMode, heightSize, drawableHeight); // // Set view dimensions // setMeasuredDimension(viewWidth, viewHeight); // // Fit content within view // fitImageToView(); } /** * If the normalizedScale is equal to 1, then the image is made to fit the screen. Otherwise, * it is made to fit the screen according to the dimensions of the previous image matrix. This * allows the image to maintain its zoom after rotation. */ private void fitImageToView() { Drawable drawable = getDrawable(); if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) { return; } if (matrix == null || prevMatrix == null) { return; } int drawableWidth = drawable.getIntrinsicWidth(); int drawableHeight = drawable.getIntrinsicHeight(); // // Scale image for view // float scaleX = (float) viewWidth / drawableWidth; float scaleY = (float) viewHeight / drawableHeight; switch (mScaleType) { case CENTER: scaleX = scaleY = 1; break; case CENTER_CROP: scaleX = scaleY = Math.max(scaleX, scaleY); break; case CENTER_INSIDE: scaleX = scaleY = Math.min(1, Math.min(scaleX, scaleY)); case FIT_CENTER: scaleX = scaleY = Math.min(scaleX, scaleY); break; case FIT_XY: break; default: // // FIT_START and FIT_END not supported // throw new UnsupportedOperationException( "TouchImageView does not support FIT_START or FIT_END"); } // // Center the image // float redundantXSpace = viewWidth - (scaleX * drawableWidth); float redundantYSpace = viewHeight - (scaleY * drawableHeight); matchViewWidth = viewWidth - redundantXSpace; matchViewHeight = viewHeight - redundantYSpace; if (!isZoomed() && !imageRenderedAtLeastOnce) { // // Stretch and center image to fit view // matrix.setScale(scaleX, scaleY); matrix.postTranslate(redundantXSpace / 2, redundantYSpace / 2); normalizedScale = 1; } else { // // These values should never be 0 or we will set viewWidth and viewHeight // to NaN in translateMatrixAfterRotate. To avoid this, call savePreviousImageValues // to set them equal to the current values. // if (prevMatchViewWidth == 0 || prevMatchViewHeight == 0) { savePreviousImageValues(); } prevMatrix.getValues(m); // // Rescale Matrix after rotation // m[Matrix.MSCALE_X] = matchViewWidth / drawableWidth * normalizedScale; m[Matrix.MSCALE_Y] = matchViewHeight / drawableHeight * normalizedScale; // // TransX and TransY from previous matrix // float transX = m[Matrix.MTRANS_X]; float transY = m[Matrix.MTRANS_Y]; // // Width // float prevActualWidth = prevMatchViewWidth * normalizedScale; float actualWidth = getImageWidth(); translateMatrixAfterRotate(Matrix.MTRANS_X, transX, prevActualWidth, actualWidth, prevViewWidth, viewWidth, drawableWidth); // // Height // float prevActualHeight = prevMatchViewHeight * normalizedScale; float actualHeight = getImageHeight(); translateMatrixAfterRotate(Matrix.MTRANS_Y, transY, prevActualHeight, actualHeight, prevViewHeight, viewHeight, drawableHeight); // // Set the matrix to the adjusted scale and translate values. // matrix.setValues(m); } fixTrans(); setImageMatrix(matrix); } /** * Set view dimensions based on layout params */ private int setViewSize(int mode, int size, int drawableWidth) { int viewSize; switch (mode) { case MeasureSpec.EXACTLY: viewSize = size; break; case MeasureSpec.AT_MOST: viewSize = Math.min(drawableWidth, size); break; case MeasureSpec.UNSPECIFIED: viewSize = drawableWidth; break; default: viewSize = size; break; } return viewSize; } /** * After rotating, the matrix needs to be translated. This function finds the area of image * which was previously centered and adjusts translations so that is again the center, * post-rotation. * * @param axis Matrix.MTRANS_X or Matrix.MTRANS_Y * @param trans the value of trans in that axis before the rotation * @param prevImageSize the width/height of the image before the rotation * @param imageSize width/height of the image after rotation * @param prevViewSize width/height of view before rotation * @param viewSize width/height of view after rotation * @param drawableSize width/height of drawable */ private void translateMatrixAfterRotate(int axis, float trans, float prevImageSize, float imageSize, int prevViewSize, int viewSize, int drawableSize) { if (imageSize < viewSize) { // // The width/height of image is less than the view's width/height. Center it. // m[axis] = (viewSize - (drawableSize * m[Matrix.MSCALE_X])) * 0.5f; } else if (trans > 0) { // // The image is larger than the view, but was not before rotation. Center it. // m[axis] = -((imageSize - viewSize) * 0.5f); } else { // // Find the area of the image which was previously centered in the view. Determine its distance // from the left/top side of the view as a fraction of the entire image's width/height. Use that percentage // to calculate the trans in the new view width/height. // float percentage = (Math.abs(trans) + (0.5f * prevViewSize)) / prevImageSize; m[axis] = -((percentage * imageSize) - (viewSize * 0.5f)); } } private void setState(State state) { this.state = state; } public boolean canScrollHorizontallyFroyo(int direction) { return canScrollHorizontally(direction); } @Override public boolean canScrollHorizontally(int direction) { matrix.getValues(m); float x = m[Matrix.MTRANS_X]; if (getImageWidth() < viewWidth) { return false; } else if (x >= -1 && direction < 0) { return false; } else if (Math.abs(x) + viewWidth + 1 >= getImageWidth() && direction > 0) { return false; } return true; } /** * Gesture Listener detects a single click or long click and passes that on * to the view's listener. * * @author Ortiz */ private class GestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onSingleTapConfirmed(MotionEvent e) { if (doubleTapListener != null) { return doubleTapListener.onSingleTapConfirmed(e); } return performClick(); } @Override public void onLongPress(MotionEvent e) { performLongClick(); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (fling != null) { // // If a previous fling is still active, it should be cancelled so that two flings // are not run simultaenously. // fling.cancelFling(); } fling = new Fling((int) velocityX, (int) velocityY); compatPostOnAnimation(fling); return super.onFling(e1, e2, velocityX, velocityY); } @Override public boolean onDoubleTap(MotionEvent e) { boolean consumed = false; if (doubleTapListener != null) { consumed = doubleTapListener.onDoubleTap(e); } if (state == State.NONE) { float targetZoom = (normalizedScale == minScale) ? maxScale : minScale; DoubleTapZoom doubleTap = new DoubleTapZoom(targetZoom, e.getX(), e.getY(), false); compatPostOnAnimation(doubleTap); consumed = true; } return consumed; } @Override public boolean onDoubleTapEvent(MotionEvent e) { if (doubleTapListener != null) { return doubleTapListener.onDoubleTapEvent(e); } return false; } } public interface OnTouchImageViewListener { public void onMove(); } /** * Responsible for all touch events. Handles the heavy lifting of drag and also sends * touch events to Scale Detector and Gesture Detector. * * @author Ortiz */ private class PrivateOnTouchListener implements OnTouchListener { // // Remember last point position for dragging // private PointF last = new PointF(); @Override public boolean onTouch(View v, MotionEvent event) { mScaleDetector.onTouchEvent(event); mGestureDetector.onTouchEvent(event); PointF curr = new PointF(event.getX(), event.getY()); if (state == State.NONE || state == State.DRAG || state == State.FLING) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: last.set(curr); if (fling != null) fling.cancelFling(); setState(State.DRAG); break; case MotionEvent.ACTION_MOVE: if (state == State.DRAG) { float deltaX = curr.x - last.x; float deltaY = curr.y - last.y; float fixTransX = getFixDragTrans(deltaX, viewWidth, getImageWidth()); float fixTransY = getFixDragTrans(deltaY, viewHeight, getImageHeight()); matrix.postTranslate(fixTransX, fixTransY); fixTrans(); last.set(curr.x, curr.y); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: setState(State.NONE); break; } } setImageMatrix(matrix); // // User-defined OnTouchListener // if (userTouchListener != null) { userTouchListener.onTouch(v, event); } // // OnTouchImageViewListener is set: TouchImageView dragged by user. // if (touchImageViewListener != null) { touchImageViewListener.onMove(); } // // indicate event was handled // return true; } } /** * ScaleListener detects user two finger scaling and scales image. * * @author Ortiz */ private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { @Override public boolean onScaleBegin(ScaleGestureDetector detector) { setState(State.ZOOM); return true; } @Override public boolean onScale(ScaleGestureDetector detector) { scaleImage(detector.getScaleFactor(), detector.getFocusX(), detector.getFocusY(), true); // // OnTouchImageViewListener is set: TouchImageView pinch zoomed by user. // if (touchImageViewListener != null) { touchImageViewListener.onMove(); } return true; } @Override public void onScaleEnd(ScaleGestureDetector detector) { super.onScaleEnd(detector); setState(State.NONE); boolean animateToZoomBoundary = false; float targetZoom = normalizedScale; if (normalizedScale > maxScale) { targetZoom = maxScale; animateToZoomBoundary = true; } else if (normalizedScale < minScale) { targetZoom = minScale; animateToZoomBoundary = true; } if (animateToZoomBoundary) { DoubleTapZoom doubleTap = new DoubleTapZoom(targetZoom, viewWidth / 2, viewHeight / 2, true); compatPostOnAnimation(doubleTap); } } } private void scaleImage(double deltaScale, float focusX, float focusY, boolean stretchImageToSuper) { float lowerScale, upperScale; if (stretchImageToSuper) { lowerScale = superMinScale; upperScale = superMaxScale; } else { lowerScale = minScale; upperScale = maxScale; } float origScale = normalizedScale; normalizedScale *= deltaScale; if (normalizedScale > upperScale) { normalizedScale = upperScale; deltaScale = upperScale / origScale; } else if (normalizedScale < lowerScale) { normalizedScale = lowerScale; deltaScale = lowerScale / origScale; } matrix.postScale((float) deltaScale, (float) deltaScale, focusX, focusY); fixScaleTrans(); } /** * DoubleTapZoom calls a series of runnables which apply * an animated zoom in/out graphic to the image. * * @author Ortiz */ private class DoubleTapZoom implements Runnable { private long startTime; private static final float ZOOM_TIME = 500; private float startZoom, targetZoom; private float bitmapX, bitmapY; private boolean stretchImageToSuper; private AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator(); private PointF startTouch; private PointF endTouch; DoubleTapZoom(float targetZoom, float focusX, float focusY, boolean stretchImageToSuper) { setState(State.ANIMATE_ZOOM); startTime = System.currentTimeMillis(); this.startZoom = normalizedScale; this.targetZoom = targetZoom; this.stretchImageToSuper = stretchImageToSuper; PointF bitmapPoint = transformCoordTouchToBitmap(focusX, focusY, false); this.bitmapX = bitmapPoint.x; this.bitmapY = bitmapPoint.y; // // Used for translating image during scaling // startTouch = transformCoordBitmapToTouch(bitmapX, bitmapY); endTouch = new PointF(viewWidth / 2, viewHeight / 2); } @Override public void run() { float t = interpolate(); double deltaScale = calculateDeltaScale(t); scaleImage(deltaScale, bitmapX, bitmapY, stretchImageToSuper); translateImageToCenterTouchPosition(t); fixScaleTrans(); setImageMatrix(matrix); // // OnTouchImageViewListener is set: double tap runnable updates listener // with every frame. // if (touchImageViewListener != null) { touchImageViewListener.onMove(); } if (t < 1f) { // // We haven't finished zooming // compatPostOnAnimation(this); } else { // // Finished zooming // setState(State.NONE); } } /** * Interpolate between where the image should start and end in order to translate * the image so that the point that is touched is what ends up centered at the end * of the zoom. */ private void translateImageToCenterTouchPosition(float t) { float targetX = startTouch.x + t * (endTouch.x - startTouch.x); float targetY = startTouch.y + t * (endTouch.y - startTouch.y); PointF curr = transformCoordBitmapToTouch(bitmapX, bitmapY); matrix.postTranslate(targetX - curr.x, targetY - curr.y); } /** * Use interpolator to get t */ private float interpolate() { long currTime = System.currentTimeMillis(); float elapsed = (currTime - startTime) / ZOOM_TIME; elapsed = Math.min(1f, elapsed); return interpolator.getInterpolation(elapsed); } /** * Interpolate the current targeted zoom and get the delta * from the current zoom. */ private double calculateDeltaScale(float t) { double zoom = startZoom + t * (targetZoom - startZoom); return zoom / normalizedScale; } } /** * This function will transform the coordinates in the touch event to the coordinate * system of the drawable that the imageview contain * * @param x x-coordinate of touch event * @param y y-coordinate of touch event * @param clipToBitmap Touch event may occur within view, but outside image content. True, to clip * return value * to the bounds of the bitmap size. * @return Coordinates of the point touched, in the coordinate system of the original drawable. */ private PointF transformCoordTouchToBitmap(float x, float y, boolean clipToBitmap) { matrix.getValues(m); float origW = getDrawable().getIntrinsicWidth(); float origH = getDrawable().getIntrinsicHeight(); float transX = m[Matrix.MTRANS_X]; float transY = m[Matrix.MTRANS_Y]; float finalX = ((x - transX) * origW) / getImageWidth(); float finalY = ((y - transY) * origH) / getImageHeight(); if (clipToBitmap) { finalX = Math.min(Math.max(finalX, 0), origW); finalY = Math.min(Math.max(finalY, 0), origH); } return new PointF(finalX, finalY); } /** * Inverse of transformCoordTouchToBitmap. This function will transform the coordinates in the * drawable's coordinate system to the view's coordinate system. * * @param bx x-coordinate in original bitmap coordinate system * @param by y-coordinate in original bitmap coordinate system * @return Coordinates of the point in the view's coordinate system. */ private PointF transformCoordBitmapToTouch(float bx, float by) { matrix.getValues(m); float origW = getDrawable().getIntrinsicWidth(); float origH = getDrawable().getIntrinsicHeight(); float px = bx / origW; float py = by / origH; float finalX = m[Matrix.MTRANS_X] + getImageWidth() * px; float finalY = m[Matrix.MTRANS_Y] + getImageHeight() * py; return new PointF(finalX, finalY); } /** * Fling launches sequential runnables which apply * the fling graphic to the image. The values for the translation * are interpolated by the Scroller. * * @author Ortiz */ private class Fling implements Runnable { CompatScroller scroller; int currX, currY; Fling(int velocityX, int velocityY) { setState(State.FLING); scroller = new CompatScroller(context); matrix.getValues(m); int startX = (int) m[Matrix.MTRANS_X]; int startY = (int) m[Matrix.MTRANS_Y]; int minX, maxX, minY, maxY; if (getImageWidth() > viewWidth) { minX = viewWidth - (int) getImageWidth(); maxX = 0; } else { minX = maxX = startX; } if (getImageHeight() > viewHeight) { minY = viewHeight - (int) getImageHeight(); maxY = 0; } else { minY = maxY = startY; } scroller.fling(startX, startY, (int) velocityX, (int) velocityY, minX, maxX, minY, maxY); currX = startX; currY = startY; } public void cancelFling() { if (scroller != null) { setState(State.NONE); scroller.forceFinished(true); } } @Override public void run() { // // OnTouchImageViewListener is set: TouchImageView listener has been flung by user. // Listener runnable updated with each frame of fling animation. // if (touchImageViewListener != null) { touchImageViewListener.onMove(); } if (scroller.isFinished()) { scroller = null; return; } if (scroller.computeScrollOffset()) { int newX = scroller.getCurrX(); int newY = scroller.getCurrY(); int transX = newX - currX; int transY = newY - currY; currX = newX; currY = newY; matrix.postTranslate(transX, transY); fixTrans(); setImageMatrix(matrix); compatPostOnAnimation(this); } } } @TargetApi(Build.VERSION_CODES.GINGERBREAD) private class CompatScroller { Scroller scroller; OverScroller overScroller; boolean isPreGingerbread; public CompatScroller(Context context) { if (VERSION.SDK_INT < VERSION_CODES.GINGERBREAD) { isPreGingerbread = true; scroller = new Scroller(context); } else { isPreGingerbread = false; overScroller = new OverScroller(context); } } public void fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY) { if (isPreGingerbread) { scroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY); } else { overScroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY); } } public void forceFinished(boolean finished) { if (isPreGingerbread) { scroller.forceFinished(finished); } else { overScroller.forceFinished(finished); } } public boolean isFinished() { if (isPreGingerbread) { return scroller.isFinished(); } else { return overScroller.isFinished(); } } public boolean computeScrollOffset() { if (isPreGingerbread) { return scroller.computeScrollOffset(); } else { overScroller.computeScrollOffset(); return overScroller.computeScrollOffset(); } } public int getCurrX() { if (isPreGingerbread) { return scroller.getCurrX(); } else { return overScroller.getCurrX(); } } public int getCurrY() { if (isPreGingerbread) { return scroller.getCurrY(); } else { return overScroller.getCurrY(); } } } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void compatPostOnAnimation(Runnable runnable) { if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) { postOnAnimation(runnable); } else { postDelayed(runnable, 1000 / 60); } } private class ZoomVariables { public float scale; public float focusX; public float focusY; public ScaleType scaleType; public ZoomVariables(float scale, float focusX, float focusY, ScaleType scaleType) { this.scale = scale; this.focusX = focusX; this.focusY = focusY; this.scaleType = scaleType; } } private void printMatrixInfo() { float[] n = new float[9]; matrix.getValues(n); Log.d(DEBUG, "Scale: " + n[Matrix.MSCALE_X] + " TransX: " + n[Matrix.MTRANS_X] + " TransY: " + n[Matrix.MTRANS_Y]); } } ================================================ FILE: PhotoPicker/src/main/res/anim/dialog_enter.xml ================================================ ================================================ FILE: PhotoPicker/src/main/res/anim/dialog_exit.xml ================================================ ================================================ FILE: PhotoPicker/src/main/res/drawable/__picker_bg_material_item.xml ================================================ ================================================ FILE: PhotoPicker/src/main/res/drawable/__picker_camera.xml ================================================ ================================================ FILE: PhotoPicker/src/main/res/drawable/__picker_checkbox_bg.xml ================================================ ================================================ FILE: PhotoPicker/src/main/res/drawable/__picker_default_weixin.xml ================================================ ================================================ FILE: PhotoPicker/src/main/res/drawable/__picker_delete.xml ================================================ ================================================ FILE: PhotoPicker/src/main/res/drawable/__picker_photo_bg.xml ================================================ ================================================ FILE: PhotoPicker/src/main/res/drawable-v21/__picker_bg_material_item.xml ================================================ ================================================ FILE: PhotoPicker/src/main/res/layout/__picker_activity_photo_pager.xml ================================================ ================================================ FILE: PhotoPicker/src/main/res/layout/__picker_activity_photo_picker.xml ================================================ ================================================ FILE: PhotoPicker/src/main/res/layout/__picker_fragment_photo_picker.xml ================================================