├── LICENSE ├── PickPhotoDialog ├── app │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── ywl5320 │ │ │ └── csdn │ │ │ └── pickphoto │ │ │ └── ExampleInstrumentedTest.java │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── ywl5320 │ │ │ │ └── csdn │ │ │ │ └── pickphoto │ │ │ │ ├── MainActivity.java │ │ │ │ ├── adapter │ │ │ │ ├── ImgGridViewAdapter.java │ │ │ │ └── ImgListViewAdapter.java │ │ │ │ ├── adviewpager │ │ │ │ ├── adapter │ │ │ │ │ └── ImageViewPagerAdapter.java │ │ │ │ └── adutils │ │ │ │ │ └── AdViewpagerUtil.java │ │ │ │ ├── beans │ │ │ │ ├── ImgBean.java │ │ │ │ └── ImgFloderBean.java │ │ │ │ ├── dialog │ │ │ │ ├── BaseDialog.java │ │ │ │ ├── ImgShowDialog.java │ │ │ │ ├── PhotoListDialog.java │ │ │ │ └── PickPhotoDialog.java │ │ │ │ └── util │ │ │ │ └── AppUtil.java │ │ └── res │ │ │ ├── anim │ │ │ ├── dialog_enter.xml │ │ │ └── dialog_exit.xml │ │ │ ├── drawable │ │ │ ├── blue_item.xml │ │ │ ├── blue_item_r.xml │ │ │ ├── btn_selector.xml │ │ │ ├── circle_drawable.xml │ │ │ ├── circle_drawable_pressed.xml │ │ │ ├── dot_selector.xml │ │ │ └── item_selected_folder.xml │ │ │ ├── layout │ │ │ ├── activity_main.xml │ │ │ ├── activity_photo_view_layout.xml │ │ │ ├── activity_selected_photo_layout.xml │ │ │ ├── dialog_choice_photo_layout.xml │ │ │ ├── dialog_photo_list_layout.xml │ │ │ ├── dialog_photo_show_layout.xml │ │ │ ├── include_comm_game_text_title.xml │ │ │ ├── item_img_adapter_layout.xml │ │ │ └── item_listview_layout.xml │ │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ ├── icon_img_bg.png │ │ │ ├── icon_img_folder_more.png │ │ │ ├── icon_selected.png │ │ │ └── icon_unselected.png │ │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ └── dimens.xml │ │ │ ├── values │ │ │ ├── colors.xml │ │ │ ├── dimens.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ │ └── xml │ │ │ └── provider_paths.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── ywl5320 │ │ └── csdn │ │ └── pickphoto │ │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── photoviewlibrary │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── uk │ │ │ └── co │ │ │ └── senab │ │ │ └── photoview │ │ │ └── ExampleInstrumentedTest.java │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── uk │ │ │ │ └── co │ │ │ │ └── senab │ │ │ │ └── photoview │ │ │ │ ├── Compat.java │ │ │ │ ├── DefaultOnDoubleTapListener.java │ │ │ │ ├── IPhotoView.java │ │ │ │ ├── PhotoView.java │ │ │ │ ├── PhotoViewAttacher.java │ │ │ │ ├── gestures │ │ │ │ ├── CupcakeGestureDetector.java │ │ │ │ ├── EclairGestureDetector.java │ │ │ │ ├── FroyoGestureDetector.java │ │ │ │ ├── GestureDetector.java │ │ │ │ ├── OnGestureListener.java │ │ │ │ └── VersionedGestureDetector.java │ │ │ │ ├── log │ │ │ │ ├── LogManager.java │ │ │ │ ├── Logger.java │ │ │ │ └── LoggerDefault.java │ │ │ │ └── scrollerproxy │ │ │ │ ├── GingerScroller.java │ │ │ │ ├── IcsScroller.java │ │ │ │ ├── PreGingerScroller.java │ │ │ │ └── ScrollerProxy.java │ │ └── res │ │ │ └── values │ │ │ └── strings.xml │ │ └── test │ │ └── java │ │ └── uk │ │ └── co │ │ └── senab │ │ └── photoview │ │ └── ExampleUnitTest.java └── settings.gradle ├── README.md └── imgs ├── 1.png ├── 2.png ├── 3.png ├── 4.png └── pickphotodialog.gif /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.ywl5320.csdn.pickphoto" 8 | minSdkVersion 14 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.2.0' 28 | testCompile 'junit:junit:4.12' 29 | compile 'jp.wasabeef:glide-transformations:2.0.1' 30 | compile project(':photoviewlibrary') 31 | } 32 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Android\android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/androidTest/java/com/ywl5320/csdn/pickphoto/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.ywl5320.csdn.pickphoto; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.ywl5320.csdn.pickphoto", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/java/com/ywl5320/csdn/pickphoto/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.ywl5320.csdn.pickphoto; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.DialogInterface; 5 | import android.content.Intent; 6 | import android.graphics.Bitmap; 7 | import android.os.Bundle; 8 | import android.support.annotation.Nullable; 9 | import android.support.v4.view.ViewPager; 10 | import android.support.v7.app.AppCompatActivity; 11 | import android.view.Gravity; 12 | import android.view.View; 13 | import android.view.Window; 14 | import android.widget.Button; 15 | import android.widget.LinearLayout; 16 | import android.widget.Toast; 17 | 18 | import com.ywl5320.csdn.pickphoto.adviewpager.adutils.AdViewpagerUtil; 19 | import com.ywl5320.csdn.pickphoto.beans.ImgBean; 20 | import com.ywl5320.csdn.pickphoto.dialog.PickPhotoDialog; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | /** 26 | * Created by ywl on 2016/11/22. 27 | */ 28 | 29 | public class MainActivity extends AppCompatActivity { 30 | 31 | private Button btnChoicePhoto; 32 | private ViewPager viewpager; 33 | private LinearLayout lydots; 34 | private AdViewpagerUtil adViewpagerUtil; 35 | 36 | private PickPhotoDialog pickPhotoDialog; 37 | 38 | private List imgBeens = new ArrayList(); 39 | 40 | @Override 41 | protected void onCreate(@Nullable final Bundle savedInstanceState) { 42 | super.onCreate(savedInstanceState); 43 | setContentView(R.layout.activity_main); 44 | btnChoicePhoto = (Button) findViewById(R.id.btn_choice_photo); 45 | viewpager = (ViewPager) findViewById(R.id.viewpager); 46 | lydots = (LinearLayout) findViewById(R.id.ly_dots); 47 | 48 | btnChoicePhoto.setOnClickListener(new View.OnClickListener() { 49 | @Override 50 | public void onClick(View view) { 51 | if(adViewpagerUtil != null) { 52 | adViewpagerUtil.stopLoopViewPager(); 53 | } 54 | pickPhotoDialog = new PickPhotoDialog(MainActivity.this, MainActivity.this); 55 | Window window = pickPhotoDialog.getWindow(); 56 | window.setGravity(Gravity.BOTTOM); 57 | window.setWindowAnimations(R.style.DialogEnter); 58 | pickPhotoDialog.setCutImg(false, 5);//false:不裁剪(数量5生效) true:裁剪,数量不生效 59 | pickPhotoDialog.setSelectedImgs(imgBeens);//已选择的图片,选择时标出 60 | pickPhotoDialog.setOnPhotoResultListener(new PickPhotoDialog.OnPhotoResultListener() { 61 | @Override 62 | public void onCameraResult(String path) {//相机拍照图片路径 63 | imgBeens.clear(); 64 | ImgBean imgBean = new ImgBean(); 65 | imgBean.setPath(path); 66 | imgBeens.add(imgBean); 67 | adViewpagerUtil = new AdViewpagerUtil(MainActivity.this, viewpager, lydots, 8, 4, imgBeens); 68 | adViewpagerUtil.initVps(); 69 | } 70 | 71 | @Override 72 | public void onCutPhotoResult(Bitmap bitmap) { 73 | //图片(相机和相册)裁剪后返回的bitmap 74 | } 75 | 76 | @Override 77 | public void onPhotoResult(List selectedImgs) {//相册多图选择返回图片路径结果集 78 | if(selectedImgs != null && selectedImgs.size() > 0) { 79 | imgBeens.clear(); 80 | imgBeens.addAll(selectedImgs); 81 | adViewpagerUtil = new AdViewpagerUtil(MainActivity.this, viewpager, lydots, 8, 4, imgBeens); 82 | adViewpagerUtil.initVps(); 83 | } 84 | else 85 | { 86 | if(adViewpagerUtil != null) { 87 | adViewpagerUtil.startLoopViewPager(); 88 | } 89 | } 90 | } 91 | }); 92 | pickPhotoDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { 93 | @Override 94 | public void onDismiss(DialogInterface dialogInterface) { 95 | if(adViewpagerUtil != null) { 96 | adViewpagerUtil.startLoopViewPager(); 97 | } 98 | } 99 | }); 100 | 101 | pickPhotoDialog.show(); 102 | } 103 | }); 104 | } 105 | 106 | @TargetApi(23) 107 | @Override 108 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 109 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 110 | if (pickPhotoDialog != null) 111 | { 112 | pickPhotoDialog.onRequestPermissionsResult(requestCode, permissions, grantResults); 113 | } 114 | } 115 | 116 | 117 | @Override 118 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 119 | // TODO Auto-generated method stub 120 | super.onActivityResult(requestCode, resultCode, data); 121 | 122 | if(pickPhotoDialog != null) 123 | { 124 | pickPhotoDialog.onActivityResult(requestCode, resultCode, data); 125 | } 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/java/com/ywl5320/csdn/pickphoto/adapter/ImgGridViewAdapter.java: -------------------------------------------------------------------------------- 1 | 2 | package com.ywl5320.csdn.pickphoto.adapter; 3 | 4 | 5 | import android.content.Context; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.BaseAdapter; 10 | import android.widget.ImageView; 11 | 12 | import com.bumptech.glide.Glide; 13 | import com.ywl5320.csdn.pickphoto.R; 14 | import com.ywl5320.csdn.pickphoto.beans.ImgBean; 15 | 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | 19 | 20 | /** 21 | * @author ywl 22 | * 23 | */ 24 | public class ImgGridViewAdapter extends BaseAdapter { 25 | protected Context context; 26 | protected LayoutInflater mlayoutInflate; 27 | protected List mDatas = new ArrayList(); 28 | private boolean isaddpath = false; 29 | private String ppath = ""; 30 | private OnImgSelectedListener onImgSelectedListener; 31 | 32 | public ImgGridViewAdapter(Context context, List mDatas) { 33 | this.context = context; 34 | this.mDatas = mDatas; 35 | if(this.context!=null) 36 | mlayoutInflate = LayoutInflater.from(this.context); 37 | } 38 | 39 | public void setOnImgSelectedListener(OnImgSelectedListener onImgSelectedListener) { 40 | this.onImgSelectedListener = onImgSelectedListener; 41 | } 42 | 43 | public void setPpath(String ppath) { 44 | this.ppath = ppath; 45 | } 46 | 47 | public void setIsaddpath(boolean isaddpath) { 48 | this.isaddpath = isaddpath; 49 | } 50 | 51 | public boolean isaddpath() { 52 | return isaddpath; 53 | } 54 | 55 | public String getPpath() { 56 | return ppath; 57 | } 58 | 59 | @Override 60 | public int getCount() { 61 | return mDatas.size(); 62 | } 63 | 64 | @Override 65 | public Object getItem(int position) { 66 | return mDatas.get(position); 67 | } 68 | 69 | @Override 70 | public long getItemId(int position) { 71 | return position; 72 | } 73 | 74 | @Override 75 | public View getView(int position, View convertView, ViewGroup parent) { 76 | ViewHolder viewHolder; 77 | final ImgBean imgBean = mDatas.get(position); 78 | if(convertView == null) 79 | { 80 | convertView = mlayoutInflate.inflate(R.layout.item_img_adapter_layout, parent, false); 81 | viewHolder = new ViewHolder(); 82 | viewHolder.ivImg = (ImageView) convertView.findViewById(R.id.iv_img); 83 | viewHolder.ivSelected = (ImageView) convertView.findViewById(R.id.iv_selected); 84 | viewHolder.vGrayBg = convertView.findViewById(R.id.v_gray_bg); 85 | convertView.setTag(viewHolder); 86 | } 87 | else 88 | { 89 | viewHolder = (ViewHolder) convertView.getTag(); 90 | } 91 | 92 | if(!isaddpath) { 93 | Glide.with(context).load(imgBean.getPath()).into(viewHolder.ivImg); 94 | } 95 | else 96 | { 97 | Glide.with(context).load(ppath + "/" + imgBean.getPath()).into(viewHolder.ivImg); 98 | } 99 | 100 | if(imgBean.isSelected()) 101 | { 102 | viewHolder.ivSelected.setSelected(true); 103 | viewHolder.vGrayBg.setVisibility(View.VISIBLE); 104 | } 105 | else 106 | { 107 | viewHolder.ivSelected.setSelected(false); 108 | viewHolder.vGrayBg.setVisibility(View.GONE); 109 | } 110 | 111 | viewHolder.ivSelected.setOnClickListener(new View.OnClickListener() { 112 | @Override 113 | public void onClick(View view) { 114 | if(onImgSelectedListener != null) 115 | { 116 | onImgSelectedListener.onSelected(imgBean); 117 | } 118 | } 119 | }); 120 | 121 | return convertView; 122 | } 123 | 124 | public class ViewHolder 125 | { 126 | public ImageView ivImg; 127 | public ImageView ivSelected; 128 | public View vGrayBg; 129 | 130 | public ViewHolder(){} 131 | } 132 | 133 | public interface OnImgSelectedListener 134 | { 135 | void onSelected(ImgBean imgBean); 136 | } 137 | 138 | } 139 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/java/com/ywl5320/csdn/pickphoto/adapter/ImgListViewAdapter.java: -------------------------------------------------------------------------------- 1 | 2 | package com.ywl5320.csdn.pickphoto.adapter; 3 | 4 | 5 | import android.content.Context; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.BaseAdapter; 10 | import android.widget.ImageView; 11 | import android.widget.TextView; 12 | 13 | import com.bumptech.glide.Glide; 14 | import com.ywl5320.csdn.pickphoto.R; 15 | import com.ywl5320.csdn.pickphoto.beans.ImgFloderBean; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | 20 | 21 | /** 22 | * @author ywl 23 | * 24 | */ 25 | public class ImgListViewAdapter extends BaseAdapter { 26 | protected Context context; 27 | protected LayoutInflater mlayoutInflate; 28 | protected List mDatas = new ArrayList(); 29 | 30 | public ImgListViewAdapter(Context context, List mDatas) { 31 | this.context = context; 32 | this.mDatas = mDatas; 33 | if(this.context!=null) 34 | mlayoutInflate = LayoutInflater.from(this.context); 35 | } 36 | 37 | 38 | @Override 39 | public int getCount() { 40 | return mDatas.size(); 41 | } 42 | 43 | @Override 44 | public Object getItem(int position) { 45 | return mDatas.get(position); 46 | } 47 | 48 | @Override 49 | public long getItemId(int position) { 50 | return position; 51 | } 52 | 53 | @Override 54 | public View getView(int position, View convertView, ViewGroup parent) { 55 | ViewHolder viewHolder; 56 | ImgFloderBean imgFloderBean = mDatas.get(position); 57 | if(convertView == null) 58 | { 59 | convertView = mlayoutInflate.inflate(R.layout.item_listview_layout, parent, false); 60 | viewHolder = new ViewHolder(); 61 | viewHolder.ivImg = (ImageView) convertView.findViewById(R.id.iv_img); 62 | viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name); 63 | viewHolder.tvNumber = (TextView) convertView.findViewById(R.id.tv_num); 64 | viewHolder.ivSelected = (ImageView) convertView.findViewById(R.id.iv_selected); 65 | convertView.setTag(viewHolder); 66 | } 67 | else 68 | { 69 | viewHolder = (ViewHolder) convertView.getTag(); 70 | } 71 | 72 | viewHolder.tvName.setText(imgFloderBean.getDirName()); 73 | if(imgFloderBean.getType() == 0) 74 | { 75 | viewHolder.tvNumber.setVisibility(View.GONE); 76 | } 77 | else 78 | { 79 | viewHolder.tvNumber.setVisibility(View.VISIBLE); 80 | viewHolder.tvNumber.setText(imgFloderBean.getSize() + "张"); 81 | } 82 | 83 | if(imgFloderBean.isSelected()) { 84 | viewHolder.ivSelected.setSelected(true); 85 | } 86 | else 87 | { 88 | viewHolder.ivSelected.setSelected(false); 89 | } 90 | Glide.with(context).load(imgFloderBean.getFirstImgPath()).into(viewHolder.ivImg); 91 | 92 | return convertView; 93 | } 94 | 95 | public class ViewHolder 96 | { 97 | public ImageView ivImg; 98 | public TextView tvName; 99 | public TextView tvNumber; 100 | public ImageView ivSelected; 101 | public ViewHolder(){} 102 | } 103 | 104 | } 105 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/java/com/ywl5320/csdn/pickphoto/adviewpager/adapter/ImageViewPagerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.ywl5320.csdn.pickphoto.adviewpager.adapter; 2 | 3 | import android.content.Context; 4 | import android.support.v4.view.PagerAdapter; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.ImageView; 8 | 9 | public class ImageViewPagerAdapter extends PagerAdapter { 10 | 11 | private ImageView[] imageViews; 12 | private int size; 13 | private Context context; 14 | 15 | public ImageViewPagerAdapter(Context context, ImageView[] imageViews) { 16 | this.context = context; 17 | this.imageViews = imageViews; 18 | size = imageViews.length; 19 | } 20 | 21 | @Override 22 | public int getCount() { 23 | return imageViews.length; 24 | } 25 | 26 | @Override 27 | public boolean isViewFromObject(View arg0, Object arg1) { 28 | return arg0 == arg1; 29 | } 30 | 31 | @Override 32 | public void destroyItem(ViewGroup container, int position, Object object) { 33 | // ((ViewPager) container).removeView((View) object);// 完全溢出view,避免数据多时出现重复现象 34 | container.removeView(imageViews[position]);//删除页卡 35 | } 36 | 37 | @Override 38 | public Object instantiateItem(ViewGroup container, int position) { 39 | container.addView(imageViews[position], 0); 40 | return imageViews[position]; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/java/com/ywl5320/csdn/pickphoto/adviewpager/adutils/AdViewpagerUtil.java: -------------------------------------------------------------------------------- 1 | package com.ywl5320.csdn.pickphoto.adviewpager.adutils; 2 | 3 | import android.content.Context; 4 | import android.os.Handler; 5 | import android.support.v4.view.ViewPager; 6 | import android.view.MotionEvent; 7 | import android.view.View; 8 | import android.widget.ImageView; 9 | import android.widget.LinearLayout; 10 | 11 | import com.bumptech.glide.Glide; 12 | import com.ywl5320.csdn.pickphoto.R; 13 | import com.ywl5320.csdn.pickphoto.adviewpager.adapter.ImageViewPagerAdapter; 14 | import com.ywl5320.csdn.pickphoto.beans.ImgBean; 15 | 16 | import java.util.List; 17 | 18 | /** 19 | * Created by ywl on 2016/6/24. 20 | */ 21 | public class AdViewpagerUtil { 22 | 23 | private Context context; 24 | private ViewPager viewPager; 25 | private ImageViewPagerAdapter mimageViewPagerAdapter; 26 | private ImageView[] mImageViews; 27 | private List urls; 28 | private LinearLayout dotlayout; 29 | private ImageView[] dotViews; 30 | 31 | private boolean isRight = true; // 判断viewpager是不是向右滑动 32 | private int lastPosition = 1; // 记录viewpager从哪个页面滑动到当前页面,从而区分滑动方向 33 | private int autoIndex = 1; // 自动轮询时自增坐标,能确定导航圆点的位置 34 | private int currentIndex = 0; //当前item 35 | private int delayTime = 5000; //自动轮播的时间间隔 36 | private int imgsize = 0; //图片的数量,item的数量 37 | private boolean isLoop = false;//轮播开关 38 | 39 | private OnAdPageChangeListener onAdPageChangeListener; //pagechange回调 40 | private OnAdItemClickListener onAdItemClickListener; //点击事件回调 41 | 42 | private int dotsize = 8; //小圆点的大小宽度 43 | private int dotoffset = 4; //小圆点的间距 44 | 45 | /** 46 | * 不带小圆点 47 | * @param context 48 | * @param viewPager 49 | * @param urls 50 | */ 51 | public AdViewpagerUtil(Context context, ViewPager viewPager, List urls) { 52 | this.context = context; 53 | this.viewPager = viewPager; 54 | this.urls = urls; 55 | } 56 | 57 | /** 58 | * 有小圆点 59 | * @param context 60 | * @param viewPager 61 | * @param dotlayout 62 | * @param dotsize 63 | * @param dotoffset 64 | * @param urls 65 | */ 66 | public AdViewpagerUtil(Context context, ViewPager viewPager, LinearLayout dotlayout, int dotsize, int dotoffset, List urls) { 67 | this.context = context; 68 | this.viewPager = viewPager; 69 | this.dotlayout = dotlayout; 70 | this.dotsize = dotsize; 71 | this.urls = urls; 72 | } 73 | 74 | public void setOnAdPageChangeListener(OnAdPageChangeListener onAdPageChangeListener) { 75 | this.onAdPageChangeListener = onAdPageChangeListener; 76 | } 77 | 78 | public void setOnAdItemClickListener(OnAdItemClickListener onAdItemClickListener) { 79 | this.onAdItemClickListener = onAdItemClickListener; 80 | } 81 | 82 | private void initAdimgs(List urls) 83 | { 84 | LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 85 | int length = urls.size() + 2; 86 | mImageViews = new ImageView[length]; 87 | for (int i = 0; i urls) 97 | { 98 | if (urls.size() > 0) { 99 | imgsize = length; 100 | for (int i = 0; i < length; i++) { 101 | if (i < length - 2) { 102 | final int index = i; 103 | Glide.with(context) .load(urls.get(i).getPath()).into(mImageViews[i + 1]); 104 | mImageViews[i + 1].setOnClickListener(new View.OnClickListener() { 105 | 106 | @Override 107 | public void onClick(View v) { 108 | // TODO Auto-generated method stub 109 | if(onAdItemClickListener != null) 110 | { 111 | onAdItemClickListener.onItemClick(mImageViews[index + 1], index); 112 | } 113 | } 114 | }); 115 | } 116 | } 117 | Glide.with(context) .load(urls.get(urls.size() - 1).getPath()).into(mImageViews[0]); 118 | Glide.with(context) .load(urls.get(0).getPath()).into(mImageViews[length - 1]); 119 | } 120 | } 121 | 122 | public void initVps() 123 | { 124 | initAdimgs(urls); 125 | initDots(urls.size()); 126 | mimageViewPagerAdapter = new ImageViewPagerAdapter(context, mImageViews); 127 | viewPager.setAdapter(mimageViewPagerAdapter); 128 | viewPager.setOffscreenPageLimit(mImageViews.length); 129 | startLoopViewPager(); 130 | viewPager.setOnTouchListener(new View.OnTouchListener() { 131 | 132 | @Override 133 | public boolean onTouch(View v, MotionEvent event) { 134 | // TODO Auto-generated method stub 135 | int action = event.getAction(); 136 | switch (action) { 137 | case MotionEvent.ACTION_DOWN: 138 | case MotionEvent.ACTION_MOVE: 139 | isLoop = true; 140 | stopLoopViewPager(); 141 | break; 142 | case MotionEvent.ACTION_UP: 143 | case MotionEvent.ACTION_CANCEL: 144 | isLoop = false; 145 | startLoopViewPager(); 146 | default: 147 | break; 148 | } 149 | return false; 150 | } 151 | }); 152 | viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 153 | 154 | @Override 155 | public void onPageScrollStateChanged(int arg0) { 156 | // TODO Auto-generated method stub 157 | if (isRight) { 158 | if (arg0 != 1) { 159 | if (lastPosition == 0) { 160 | viewPager.setCurrentItem(imgsize - 2, false); 161 | } else if (lastPosition == imgsize - 1) { 162 | viewPager.setCurrentItem(1, false); 163 | } 164 | } 165 | } 166 | 167 | if(onAdPageChangeListener != null) 168 | { 169 | onAdPageChangeListener.onPageScrollStateChanged(arg0); 170 | } 171 | } 172 | 173 | @Override 174 | public void onPageScrolled(int arg0, float arg1, int arg2) { 175 | // TODO Auto-generated method stub 176 | if (!isRight) { 177 | if (arg1 < 0.01) { 178 | if (arg0 == 0) { 179 | viewPager.setCurrentItem(imgsize - 2, false); 180 | } else if (arg0 == imgsize - 1) { 181 | viewPager.setCurrentItem(1, false); 182 | } 183 | } 184 | } 185 | if(onAdPageChangeListener != null) 186 | { 187 | onAdPageChangeListener.onPageScrolled(arg0, arg1, arg2); 188 | } 189 | } 190 | 191 | @Override 192 | public void onPageSelected(int arg0) { 193 | // TODO Auto-generated method stub 194 | autoIndex = arg0; 195 | if (lastPosition < arg0 && lastPosition != 0) { 196 | isRight = true; 197 | } else if (lastPosition == imgsize - 1) { 198 | isRight = true; 199 | } 200 | if (lastPosition > arg0 && lastPosition != imgsize - 1) { 201 | isRight = false; 202 | } else if (lastPosition == 0) { 203 | isRight = false; 204 | } 205 | lastPosition = arg0; 206 | 207 | if (arg0 == 0) { 208 | currentIndex = imgsize - 2; 209 | } else if (arg0 == imgsize - 1) { 210 | currentIndex = 1; 211 | } else { 212 | currentIndex = arg0; 213 | } 214 | 215 | for (int i = 0; i < dotViews.length; i++) { 216 | if (i == currentIndex - 1) { 217 | dotViews[i].setSelected(true); 218 | } else { 219 | dotViews[i].setSelected(false); 220 | } 221 | } 222 | 223 | if(onAdPageChangeListener != null) 224 | { 225 | onAdPageChangeListener.onPageSelected(arg0); 226 | } 227 | } 228 | 229 | }); 230 | viewPager.setCurrentItem(1);// 初始化时设置显示第一页(ViewPager中索引为1) 231 | } 232 | 233 | public void initDots(int length) 234 | { 235 | if(dotlayout == null) 236 | return; 237 | dotlayout.removeAllViews(); 238 | LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(dip2px(context, dotsize), dip2px(context, dotsize)); 239 | mParams.setMargins(dip2px(context, dotoffset), 0, dip2px(context, dotoffset), 0);//设置小圆点左右之间的间隔 240 | 241 | dotViews = new ImageView[length]; 242 | 243 | for(int i = 0; i < length; i++) 244 | { 245 | ImageView imageView = new ImageView(context); 246 | imageView.setLayoutParams(mParams); 247 | imageView.setImageResource(R.drawable.dot_selector); 248 | if(i== 0) 249 | { 250 | imageView.setSelected(true);//默认启动时,选中第一个小圆点 251 | } 252 | else { 253 | imageView.setSelected(false); 254 | } 255 | dotViews[i] = imageView;//得到每个小圆点的引用,用于滑动页面时,(onPageSelected方法中)更改它们的状态。 256 | dotlayout.addView(imageView);//添加到布局里面显示 257 | } 258 | } 259 | 260 | Handler handler = new Handler(); 261 | Runnable runnable = new Runnable() { 262 | @Override 263 | public void run() { 264 | // 要做的事情 265 | if (viewPager.getChildCount() > 0) { 266 | handler.postDelayed(this, delayTime); 267 | autoIndex++; 268 | viewPager.setCurrentItem(autoIndex % imgsize, true); 269 | } 270 | } 271 | }; 272 | 273 | public void startLoopViewPager() { 274 | if (!isLoop && viewPager != null) { 275 | handler.postDelayed(runnable, delayTime);// 每两秒执行一次runnable. 276 | isLoop = true; 277 | } 278 | 279 | } 280 | 281 | public void stopLoopViewPager() { 282 | if (isLoop && viewPager != null) { 283 | handler.removeCallbacks(runnable); 284 | isLoop = false; 285 | } 286 | } 287 | 288 | public interface OnAdItemClickListener 289 | { 290 | void onItemClick(View v, int flag); 291 | } 292 | 293 | public interface OnAdPageChangeListener 294 | { 295 | void onPageScrollStateChanged(int arg0); 296 | void onPageScrolled(int arg0, float arg1, int arg2); 297 | void onPageSelected(int arg0); 298 | } 299 | 300 | public int dip2px(Context context, float dipValue) { 301 | final float scale = context.getResources().getDisplayMetrics().density; 302 | return (int) (dipValue * scale + 0.5f); 303 | } 304 | 305 | } 306 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/java/com/ywl5320/csdn/pickphoto/beans/ImgBean.java: -------------------------------------------------------------------------------- 1 | package com.ywl5320.csdn.pickphoto.beans; 2 | 3 | /** 4 | * Created by ywl on 2016/11/22. 5 | */ 6 | 7 | public class ImgBean { 8 | 9 | private String path; 10 | private boolean isSelected; 11 | 12 | public boolean isSelected() { 13 | return isSelected; 14 | } 15 | 16 | public void setSelected(boolean selected) { 17 | isSelected = selected; 18 | } 19 | 20 | public String getPath() { 21 | return path; 22 | } 23 | 24 | public void setPath(String path) { 25 | this.path = path; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/java/com/ywl5320/csdn/pickphoto/beans/ImgFloderBean.java: -------------------------------------------------------------------------------- 1 | package com.ywl5320.csdn.pickphoto.beans; 2 | 3 | /** 4 | * Created by ywl on 2016/11/21. 5 | */ 6 | 7 | public class ImgFloderBean { 8 | 9 | private String dir; 10 | private String firstImgPath; 11 | private String dirName; 12 | private int size; 13 | private boolean isSelected; 14 | private int type;//0全部 非0文件夹 15 | 16 | public boolean isSelected() { 17 | return isSelected; 18 | } 19 | 20 | public void setSelected(boolean selected) { 21 | isSelected = selected; 22 | } 23 | 24 | public int getType() { 25 | return type; 26 | } 27 | 28 | public void setType(int type) { 29 | this.type = type; 30 | } 31 | 32 | public String getDirName() { 33 | return dirName; 34 | } 35 | 36 | public void setDirName(String dirName) { 37 | this.dirName = dirName; 38 | } 39 | 40 | public int getSize() { 41 | return size; 42 | } 43 | 44 | public void setSize(int size) { 45 | this.size = size; 46 | } 47 | 48 | public String getDir() { 49 | return dir; 50 | } 51 | 52 | public void setDir(String dir) { 53 | this.dir = dir; 54 | } 55 | 56 | public String getFirstImgPath() { 57 | return firstImgPath; 58 | } 59 | 60 | public void setFirstImgPath(String firstImgPath) { 61 | this.firstImgPath = firstImgPath; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/java/com/ywl5320/csdn/pickphoto/dialog/BaseDialog.java: -------------------------------------------------------------------------------- 1 | package com.ywl5320.csdn.pickphoto.dialog; 2 | 3 | import android.app.Dialog; 4 | import android.content.Context; 5 | import android.graphics.Color; 6 | import android.os.Build; 7 | import android.view.View; 8 | import android.view.Window; 9 | import android.view.WindowManager; 10 | 11 | import com.ywl5320.csdn.pickphoto.R; 12 | 13 | /** 14 | * Created by window10 on 2016/3/18. 15 | */ 16 | public class BaseDialog extends Dialog { 17 | 18 | public Context context; 19 | public BaseDialog(Context context) { 20 | super(context, R.style.StyleDialog); 21 | this.context = context; 22 | if(true) { 23 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明实现 24 | Window window = getWindow(); 25 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 26 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 27 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); 28 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 29 | window.setStatusBarColor(Color.TRANSPARENT); 30 | } 31 | //透明状态栏 32 | else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4全透明 33 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 34 | // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); 35 | 36 | } 37 | } 38 | 39 | } 40 | 41 | public BaseDialog(Context context, int style) { 42 | super(context, style); 43 | this.context = context; 44 | 45 | if(true) { 46 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明实现 47 | Window window = getWindow(); 48 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 49 | window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 50 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); 51 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 52 | window.setStatusBarColor(Color.TRANSPARENT); 53 | } 54 | //透明状态栏 55 | else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4全透明 56 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 57 | // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); 58 | 59 | } 60 | } 61 | 62 | } 63 | 64 | @Override 65 | public void setContentView(int layoutResID) { 66 | super.setContentView(layoutResID); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/java/com/ywl5320/csdn/pickphoto/dialog/ImgShowDialog.java: -------------------------------------------------------------------------------- 1 | package com.ywl5320.csdn.pickphoto.dialog; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | 7 | import com.bumptech.glide.Glide; 8 | import com.ywl5320.csdn.pickphoto.R; 9 | 10 | import uk.co.senab.photoview.PhotoView; 11 | import uk.co.senab.photoview.PhotoViewAttacher; 12 | 13 | /** 14 | * Created by ywl on 2016/11/23. 15 | */ 16 | 17 | public class ImgShowDialog extends BaseDialog{ 18 | 19 | private PhotoView photoView; 20 | private String imgpath; 21 | private PhotoViewAttacher mAttacher; 22 | 23 | public ImgShowDialog(Context context) { 24 | super(context); 25 | } 26 | 27 | public void setImgpath(String imgpath) { 28 | this.imgpath = imgpath; 29 | } 30 | 31 | @Override 32 | protected void onCreate(Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | setContentView(R.layout.dialog_photo_show_layout); 35 | photoView = (PhotoView) findViewById(R.id.photoview); 36 | Glide.with(context).load(imgpath).into(photoView); 37 | mAttacher = new PhotoViewAttacher(photoView); 38 | mAttacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() { 39 | 40 | @Override 41 | public void onPhotoTap(View view, float x, float y) { 42 | 43 | } 44 | 45 | @Override 46 | public void onOutsidePhotoTap() { 47 | 48 | } 49 | }); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/java/com/ywl5320/csdn/pickphoto/dialog/PhotoListDialog.java: -------------------------------------------------------------------------------- 1 | package com.ywl5320.csdn.pickphoto.dialog; 2 | 3 | import android.animation.ValueAnimator; 4 | import android.app.Activity; 5 | import android.app.ProgressDialog; 6 | import android.content.ContentResolver; 7 | import android.content.Context; 8 | import android.database.Cursor; 9 | import android.graphics.Rect; 10 | import android.net.Uri; 11 | import android.os.Build; 12 | import android.os.Bundle; 13 | import android.os.Environment; 14 | import android.os.Handler; 15 | import android.os.Message; 16 | import android.provider.MediaStore; 17 | import android.util.DisplayMetrics; 18 | import android.view.View; 19 | import android.view.ViewGroup; 20 | import android.widget.AdapterView; 21 | import android.widget.GridView; 22 | import android.widget.ImageView; 23 | import android.widget.LinearLayout; 24 | import android.widget.ListView; 25 | import android.widget.RelativeLayout; 26 | import android.widget.TextView; 27 | import android.widget.Toast; 28 | 29 | 30 | import com.ywl5320.csdn.pickphoto.R; 31 | import com.ywl5320.csdn.pickphoto.adapter.ImgGridViewAdapter; 32 | import com.ywl5320.csdn.pickphoto.adapter.ImgListViewAdapter; 33 | import com.ywl5320.csdn.pickphoto.beans.ImgBean; 34 | import com.ywl5320.csdn.pickphoto.beans.ImgFloderBean; 35 | 36 | import java.io.File; 37 | import java.io.FilenameFilter; 38 | import java.lang.reflect.Field; 39 | import java.util.ArrayList; 40 | import java.util.Collections; 41 | import java.util.HashSet; 42 | import java.util.List; 43 | import java.util.Set; 44 | 45 | /** 46 | * Created by ywl on 2016/11/23. 47 | */ 48 | 49 | public class PhotoListDialog extends BaseDialog{ 50 | 51 | private ProgressDialog progressDialog; 52 | 53 | private LinearLayout lySystemBar; 54 | private ImageView mivBack; 55 | private TextView mtvMenu; 56 | private TextView mtvTitle; 57 | private TextView tvSelected; 58 | private RelativeLayout rlActions; 59 | private RelativeLayout rlSelected; 60 | private View vBg; 61 | private Activity activity; 62 | 63 | private GridView gridView; 64 | private ListView listView; 65 | 66 | private List imgFloderBeens; 67 | private List imgs; 68 | private List tempImgs; 69 | private List selectedImgBeans; 70 | private List alreadySelectedImgs; 71 | 72 | private ImgGridViewAdapter imgGridViewAdapter; 73 | private ImgListViewAdapter imgListViewAdapter; 74 | 75 | private int listviewHeight = 0; 76 | private boolean isShowListView = false; 77 | private int MAX_COUNT = 9; 78 | private String dirPath = ""; 79 | 80 | private OnChoicePhotoListener onChoicePhotoListener; 81 | 82 | 83 | public PhotoListDialog(Context context, Activity activity) { 84 | super(context); 85 | this.activity = activity; 86 | } 87 | 88 | public void setAlreadySelectedImgs(List alreadySelectedImgs) { 89 | this.alreadySelectedImgs = alreadySelectedImgs; 90 | } 91 | 92 | public void setOnChoicePhotoListener(OnChoicePhotoListener onChoicePhotoListener) { 93 | this.onChoicePhotoListener = onChoicePhotoListener; 94 | } 95 | 96 | 97 | public void setMAX_COUNT(int MAX_COUNT) { 98 | this.MAX_COUNT = MAX_COUNT; 99 | } 100 | 101 | 102 | 103 | @Override 104 | protected void onCreate(Bundle savedInstanceState) { 105 | super.onCreate(savedInstanceState); 106 | setContentView(R.layout.dialog_photo_list_layout); 107 | 108 | mtvTitle = (TextView) findViewById(R.id.tv_title); 109 | mivBack = (ImageView) findViewById(R.id.iv_back); 110 | mtvMenu = (TextView) findViewById(R.id.tv_right); 111 | lySystemBar = (LinearLayout) findViewById(R.id.ly_system_bar); 112 | initSystembar(lySystemBar); 113 | 114 | 115 | mtvTitle.setText("图片选择"); 116 | mivBack.setVisibility(View.VISIBLE); 117 | mivBack.setOnClickListener(new View.OnClickListener() { 118 | @Override 119 | public void onClick(View v) { 120 | dismiss(); 121 | } 122 | }); 123 | 124 | gridView = (GridView) findViewById(R.id.gridview); 125 | tvSelected = (TextView) findViewById(R.id.tv_selected); 126 | listView = (ListView) findViewById(R.id.listview); 127 | rlActions = (RelativeLayout) findViewById(R.id.rl_actions); 128 | rlSelected = (RelativeLayout) findViewById(R.id.rl_selected); 129 | vBg = findViewById(R.id.v_bg); 130 | 131 | listviewHeight = (int)(getScreenHeight(activity) * 0.70f); 132 | RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, listviewHeight); 133 | lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 134 | lp.setMargins(0, 0, 0, dip2px(context, 50)); 135 | listView.setLayoutParams(lp); 136 | 137 | rlSelected.setOnClickListener(new View.OnClickListener() { 138 | @Override 139 | public void onClick(View view) { 140 | if(isShowListView) { 141 | hideFloor(listView); 142 | } 143 | else { 144 | showFloor(listView); 145 | } 146 | } 147 | }); 148 | 149 | mtvMenu.setOnClickListener(new View.OnClickListener() { 150 | @Override 151 | public void onClick(View view) { 152 | if(imgGridViewAdapter.isaddpath()) 153 | { 154 | for(ImgBean imgBean : selectedImgBeans) 155 | { 156 | imgBean.setPath(dirPath + "/" + imgBean.getPath()); 157 | } 158 | } 159 | 160 | if(onChoicePhotoListener != null) 161 | { 162 | onChoicePhotoListener.onResult(selectedImgBeans); 163 | } 164 | 165 | dismiss(); 166 | } 167 | }); 168 | 169 | hideFloor(listView); 170 | 171 | setAdapter(); 172 | progressDialog = ProgressDialog.show(context, null, "正在加载..."); 173 | new Handler().postDelayed(new Runnable() { 174 | @Override 175 | public void run() { 176 | scanImgs(); 177 | } 178 | }, 500); 179 | 180 | } 181 | 182 | public void setAdapter() 183 | { 184 | imgs = new ArrayList<>(); 185 | tempImgs = new ArrayList<>(); 186 | selectedImgBeans = new ArrayList<>(); 187 | imgFloderBeens = new ArrayList<>(); 188 | imgGridViewAdapter = new ImgGridViewAdapter(context, imgs); 189 | imgListViewAdapter = new ImgListViewAdapter(context, imgFloderBeens); 190 | gridView.setAdapter(imgGridViewAdapter); 191 | listView.setAdapter(imgListViewAdapter); 192 | mtvMenu.setText("(" + selectedImgBeans.size() + "/" + MAX_COUNT + ")"); 193 | 194 | gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 195 | @Override 196 | public void onItemClick(AdapterView adapterView, View view, int i, long l) { 197 | ImgBean imgBean = (ImgBean) gridView.getItemAtPosition(i); 198 | String imgPath = ""; 199 | if(imgGridViewAdapter.isaddpath()) 200 | { 201 | imgPath = imgGridViewAdapter.getPpath() + "/" + imgBean.getPath(); 202 | } 203 | else { 204 | imgPath = imgBean.getPath(); 205 | } 206 | ImgShowDialog imgShowDialog = new ImgShowDialog(context); 207 | imgShowDialog.setImgpath(imgPath); 208 | imgShowDialog.show(); 209 | 210 | } 211 | }); 212 | 213 | imgGridViewAdapter.setOnImgSelectedListener(new ImgGridViewAdapter.OnImgSelectedListener() { 214 | @Override 215 | public void onSelected(ImgBean imgBean) { 216 | if(selectedImgBeans.size() < MAX_COUNT) { 217 | imgBean.setSelected(!imgBean.isSelected()); 218 | if (imgBean.isSelected()) { 219 | if (MAX_COUNT == 1) { 220 | for (ImgBean imb : selectedImgBeans) { 221 | imb.setSelected(false); 222 | } 223 | selectedImgBeans.clear(); 224 | selectedImgBeans.add(imgBean); 225 | } else { 226 | if (!selectedImgBeans.contains(imgBean)) { 227 | selectedImgBeans.add(imgBean); 228 | } 229 | } 230 | } else { 231 | if (selectedImgBeans.contains(imgBean)) { 232 | selectedImgBeans.remove(imgBean); 233 | } 234 | } 235 | System.out.println("size:" + selectedImgBeans.size()); 236 | mtvMenu.setText("(" + selectedImgBeans.size() + "/" + MAX_COUNT + ")"); 237 | imgGridViewAdapter.notifyDataSetChanged(); 238 | } 239 | else 240 | { 241 | if(imgBean.isSelected()) 242 | { 243 | imgBean.setSelected(false); 244 | if (selectedImgBeans.contains(imgBean)) { 245 | selectedImgBeans.remove(imgBean); 246 | } 247 | } 248 | mtvMenu.setText("(" + selectedImgBeans.size() + "/" + MAX_COUNT + ")"); 249 | imgGridViewAdapter.notifyDataSetChanged(); 250 | } 251 | } 252 | }); 253 | 254 | 255 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 256 | @Override 257 | public void onItemClick(AdapterView adapterView, View view, int i, long l) { 258 | ImgFloderBean imgFloderBean = (ImgFloderBean) listView.getItemAtPosition(i); 259 | if(!imgFloderBean.isSelected()) { 260 | if (imgFloderBean.getType() == 0) { 261 | imgs.clear(); 262 | imgs.addAll(tempImgs); 263 | imgGridViewAdapter.setIsaddpath(false); 264 | imgGridViewAdapter.notifyDataSetInvalidated(); 265 | } else { 266 | scanChildDirPaths(imgFloderBean.getDir()); 267 | } 268 | setSelectedFolder(i); 269 | tvSelected.setText(imgFloderBean.getDirName()); 270 | for(ImgBean imb : selectedImgBeans) 271 | { 272 | imb.setSelected(false); 273 | } 274 | selectedImgBeans.clear(); 275 | mtvMenu.setText("(" + selectedImgBeans.size() + "/" + MAX_COUNT + ")"); 276 | } 277 | hideFloor(listView); 278 | } 279 | }); 280 | } 281 | 282 | 283 | public void setSelectedFolder(int position) 284 | { 285 | int size = imgFloderBeens.size(); 286 | for(int i = 0; i < size; i++) 287 | { 288 | if(i == position) 289 | { 290 | imgFloderBeens.get(i).setSelected(true); 291 | } 292 | else 293 | { 294 | imgFloderBeens.get(i).setSelected(false); 295 | } 296 | } 297 | imgListViewAdapter.notifyDataSetChanged(); 298 | } 299 | 300 | public void scanChildDirPaths(String dirPath) 301 | { 302 | this.dirPath = dirPath; 303 | File file = new File(dirPath); 304 | String[]childFilePaths = file.list(new FilenameFilter() { 305 | @Override 306 | public boolean accept(File file, String s) { 307 | if(s.endsWith(".jpg") || s.endsWith(".jpeg") || s.endsWith(".png")) 308 | return true; 309 | return false; 310 | } 311 | }); 312 | 313 | if(childFilePaths != null) 314 | { 315 | imgGridViewAdapter.setIsaddpath(true); 316 | imgGridViewAdapter.setPpath(dirPath); 317 | imgs.clear(); 318 | int length = childFilePaths.length; 319 | for(int i = 0; i < length; i++) 320 | { 321 | ImgBean imgBean = new ImgBean(); 322 | imgBean.setPath(childFilePaths[i]); 323 | imgs.add(imgBean); 324 | } 325 | Collections.reverse(imgs); 326 | imgGridViewAdapter.notifyDataSetInvalidated(); 327 | } 328 | 329 | } 330 | 331 | private void sortSelectedImgs(List imgs) 332 | { 333 | if(alreadySelectedImgs != null) 334 | { 335 | selectedImgBeans.clear(); 336 | for(ImgBean img : alreadySelectedImgs) 337 | { 338 | for(ImgBean imgBean : imgs) 339 | { 340 | if(img.getPath().equals(imgBean.getPath())) 341 | { 342 | imgBean.setSelected(true); 343 | selectedImgBeans.add(imgBean); 344 | } 345 | } 346 | } 347 | mtvMenu.setText("(" + selectedImgBeans.size() + "/" + MAX_COUNT + ")"); 348 | } 349 | } 350 | 351 | 352 | public void scanImgs() 353 | { 354 | if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 355 | { 356 | Toast.makeText(context, "当前存储卡不可用", Toast.LENGTH_SHORT).show(); 357 | return; 358 | } 359 | new Thread() 360 | { 361 | @Override 362 | public void run() { 363 | Uri muri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 364 | ContentResolver cr = activity.getContentResolver(); 365 | 366 | Cursor cursor = cr.query(muri, null, MediaStore.Images.Media.MIME_TYPE + " = ? or " + MediaStore.Images.Media.MIME_TYPE + " = ? ", new String[]{"image/jpeg", "image/png"}, MediaStore.Images.Media.DATE_MODIFIED); 367 | 368 | Set dirPaths = new HashSet(); 369 | int count = cursor.getCount(); 370 | for(int i = count - 1; i >= 0; i--) { 371 | cursor.moveToPosition(i); 372 | String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); 373 | 374 | if(i == count - 1) 375 | { 376 | ImgFloderBean imgflder = new ImgFloderBean(); 377 | imgflder.setFirstImgPath(path); 378 | imgflder.setDirName("所有图片"); 379 | imgflder.setType(0); 380 | imgflder.setSelected(true); 381 | imgFloderBeens.add(imgflder); 382 | } 383 | 384 | ImgBean imgBean = new ImgBean(); 385 | imgBean.setPath(path); 386 | imgBean.setSelected(false); 387 | // checkSelectedImgBeans(imgBean); 388 | imgs.add(imgBean); 389 | tempImgs.add(imgBean); 390 | File parentFile = new File(path).getParentFile(); 391 | 392 | if (parentFile == null) 393 | continue; 394 | String dirPath = parentFile.getAbsolutePath(); 395 | ImgFloderBean imgFloderBean = null; 396 | 397 | if (dirPaths.contains(dirPath)) { 398 | continue; 399 | } else { 400 | dirPaths.add(dirPath); 401 | imgFloderBean = new ImgFloderBean(); 402 | imgFloderBean.setDir(dirPath); 403 | imgFloderBean.setFirstImgPath(path); 404 | imgFloderBean.setDirName(parentFile.getName()); 405 | imgFloderBean.setType(1); 406 | imgFloderBean.setSelected(false); 407 | imgFloderBeens.add(imgFloderBean); 408 | } 409 | 410 | if (parentFile.list() == null) 411 | continue; 412 | 413 | int picSize = parentFile.list(new FilenameFilter() { 414 | @Override 415 | public boolean accept(File file, String s) { 416 | if (s.endsWith(".jpg") || s.endsWith(".jpeg") || s.endsWith(".png")) 417 | return true; 418 | return false; 419 | } 420 | }).length; 421 | 422 | if (imgFloderBean != null) { 423 | imgFloderBean.setSize(picSize); 424 | } 425 | 426 | } 427 | cursor.close(); 428 | Message message = Message.obtain(); 429 | message.what = 0x001; 430 | handler.sendMessage(message); 431 | } 432 | }.start(); 433 | 434 | } 435 | 436 | Handler handler = new Handler(){ 437 | @Override 438 | public void handleMessage(Message msg) { 439 | super.handleMessage(msg); 440 | if(msg.what == 0x001) 441 | { 442 | sortSelectedImgs(imgs); 443 | imgGridViewAdapter.setIsaddpath(false); 444 | imgGridViewAdapter.notifyDataSetInvalidated(); 445 | } 446 | progressDialog.dismiss(); 447 | } 448 | }; 449 | 450 | public void showFloor(final View v) { 451 | isShowListView = true; 452 | ValueAnimator animator; 453 | animator = ValueAnimator.ofFloat(listviewHeight, 0); 454 | animator.setTarget(v); 455 | animator.setDuration(200).start(); 456 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 457 | @Override 458 | public void onAnimationUpdate(ValueAnimator animation) { 459 | v.setTranslationY((Float) animation.getAnimatedValue()); 460 | } 461 | }); 462 | 463 | vBg.animate().alpha(1).setDuration(200); 464 | vBg.setVisibility(View.VISIBLE); 465 | } 466 | 467 | public void hideFloor(final View v) 468 | { 469 | isShowListView = false; 470 | ValueAnimator animator; 471 | animator = ValueAnimator.ofFloat(0, listviewHeight); 472 | animator.setTarget(v); 473 | animator.setDuration(200).start(); 474 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 475 | @Override 476 | public void onAnimationUpdate(ValueAnimator animation) { 477 | v.setTranslationY((Float) animation.getAnimatedValue()); 478 | } 479 | }); 480 | vBg.animate().alpha(0).setDuration(200); 481 | vBg.setVisibility(View.GONE); 482 | 483 | } 484 | 485 | public void initSystembar(View lySystemBar) 486 | { 487 | if(true) { 488 | if (Build.VERSION.SDK_INT >= 19) { 489 | if (lySystemBar != null) { 490 | lySystemBar.setVisibility(View.VISIBLE); 491 | LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) lySystemBar.getLayoutParams(); 492 | lp.height = getStatusHeight(activity); 493 | lySystemBar.requestLayout(); 494 | } 495 | } else { 496 | if (lySystemBar != null) { 497 | lySystemBar.setVisibility(View.GONE); 498 | } 499 | } 500 | } 501 | } 502 | 503 | public interface OnChoicePhotoListener 504 | { 505 | void onResult(List imgBeens); 506 | } 507 | 508 | public int getScreenHeight(Activity context) 509 | { 510 | DisplayMetrics dm = new DisplayMetrics(); 511 | context.getWindowManager().getDefaultDisplay().getMetrics(dm); 512 | int mScreenHeight = dm.heightPixels; 513 | return mScreenHeight; 514 | } 515 | 516 | public int dip2px(Context context, float dipValue) { 517 | final float scale = context.getResources().getDisplayMetrics().density; 518 | return (int) (dipValue * scale + 0.5f); 519 | } 520 | 521 | public static int getStatusHeight(Activity activity) 522 | { 523 | int statusBarHeight = 0; 524 | try { 525 | Class c = Class.forName("com.android.internal.R$dimen"); 526 | Object o = c.newInstance(); 527 | Field field = c.getField("status_bar_height"); 528 | int x = (Integer) field.get(o); 529 | statusBarHeight = activity.getResources().getDimensionPixelSize(x); 530 | } catch (Exception e) { 531 | e.printStackTrace(); 532 | Rect frame = new Rect(); 533 | activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); 534 | statusBarHeight = frame.top; 535 | } 536 | return statusBarHeight; 537 | } 538 | } 539 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/java/com/ywl5320/csdn/pickphoto/dialog/PickPhotoDialog.java: -------------------------------------------------------------------------------- 1 | package com.ywl5320.csdn.pickphoto.dialog; 2 | 3 | import android.Manifest; 4 | import android.app.Activity; 5 | import android.content.ContentValues; 6 | import android.content.Context; 7 | import android.content.Intent; 8 | import android.content.pm.PackageManager; 9 | import android.database.Cursor; 10 | import android.graphics.Bitmap; 11 | import android.net.Uri; 12 | import android.os.Build; 13 | import android.os.Bundle; 14 | import android.os.Environment; 15 | import android.provider.MediaStore; 16 | import android.support.v4.app.ActivityCompat; 17 | import android.support.v4.content.ContextCompat; 18 | import android.support.v4.content.FileProvider; 19 | import android.view.View; 20 | import android.widget.Button; 21 | import android.widget.Toast; 22 | 23 | 24 | import com.ywl5320.csdn.pickphoto.R; 25 | import com.ywl5320.csdn.pickphoto.beans.ImgBean; 26 | import com.ywl5320.csdn.pickphoto.util.AppUtil; 27 | 28 | import java.io.File; 29 | import java.io.FileNotFoundException; 30 | import java.util.List; 31 | 32 | /** 33 | * Created by ywl on 2016/6/14. 34 | */ 35 | public class PickPhotoDialog extends BaseDialog{ 36 | 37 | private OnPhotoResultListener onPhotoResultListener; 38 | private static final int REQUEST_CAMERA_CODE = 0x0002; 39 | private static final int REQUEST_EXTERNAL_STORAGE_CODE = 0x0003; 40 | private static final int REQUEST_CAMERA_RESULT_CODE = 0x0004; 41 | private static final int REQUEST_CLICK_PHOTO_CODE = 0x0005; 42 | 43 | private Activity activity; 44 | private File file; 45 | private Uri imageUri; 46 | private String imgname = ""; 47 | 48 | private boolean isCutImg = false; 49 | private int maxcount = 1; 50 | 51 | private Button btnCamera; 52 | private Button btnPhoto; 53 | 54 | private List selectedImgs; 55 | 56 | public void setSelectedImgs(List selectedImgs) { 57 | this.selectedImgs = selectedImgs; 58 | } 59 | 60 | public PickPhotoDialog(Context context, Activity activity) { 61 | super(context); 62 | this.activity = activity; 63 | } 64 | 65 | public void setCutImg(boolean cutImg, int maxcount) { 66 | isCutImg = cutImg; 67 | this.maxcount = maxcount; 68 | if(isCutImg) 69 | { 70 | this.maxcount = 1; 71 | } 72 | } 73 | 74 | @Override 75 | protected void onCreate(Bundle savedInstanceState) { 76 | super.onCreate(savedInstanceState); 77 | setContentView(R.layout.dialog_choice_photo_layout); 78 | btnCamera = (Button) findViewById(R.id.btn_takephoto); 79 | btnPhoto = (Button) findViewById(R.id.btn_photo); 80 | 81 | btnCamera.setOnClickListener(new View.OnClickListener() { 82 | @Override 83 | public void onClick(View view) { 84 | if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 85 | { 86 | Toast.makeText(context, "当前存储卡不可用", Toast.LENGTH_SHORT).show(); 87 | return; 88 | } 89 | if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED//相机权限 90 | || ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED//读取存储卡权限 91 | || ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) 92 | { 93 | ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_CODE); 94 | } 95 | else 96 | { 97 | file = new File(getImgPath()); 98 | if (!file.exists()) { 99 | file.mkdirs(); 100 | } 101 | imgname = getHeadImgName(); 102 | 103 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 104 | 105 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M){ 106 | imageUri = Uri.fromFile(new File(file, imgname)); 107 | }else{ 108 | //7.0 调用系统相机拍照不再允许使用Uri方式,应该替换为FileProvider 并且这样可以解决MIUI系统上拍照返回size为0的情况 109 | imageUri = FileProvider.getUriForFile(context, AppUtil.getAppPackName(context) + ".provider", new File(file, imgname)); 110 | } 111 | intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 112 | activity.startActivityForResult(intent, REQUEST_CAMERA_RESULT_CODE); 113 | } 114 | } 115 | }); 116 | 117 | btnPhoto.setOnClickListener(new View.OnClickListener() { 118 | @Override 119 | public void onClick(View view) { 120 | if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 121 | { 122 | Toast.makeText(context, "当前存储卡不可用", Toast.LENGTH_SHORT).show(); 123 | return; 124 | } 125 | if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED 126 | || ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) 127 | { 128 | ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE_CODE); 129 | } 130 | else 131 | { 132 | PhotoListDialog photoListDialog = new PhotoListDialog(context, activity); 133 | photoListDialog.setMAX_COUNT(maxcount); 134 | photoListDialog.setAlreadySelectedImgs(selectedImgs); 135 | photoListDialog.show(); 136 | photoListDialog.setOnChoicePhotoListener(new PhotoListDialog.OnChoicePhotoListener() { 137 | @Override 138 | public void onResult(List imgBeens) { 139 | if(isCutImg && imgBeens != null && imgBeens.size() > 0) 140 | { 141 | try { 142 | Uri photouri = Uri.fromFile(new File(imgBeens.get(0).getPath())); 143 | cropPhoto(photouri); 144 | } 145 | catch(Exception e) 146 | { 147 | e.printStackTrace(); 148 | } 149 | } 150 | else { 151 | if (onPhotoResultListener != null) { 152 | onPhotoResultListener.onPhotoResult(imgBeens); 153 | } 154 | dismiss(); 155 | } 156 | } 157 | }); 158 | } 159 | } 160 | }); 161 | } 162 | 163 | public void setOnPhotoResultListener(OnPhotoResultListener onPhotoResultListener) { 164 | this.onPhotoResultListener = onPhotoResultListener; 165 | } 166 | 167 | public interface OnPhotoResultListener 168 | { 169 | void onCameraResult(String path); 170 | 171 | void onCutPhotoResult(Bitmap bitmap); 172 | 173 | void onPhotoResult(List selectedImgs); 174 | } 175 | 176 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 177 | { 178 | 179 | if (requestCode == REQUEST_CAMERA_CODE) 180 | { 181 | if (grantResults[0] == PackageManager.PERMISSION_GRANTED) 182 | { 183 | file = new File(getImgPath()); 184 | if (!file.exists()) { 185 | file.mkdirs(); 186 | } 187 | imgname = getHeadImgName(); 188 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 189 | 190 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M){ 191 | imageUri = Uri.fromFile(new File(file, imgname)); 192 | }else{ 193 | //7.0 调用系统相机拍照不再允许使用Uri方式,应该替换为FileProvider 并且这样可以解决MIUI系统上拍照返回size为0的情况 194 | imageUri = FileProvider.getUriForFile(context, AppUtil.getAppPackName(context) + ".provider", new File(file, imgname)); 195 | } 196 | intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 197 | activity.startActivityForResult(intent, REQUEST_CAMERA_RESULT_CODE); 198 | } else 199 | { 200 | Toast.makeText(context, "请允许打开摄像头权限", Toast.LENGTH_SHORT).show(); 201 | } 202 | return; 203 | } 204 | else if(requestCode == REQUEST_EXTERNAL_STORAGE_CODE) 205 | { 206 | if (grantResults[0] == PackageManager.PERMISSION_GRANTED) 207 | { 208 | PhotoListDialog photoListDialog = new PhotoListDialog(context, activity); 209 | photoListDialog.setMAX_COUNT(9); 210 | photoListDialog.show(); 211 | } else 212 | { 213 | Toast.makeText(context, "请允许读取存储卡权限", Toast.LENGTH_SHORT).show(); 214 | } 215 | return; 216 | } 217 | } 218 | 219 | public void onActivityResult(int requestCode, int resultCode, Intent data) { 220 | if(resultCode!= activity.RESULT_OK) 221 | return; 222 | if(requestCode == REQUEST_CAMERA_RESULT_CODE) 223 | { 224 | try { 225 | MediaStore.Images.Media.insertImage(context.getContentResolver(), 226 | imageUri.getPath(), imgname, null); 227 | } catch (FileNotFoundException e) { 228 | e.printStackTrace(); 229 | } 230 | // 最后通知图库更新 231 | context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getPath()))); 232 | 233 | if(!isCutImg) { 234 | if (onPhotoResultListener != null) { 235 | onPhotoResultListener.onCameraResult(imageUri.getPath()); 236 | } 237 | dismiss(); 238 | } 239 | else 240 | { 241 | cropPhoto(imageUri); 242 | } 243 | } 244 | else if(requestCode == REQUEST_CLICK_PHOTO_CODE) 245 | { 246 | Bitmap photo = data.getParcelableExtra("data"); 247 | if(photo!= null){ 248 | if (onPhotoResultListener != null) { 249 | onPhotoResultListener.onCutPhotoResult(photo); 250 | } 251 | } 252 | dismiss(); 253 | } 254 | } 255 | 256 | public void cropPhoto(Uri uri) 257 | { 258 | Intent intent = new Intent(); 259 | intent.setAction("com.android.camera.action.CROP"); 260 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M){ 261 | intent.setDataAndType(uri, "image/*"); 262 | }else{ 263 | intent.setDataAndType(getImageContentUri(new File(uri.getPath())), "image/*");// mUri是已经选择的图片Uri 264 | } 265 | intent.putExtra("crop", "true"); 266 | intent.putExtra("aspectX", 1);// 裁剪框比例 267 | intent.putExtra("aspectY", 1); 268 | intent.putExtra("outputX", 320);// 输出图片大小 269 | intent.putExtra("outputY", 320); 270 | intent.putExtra("return-data", true); 271 | activity.startActivityForResult(intent, REQUEST_CLICK_PHOTO_CODE); 272 | } 273 | 274 | public String getImgPath() { 275 | return Environment.getExternalStorageDirectory().getPath() + "/bdgames/images/"; 276 | } 277 | 278 | public String getHeadImgName() 279 | { 280 | return System.currentTimeMillis() + ".jpg"; 281 | } 282 | 283 | /** 284 | * 转换 content:// uri 285 | * 286 | * @param imageFile 287 | * @return 288 | */ 289 | public Uri getImageContentUri(File imageFile) { 290 | String filePath = imageFile.getAbsolutePath(); 291 | Cursor cursor = context.getContentResolver().query( 292 | MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 293 | new String[] { MediaStore.Images.Media._ID }, 294 | MediaStore.Images.Media.DATA + "=? ", 295 | new String[] { filePath }, null); 296 | 297 | if (cursor != null && cursor.moveToFirst()) { 298 | int id = cursor.getInt(cursor 299 | .getColumnIndex(MediaStore.MediaColumns._ID)); 300 | Uri baseUri = Uri.parse("content://media/external/images/media"); 301 | return Uri.withAppendedPath(baseUri, "" + id); 302 | } else { 303 | if (imageFile.exists()) { 304 | ContentValues values = new ContentValues(); 305 | values.put(MediaStore.Images.Media.DATA, filePath); 306 | return context.getContentResolver().insert( 307 | MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 308 | } else { 309 | return null; 310 | } 311 | } 312 | } 313 | } 314 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/java/com/ywl5320/csdn/pickphoto/util/AppUtil.java: -------------------------------------------------------------------------------- 1 | package com.ywl5320.csdn.pickphoto.util; 2 | 3 | import android.app.ActivityManager; 4 | import android.content.Context; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * Created by ywl5320 on 2017/8/22. 10 | */ 11 | 12 | public class AppUtil { 13 | 14 | /** 15 | * 获取当前应用程序的包名 16 | * 17 | * @param context 上下文对象 18 | * @return 返回包名 19 | */ 20 | public static String getAppPackName(Context context) { 21 | //当前应用pid 22 | int pid = android.os.Process.myPid(); 23 | //任务管理类 24 | ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 25 | //遍历所有应用 26 | List infos = manager.getRunningAppProcesses(); 27 | for (ActivityManager.RunningAppProcessInfo info : infos) { 28 | if (info.pid == pid)//得到当前应用 29 | return info.processName;//返回包名 30 | } 31 | return ""; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/res/anim/dialog_enter.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/res/anim/dialog_exit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/res/drawable/blue_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/res/drawable/blue_item_r.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/res/drawable/btn_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/res/drawable/circle_drawable.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/res/drawable/circle_drawable_pressed.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/res/drawable/dot_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/res/drawable/item_selected_folder.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /PickPhotoDialog/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 10 | 15 | 16 | 25 | 26 | 27 | 28 |