├── .classpath ├── .gitignore ├── .project ├── .settings ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── AndroidManifest.xml ├── LICENSE ├── README.md ├── ic_launcher-web.png ├── libs └── android-support-v4.jar ├── proguard-project.txt ├── project.properties ├── res ├── drawable-hdpi │ ├── acm_inputbox.9.png │ ├── bg_common_toast.9.png │ ├── emotionstore_progresscancelbtn.png │ ├── ic_launcher.png │ ├── number_base.png │ └── search.png ├── drawable-mdpi │ └── ic_launcher.png ├── drawable-xhdpi │ └── ic_launcher.png ├── drawable-xxhdpi │ └── ic_launcher.png ├── drawable │ ├── comm_btn_selector.xml │ └── sidebar_background.xml ├── layout │ ├── activity_main_contact.xml │ └── phone_constacts_item.xml └── values │ ├── colors.xml │ ├── drawables.xml │ ├── strings.xml │ └── styles.xml └── src └── com └── finddreams └── sortedcontact ├── BitmapUtil.java ├── ClearEditText.java ├── ConstactUtil.java ├── ImageTextView.java ├── MainActivity.java ├── PinyinComparator.java ├── SortAdapter.java └── sortlist ├── CharacterParser.java ├── SideBar.java └── SortModel.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | SortedContactUI 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.source=1.6 5 | -------------------------------------------------------------------------------- /AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 9 | 10 | 11 | 16 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /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 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SortedContactUI 2 | 3 | 关注finddreams,一起分享,一起进步!http://blog.csdn.net/finddreams/article/details/44624111 4 | 5 | 话不多说,直接上干货,图片如下: 6 | ![这里写图片描述](http://img.blog.csdn.net/20150325161803867) 7 | 8 | 仿Android联系人SideBar排序,根据拼音A-Z字母快速查找,以及输入搜索条件过滤,显示姓名的文字图片。 9 | 这样的效果相信大家并不陌生,我们在APP中都司空见惯了,比如在选择地址的时候,选择国家,省份等等。这样的效果很多大神也都写过相应的博客,大体上实现的方式都差不多,今天我也给大家模仿一下Android联系人的排序实现。 10 | -------------------------------------------------------------------------------- /ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/finddreams/SortedContactUI/def54a47823cd9f5af328d3d05f7ad23b4ffeb5a/ic_launcher-web.png -------------------------------------------------------------------------------- /libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/finddreams/SortedContactUI/def54a47823cd9f5af328d3d05f7ad23b4ffeb5a/libs/android-support-v4.jar -------------------------------------------------------------------------------- /proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-16 15 | -------------------------------------------------------------------------------- /res/drawable-hdpi/acm_inputbox.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/finddreams/SortedContactUI/def54a47823cd9f5af328d3d05f7ad23b4ffeb5a/res/drawable-hdpi/acm_inputbox.9.png -------------------------------------------------------------------------------- /res/drawable-hdpi/bg_common_toast.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/finddreams/SortedContactUI/def54a47823cd9f5af328d3d05f7ad23b4ffeb5a/res/drawable-hdpi/bg_common_toast.9.png -------------------------------------------------------------------------------- /res/drawable-hdpi/emotionstore_progresscancelbtn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/finddreams/SortedContactUI/def54a47823cd9f5af328d3d05f7ad23b4ffeb5a/res/drawable-hdpi/emotionstore_progresscancelbtn.png -------------------------------------------------------------------------------- /res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/finddreams/SortedContactUI/def54a47823cd9f5af328d3d05f7ad23b4ffeb5a/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-hdpi/number_base.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/finddreams/SortedContactUI/def54a47823cd9f5af328d3d05f7ad23b4ffeb5a/res/drawable-hdpi/number_base.png -------------------------------------------------------------------------------- /res/drawable-hdpi/search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/finddreams/SortedContactUI/def54a47823cd9f5af328d3d05f7ad23b4ffeb5a/res/drawable-hdpi/search.png -------------------------------------------------------------------------------- /res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/finddreams/SortedContactUI/def54a47823cd9f5af328d3d05f7ad23b4ffeb5a/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/finddreams/SortedContactUI/def54a47823cd9f5af328d3d05f7ad23b4ffeb5a/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/finddreams/SortedContactUI/def54a47823cd9f5af328d3d05f7ad23b4ffeb5a/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable/comm_btn_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /res/drawable/sidebar_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /res/layout/activity_main_contact.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 25 | 26 | 29 | 30 | 37 | 38 | 48 | 49 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /res/layout/phone_constacts_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 19 | 26 | 33 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ffffff 5 | #AFAFAF 6 | #F99503 7 | #ffe8ecef 8 | #EDEDED 9 | 10 | -------------------------------------------------------------------------------- /res/values/drawables.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ffffffff 4 | 5 | 6 | -------------------------------------------------------------------------------- /res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | SortedContactUI 4 | 搜索 5 | 6 | 7 | -------------------------------------------------------------------------------- /res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/com/finddreams/sortedcontact/BitmapUtil.java: -------------------------------------------------------------------------------- 1 | package com.finddreams.sortedcontact; 2 | 3 | import java.io.BufferedOutputStream; 4 | import java.io.File; 5 | import java.io.FileNotFoundException; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | import java.util.UUID; 9 | 10 | import android.content.Context; 11 | import android.graphics.Bitmap; 12 | import android.graphics.Bitmap.CompressFormat; 13 | import android.graphics.Bitmap.Config; 14 | import android.graphics.BitmapFactory; 15 | import android.graphics.Canvas; 16 | import android.graphics.Color; 17 | import android.graphics.Paint; 18 | import android.graphics.Paint.FontMetrics; 19 | import android.graphics.PixelFormat; 20 | import android.graphics.PorterDuff.Mode; 21 | import android.graphics.PorterDuffXfermode; 22 | import android.graphics.Rect; 23 | import android.graphics.RectF; 24 | import android.graphics.drawable.BitmapDrawable; 25 | import android.graphics.drawable.Drawable; 26 | 27 | 28 | public class BitmapUtil { 29 | 30 | // 保存 bitmap 到SD卡F 31 | public static boolean saveBitmapToSDCard(Bitmap bitmap, String filePath, 32 | String fileName) { 33 | boolean flag = false; 34 | if (null != bitmap) { 35 | try { 36 | fileName = fileName + ".jpg"; 37 | File file = new File(filePath); 38 | if (!file.exists()) { 39 | file.mkdirs(); 40 | } 41 | File f = new File(filePath + fileName); 42 | if (f.exists()) { 43 | f.delete(); 44 | } 45 | BufferedOutputStream outputStream = new BufferedOutputStream( 46 | new FileOutputStream(f)); 47 | bitmap.compress(CompressFormat.JPEG, 100, outputStream); 48 | outputStream.flush(); 49 | outputStream.close(); 50 | flag = true; 51 | } catch (FileNotFoundException e) { 52 | flag = false; 53 | } catch (IOException e) { 54 | flag = false; 55 | } 56 | } 57 | return flag; 58 | 59 | } 60 | 61 | /** 62 | * 63 | * @param drawable 64 | * @return bitmap 65 | */ 66 | public static Bitmap drawableToBitmap2(Drawable drawable) { 67 | BitmapDrawable bd = (BitmapDrawable) drawable; 68 | return bd.getBitmap(); 69 | } 70 | 71 | /** 72 | * 73 | * @param bitmap 74 | * @return 75 | */ 76 | public static Drawable bitmapTodrawable(Bitmap bitmap) { 77 | Drawable drawable = new BitmapDrawable(bitmap); 78 | drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight()); 79 | return drawable; 80 | } 81 | 82 | public static Bitmap drawableToBitmap(Drawable drawable) { 83 | // 取 drawable 的长宽 84 | int w = drawable.getIntrinsicWidth(); 85 | int h = drawable.getIntrinsicHeight(); 86 | 87 | // 取 drawable 的颜色格式 88 | Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 89 | : Bitmap.Config.RGB_565; 90 | // 建立对应 bitmap 91 | Bitmap bitmap = Bitmap.createBitmap(w, h, config); 92 | // 建立对应 bitmap 的画布 93 | Canvas canvas = new Canvas(bitmap); 94 | drawable.setBounds(0, 0, w, h); 95 | // 把 drawable 内容画到画布中 96 | drawable.draw(canvas); 97 | return bitmap; 98 | } 99 | 100 | /** 101 | * 根据文字获取图片 102 | * 103 | * @param text 104 | * @return 105 | */ 106 | public static Bitmap getIndustry(Context context, String text) { 107 | String color = "#ffeeeade"; 108 | 109 | Bitmap src = BitmapFactory.decodeResource(context.getResources(), 110 | R.drawable.ic_launcher); 111 | int x = src.getWidth(); 112 | int y = src.getHeight(); 113 | Bitmap bmp = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888); 114 | Canvas canvasTemp = new Canvas(bmp); 115 | canvasTemp.drawColor(Color.parseColor(color)); 116 | Paint p = new Paint(Paint.FAKE_BOLD_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 117 | p.setColor(Color.parseColor("#ff4e0a13")); 118 | p.setAlpha(45); 119 | p.setFilterBitmap(true); 120 | int size = (int) (18 * context.getResources().getDisplayMetrics().density); 121 | p.setTextSize(size); 122 | float tX = (x - getFontlength(p, text)) / 2; 123 | float tY = (y - getFontHeight(p)) / 2 + getFontLeading(p); 124 | canvasTemp.drawText(text, tX, tY, p); 125 | return toRoundCorner(bmp, 2); 126 | } 127 | 128 | /** 129 | * @return 返回指定笔和指定字符串的长度 130 | */ 131 | public static float getFontlength(Paint paint, String str) { 132 | return paint.measureText(str); 133 | } 134 | 135 | /** 136 | * @return 返回指定笔的文字高度 137 | */ 138 | public static float getFontHeight(Paint paint) { 139 | FontMetrics fm = paint.getFontMetrics(); 140 | return fm.descent - fm.ascent; 141 | } 142 | 143 | /** 144 | * @return 返回指定笔离文字顶部的基准距离 145 | */ 146 | public static float getFontLeading(Paint paint) { 147 | FontMetrics fm = paint.getFontMetrics(); 148 | return fm.leading - fm.ascent; 149 | } 150 | 151 | /** 152 | * 获取圆角图片 153 | * 154 | * @param bitmap 155 | * @param pixels 156 | * @return 157 | */ 158 | public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { 159 | 160 | Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), 161 | bitmap.getHeight(), Config.ARGB_8888); 162 | Canvas canvas = new Canvas(output); 163 | 164 | final int color = 0xff424242; 165 | final Paint paint = new Paint(); 166 | final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 167 | final RectF rectF = new RectF(rect); 168 | final float roundPx = pixels; 169 | 170 | paint.setAntiAlias(true); 171 | canvas.drawARGB(0, 0, 0, 0); 172 | paint.setColor(color); 173 | canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 174 | 175 | paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 176 | canvas.drawBitmap(bitmap, rect, rect, paint); 177 | 178 | return output; 179 | } 180 | 181 | 182 | } 183 | -------------------------------------------------------------------------------- /src/com/finddreams/sortedcontact/ClearEditText.java: -------------------------------------------------------------------------------- 1 | package com.finddreams.sortedcontact; 2 | 3 | 4 | import android.content.Context; 5 | import android.graphics.drawable.Drawable; 6 | import android.text.Editable; 7 | import android.text.TextWatcher; 8 | import android.util.AttributeSet; 9 | import android.view.MotionEvent; 10 | import android.view.View; 11 | import android.view.View.OnFocusChangeListener; 12 | import android.view.animation.Animation; 13 | import android.view.animation.CycleInterpolator; 14 | import android.view.animation.TranslateAnimation; 15 | import android.widget.EditText; 16 | /** 17 | * @Description:带删除按钮的EditText 18 | * @author http://blog.csdn.net/finddreams 19 | */ 20 | public class ClearEditText extends EditText implements 21 | OnFocusChangeListener, TextWatcher { 22 | /** 23 | * 删除按钮的引用 24 | */ 25 | private Drawable mClearDrawable; 26 | 27 | public ClearEditText(Context context) { 28 | this(context, null); 29 | } 30 | 31 | public ClearEditText(Context context, AttributeSet attrs) { 32 | //这里构造方法也很重要,不加这个很多属性不能再XML里面定义 33 | this(context, attrs, android.R.attr.editTextStyle); 34 | } 35 | 36 | public ClearEditText(Context context, AttributeSet attrs, int defStyle) { 37 | super(context, attrs, defStyle); 38 | init(); 39 | } 40 | 41 | 42 | private void init() { 43 | //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片 44 | mClearDrawable = getCompoundDrawables()[2]; 45 | if (mClearDrawable == null) { 46 | mClearDrawable = getResources() 47 | .getDrawable(R.drawable.emotionstore_progresscancelbtn); 48 | } 49 | mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight()); 50 | setClearIconVisible(false); 51 | setOnFocusChangeListener(this); 52 | addTextChangedListener(this); 53 | } 54 | 55 | 56 | /** 57 | * 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件 58 | * 当我们按下的位置 在 EditText的宽度 - 图标到控件右边的间距 - 图标的宽度 和 59 | * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向没有考虑 60 | */ 61 | @Override 62 | public boolean onTouchEvent(MotionEvent event) { 63 | if (getCompoundDrawables()[2] != null) { 64 | if (event.getAction() == MotionEvent.ACTION_UP) { 65 | boolean touchable = event.getX() > (getWidth() 66 | - getPaddingRight() - mClearDrawable.getIntrinsicWidth()) 67 | && (event.getX() < ((getWidth() - getPaddingRight()))); 68 | if (touchable) { 69 | this.setText(""); 70 | } 71 | } 72 | } 73 | 74 | return super.onTouchEvent(event); 75 | } 76 | 77 | /** 78 | * 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏 79 | */ 80 | @Override 81 | public void onFocusChange(View v, boolean hasFocus) { 82 | if (hasFocus) { 83 | setClearIconVisible(getText().length() > 0); 84 | } else { 85 | setClearIconVisible(false); 86 | } 87 | } 88 | 89 | 90 | /** 91 | * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去 92 | * @param visible 93 | */ 94 | protected void setClearIconVisible(boolean visible) { 95 | Drawable right = visible ? mClearDrawable : null; 96 | setCompoundDrawables(getCompoundDrawables()[0], 97 | getCompoundDrawables()[1], right, getCompoundDrawables()[3]); 98 | } 99 | 100 | 101 | /** 102 | * 当输入框里面内容发生变化的时候回调的方法 103 | */ 104 | @Override 105 | public void onTextChanged(CharSequence s, int start, int count, 106 | int after) { 107 | setClearIconVisible(s.length() > 0); 108 | } 109 | 110 | @Override 111 | public void beforeTextChanged(CharSequence s, int start, int count, 112 | int after) { 113 | 114 | } 115 | 116 | @Override 117 | public void afterTextChanged(Editable s) { 118 | 119 | } 120 | 121 | 122 | /** 123 | * 设置晃动动画 124 | */ 125 | public void setShakeAnimation(){ 126 | this.setAnimation(shakeAnimation(5)); 127 | } 128 | 129 | 130 | /** 131 | * 晃动动画 132 | * @param counts 1秒钟晃动多少下 133 | * @return 134 | */ 135 | public static Animation shakeAnimation(int counts){ 136 | Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0); 137 | translateAnimation.setInterpolator(new CycleInterpolator(counts)); 138 | translateAnimation.setDuration(1000); 139 | return translateAnimation; 140 | } 141 | 142 | 143 | } 144 | -------------------------------------------------------------------------------- /src/com/finddreams/sortedcontact/ConstactUtil.java: -------------------------------------------------------------------------------- 1 | package com.finddreams.sortedcontact; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import android.content.Context; 7 | import android.database.Cursor; 8 | import android.provider.ContactsContract; 9 | 10 | public class ConstactUtil { 11 | /** 12 | * 获取所有数据 13 | * 14 | * @return 15 | */ 16 | public static Map getAllCallRecords(Context context) { 17 | Map temp = new HashMap(); 18 | Cursor c = context.getContentResolver().query( 19 | ContactsContract.Contacts.CONTENT_URI, 20 | null, 21 | null, 22 | null, 23 | ContactsContract.Contacts.DISPLAY_NAME 24 | + " COLLATE LOCALIZED ASC"); 25 | if (c.moveToFirst()) { 26 | do { 27 | // 获得联系人的ID号 28 | String contactId = c.getString(c 29 | .getColumnIndex(ContactsContract.Contacts._ID)); 30 | // 获得联系人姓名 31 | String name = c 32 | .getString(c 33 | .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 34 | // 查看该联系人有多少个电话号码。如果没有这返回值为0 35 | int phoneCount = c 36 | .getInt(c 37 | .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 38 | String number=null; 39 | if (phoneCount > 0) { 40 | // 获得联系人的电话号码 41 | Cursor phones = context.getContentResolver().query( 42 | ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 43 | null, 44 | ContactsContract.CommonDataKinds.Phone.CONTACT_ID 45 | + " = " + contactId, null, null); 46 | if (phones.moveToFirst()) { 47 | number = phones 48 | .getString(phones 49 | .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 50 | } 51 | phones.close(); 52 | } 53 | temp.put(name, number); 54 | } while (c.moveToNext()); 55 | } 56 | c.close(); 57 | return temp; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/com/finddreams/sortedcontact/ImageTextView.java: -------------------------------------------------------------------------------- 1 | package com.finddreams.sortedcontact; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.graphics.drawable.Drawable; 6 | import android.util.AttributeSet; 7 | import android.widget.TextView; 8 | /** 9 | * @Description: 文字图片,这个相信大家都知道,比如QQ底部导航上的未读消息数 10 | * 11 | * @author http://blog.csdn.net/finddreams 12 | */ 13 | public class ImageTextView extends TextView { 14 | private Bitmap bitmap; 15 | private String text; 16 | Drawable d; 17 | 18 | public ImageTextView(Context context) { 19 | super(context); 20 | 21 | } 22 | 23 | public ImageTextView(Context context, AttributeSet attrs, int defStyle) { 24 | super(context, attrs, defStyle); 25 | 26 | } 27 | 28 | public ImageTextView(Context context, AttributeSet attrs) { 29 | super(context, attrs); 30 | 31 | } 32 | 33 | public void setIconText(Context context,String text){ 34 | text = this.getText().toString().substring(0, 1); 35 | bitmap = BitmapUtil.getIndustry(context, text); 36 | d = BitmapUtil.bitmapTodrawable(bitmap); 37 | this.setCompoundDrawables(d, null, null, null); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/com/finddreams/sortedcontact/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.finddreams.sortedcontact; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.Iterator; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | import android.annotation.SuppressLint; 10 | import android.app.Activity; 11 | import android.content.Context; 12 | import android.os.AsyncTask; 13 | import android.os.Bundle; 14 | import android.text.Editable; 15 | import android.text.TextUtils; 16 | import android.text.TextWatcher; 17 | import android.view.Gravity; 18 | import android.view.View; 19 | import android.view.View.OnFocusChangeListener; 20 | import android.widget.AdapterView; 21 | import android.widget.AdapterView.OnItemClickListener; 22 | import android.widget.ListView; 23 | import android.widget.TextView; 24 | import android.widget.Toast; 25 | 26 | import com.finddreams.sortedcontact.sortlist.CharacterParser; 27 | import com.finddreams.sortedcontact.sortlist.SideBar; 28 | import com.finddreams.sortedcontact.sortlist.SortModel; 29 | import com.finddreams.sortedcontact.sortlist.SideBar.OnTouchingLetterChangedListener; 30 | 31 | 32 | /** 33 | * @Description:联系人显示界面 34 | * @author http://blog.csdn.net/finddreams 35 | */ 36 | public class MainActivity extends Activity { 37 | 38 | private View mBaseView; 39 | private ListView sortListView; 40 | private SideBar sideBar; 41 | private TextView dialog; 42 | private SortAdapter adapter; 43 | private ClearEditText mClearEditText; 44 | private Map callRecords; 45 | 46 | private CharacterParser characterParser; 47 | private List SourceDateList; 48 | 49 | private PinyinComparator pinyinComparator; 50 | 51 | @Override 52 | protected void onCreate(Bundle savedInstanceState) { 53 | // TODO Auto-generated method stub 54 | super.onCreate(savedInstanceState); 55 | setContentView(R.layout.activity_main_contact); 56 | initView(); 57 | initData(); 58 | } 59 | 60 | private void initView() { 61 | sideBar = (SideBar) this.findViewById(R.id.sidrbar); 62 | dialog = (TextView) this.findViewById(R.id.dialog); 63 | 64 | sortListView = (ListView) this.findViewById(R.id.sortlist); 65 | 66 | } 67 | 68 | private void initData() { 69 | // 实例化汉字转拼音类 70 | characterParser = CharacterParser.getInstance(); 71 | 72 | pinyinComparator = new PinyinComparator(); 73 | 74 | sideBar.setTextView(dialog); 75 | 76 | // 设置右侧触摸监听 77 | sideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() { 78 | 79 | @SuppressLint("NewApi") 80 | @Override 81 | public void onTouchingLetterChanged(String s) { 82 | // 该字母首次出现的位置 83 | int position = adapter.getPositionForSection(s.charAt(0)); 84 | if (position != -1) { 85 | sortListView.setSelection(position); 86 | } 87 | } 88 | }); 89 | 90 | sortListView.setOnItemClickListener(new OnItemClickListener() { 91 | 92 | @Override 93 | public void onItemClick(AdapterView parent, View view, 94 | int position, long id) { 95 | // 这里要利用adapter.getItem(position)来获取当前position所对应的对象 96 | // Toast.makeText(getApplication(), 97 | // ((SortModel)adapter.getItem(position)).getName(), 98 | // Toast.LENGTH_SHORT).show(); 99 | String number = callRecords.get(((SortModel) adapter 100 | .getItem(position)).getName()); 101 | Toast.makeText(MainActivity.this, number, 0).show(); 102 | } 103 | }); 104 | 105 | new ConstactAsyncTask().execute(0); 106 | 107 | } 108 | 109 | private class ConstactAsyncTask extends AsyncTask { 110 | 111 | @Override 112 | protected Integer doInBackground(Integer... arg0) { 113 | int result = -1; 114 | callRecords = ConstactUtil.getAllCallRecords(MainActivity.this); 115 | result = 1; 116 | return result; 117 | } 118 | @Override 119 | protected void onPostExecute(Integer result) { 120 | super.onPostExecute(result); 121 | if (result == 1) { 122 | List constact = new ArrayList(); 123 | for (Iterator keys = callRecords.keySet().iterator(); keys 124 | .hasNext();) { 125 | String key = keys.next(); 126 | constact.add(key); 127 | } 128 | String[] names = new String[] {}; 129 | names = constact.toArray(names); 130 | SourceDateList = filledData(names); 131 | 132 | // 根据a-z进行排序源数据 133 | Collections.sort(SourceDateList, pinyinComparator); 134 | adapter = new SortAdapter(MainActivity.this, SourceDateList); 135 | sortListView.setAdapter(adapter); 136 | 137 | mClearEditText = (ClearEditText) MainActivity.this 138 | .findViewById(R.id.filter_edit); 139 | mClearEditText.setOnFocusChangeListener(new OnFocusChangeListener() { 140 | 141 | @Override 142 | public void onFocusChange(View arg0, boolean arg1) { 143 | mClearEditText.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL); 144 | 145 | } 146 | }); 147 | // 根据输入框输入值的改变来过滤搜索 148 | mClearEditText.addTextChangedListener(new TextWatcher() { 149 | 150 | @Override 151 | public void onTextChanged(CharSequence s, int start, 152 | int before, int count) { 153 | // 当输入框里面的值为空,更新为原来的列表,否则为过滤数据列表 154 | filterData(s.toString()); 155 | } 156 | 157 | @Override 158 | public void beforeTextChanged(CharSequence s, int start, 159 | int count, int after) { 160 | 161 | } 162 | 163 | @Override 164 | public void afterTextChanged(Editable s) { 165 | } 166 | }); 167 | } 168 | } 169 | 170 | @Override 171 | protected void onPreExecute() { 172 | super.onPreExecute(); 173 | } 174 | 175 | } 176 | 177 | /** 178 | * 为ListView填充数据 179 | * 180 | * @param date 181 | * @return 182 | */ 183 | private List filledData(String[] date) { 184 | List mSortList = new ArrayList(); 185 | 186 | for (int i = 0; i < date.length; i++) { 187 | SortModel sortModel = new SortModel(); 188 | sortModel.setName(date[i]); 189 | // 汉字转换成拼音 190 | String pinyin = characterParser.getSelling(date[i]); 191 | String sortString = pinyin.substring(0, 1).toUpperCase(); 192 | 193 | // 正则表达式,判断首字母是否是英文字母 194 | if (sortString.matches("[A-Z]")) { 195 | sortModel.setSortLetters(sortString.toUpperCase()); 196 | } else { 197 | sortModel.setSortLetters("#"); 198 | } 199 | 200 | mSortList.add(sortModel); 201 | } 202 | return mSortList; 203 | 204 | } 205 | 206 | /** 207 | * 根据输入框中的值来过滤数据并更新ListView 208 | * 209 | * @param filterStr 210 | */ 211 | private void filterData(String filterStr) { 212 | List filterDateList = new ArrayList(); 213 | 214 | if (TextUtils.isEmpty(filterStr)) { 215 | filterDateList = SourceDateList; 216 | } else { 217 | filterDateList.clear(); 218 | for (SortModel sortModel : SourceDateList) { 219 | String name = sortModel.getName(); 220 | if (name.indexOf(filterStr.toString()) != -1 221 | || characterParser.getSelling(name).startsWith( 222 | filterStr.toString())) { 223 | filterDateList.add(sortModel); 224 | } 225 | } 226 | } 227 | 228 | // 根据a-z进行排序 229 | Collections.sort(filterDateList, pinyinComparator); 230 | adapter.updateListView(filterDateList); 231 | } 232 | 233 | } 234 | -------------------------------------------------------------------------------- /src/com/finddreams/sortedcontact/PinyinComparator.java: -------------------------------------------------------------------------------- 1 | package com.finddreams.sortedcontact; 2 | 3 | import java.util.Comparator; 4 | 5 | import com.finddreams.sortedcontact.sortlist.SortModel; 6 | 7 | /** 8 | * @Description:拼音的比较器 9 | * @author http://blog.csdn.net/finddreams 10 | */ 11 | public class PinyinComparator implements Comparator { 12 | 13 | public int compare(SortModel o1, SortModel o2) { 14 | if (o1.getSortLetters().equals("@") 15 | || o2.getSortLetters().equals("#")) { 16 | return -1; 17 | } else if (o1.getSortLetters().equals("#") 18 | || o2.getSortLetters().equals("@")) { 19 | return 1; 20 | } else { 21 | return o1.getSortLetters().compareTo(o2.getSortLetters()); 22 | } 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/com/finddreams/sortedcontact/SortAdapter.java: -------------------------------------------------------------------------------- 1 | package com.finddreams.sortedcontact; 2 | 3 | import java.util.List; 4 | 5 | import com.finddreams.sortedcontact.sortlist.SortModel; 6 | 7 | import android.content.Context; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.BaseAdapter; 12 | import android.widget.SectionIndexer; 13 | import android.widget.TextView; 14 | /** 15 | * @Description:用来处理集合中数据的显示与排序 16 | * @author http://blog.csdn.net/finddreams 17 | */ 18 | public class SortAdapter extends BaseAdapter implements SectionIndexer{ 19 | private List list = null; 20 | private Context mContext; 21 | 22 | public SortAdapter(Context mContext, List list) { 23 | this.mContext = mContext; 24 | this.list = list; 25 | } 26 | 27 | /** 28 | * 当ListView数据发生变化时,调用此方法来更新ListView 29 | * @param list 30 | */ 31 | public void updateListView(List list){ 32 | this.list = list; 33 | notifyDataSetChanged(); 34 | } 35 | 36 | public int getCount() { 37 | return this.list.size(); 38 | } 39 | 40 | public Object getItem(int position) { 41 | return list.get(position); 42 | } 43 | 44 | public long getItemId(int position) { 45 | return position; 46 | } 47 | 48 | public View getView(final int position, View view, ViewGroup arg2) { 49 | ViewHolder viewHolder = null; 50 | final SortModel mContent = list.get(position); 51 | if (view == null) { 52 | viewHolder = new ViewHolder(); 53 | view = LayoutInflater.from(mContext).inflate(R.layout.phone_constacts_item, null); 54 | viewHolder.tvTitle = (TextView) view.findViewById(R.id.title); 55 | viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog); 56 | viewHolder.icon = (ImageTextView) view.findViewById(R.id.icon); 57 | view.setTag(viewHolder); 58 | } else { 59 | viewHolder = (ViewHolder) view.getTag(); 60 | } 61 | 62 | //根据position获取分类的首字母的Char ascii值 63 | int section = getSectionForPosition(position); 64 | 65 | //如果当前位置等于该分类首字母的Char的位置 ,则认为是第一次出现 66 | if(position == getPositionForSection(section)){ 67 | viewHolder.tvLetter.setVisibility(View.VISIBLE); 68 | viewHolder.tvLetter.setText(mContent.getSortLetters()); 69 | }else{ 70 | viewHolder.tvLetter.setVisibility(View.GONE); 71 | } 72 | 73 | viewHolder.tvTitle.setText(this.list.get(position).getName()); 74 | viewHolder.icon.setText(this.list.get(position).getName()); 75 | viewHolder.icon.setIconText(mContext,this.list.get(position).getName()); 76 | return view; 77 | 78 | } 79 | 80 | 81 | 82 | final static class ViewHolder { 83 | TextView tvLetter; 84 | TextView tvTitle; 85 | ImageTextView icon; 86 | 87 | } 88 | 89 | 90 | /** 91 | * 根据ListView的当前位置获取分类的首字母的Char ascii值 92 | */ 93 | public int getSectionForPosition(int position) { 94 | return list.get(position).getSortLetters().charAt(0); 95 | } 96 | 97 | /** 98 | * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置 99 | */ 100 | public int getPositionForSection(int section) { 101 | for (int i = 0; i < getCount(); i++) { 102 | String sortStr = list.get(i).getSortLetters(); 103 | char firstChar = sortStr.toUpperCase().charAt(0); 104 | if (firstChar == section) { 105 | return i; 106 | } 107 | } 108 | 109 | return -1; 110 | } 111 | 112 | /** 113 | * 提取英文的首字母,非英文字母用#代替。 114 | * 115 | * @param str 116 | * @return 117 | */ 118 | private String getAlpha(String str) { 119 | String sortStr = str.trim().substring(0, 1).toUpperCase(); 120 | // 正则表达式,判断首字母是否是英文字母 121 | if (sortStr.matches("[A-Z]")) { 122 | return sortStr; 123 | } else { 124 | return "#"; 125 | } 126 | } 127 | 128 | @Override 129 | public Object[] getSections() { 130 | return null; 131 | } 132 | } -------------------------------------------------------------------------------- /src/com/finddreams/sortedcontact/sortlist/CharacterParser.java: -------------------------------------------------------------------------------- 1 | 2 | package com.finddreams.sortedcontact.sortlist; 3 | 4 | /** 5 | * @Description:Java汉字转换为拼音 6 | * @author http://blog.csdn.net/finddreams 7 | */ 8 | public class CharacterParser { 9 | private static int[] pyvalue = new int[] {-20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036, -20032, 10 | -20026, -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, -19746, -19741, -19739, -19728, 11 | -19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275, -19270, -19263, -19261, 12 | -19249, -19243, -19242, -19238, -19235, -19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996, -18977, -18961, 13 | -18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697, -18696, -18526, -18518, -18501, -18490, 14 | -18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183, -18181, -18012, -17997, -17988, 15 | -17970, -17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730, -17721, -17703, -17701, -17697, -17692, 16 | -17683, -17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202, -17185, -16983, -16970, -16942, -16915, -16733, 17 | -16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459, -16452, -16448, -16433, -16429, -16427, -16423, -16419, 18 | -16412, -16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205, -16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959, 19 | -15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659, -15652, -15640, -15631, 20 | -15625, -15454, -15448, -15436, -15435, -15419, -15416, -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362, -15183, -15180, 21 | -15165, -15158, -15153, -15150, -15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117, -15110, -15109, -14941, 22 | -14937, -14933, -14930, -14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889, -14882, -14873, -14871, -14857, 23 | -14678, -14674, -14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399, -14384, -14379, -14368, -14355, -14353, 24 | -14345, -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123, -14122, -14112, -14109, -14099, -14097, -14094, 25 | -14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896, -13894, -13878, -13870, -13859, -13847, -13831, 26 | -13658, -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367, -13359, -13356, -13343, -13340, -13329, 27 | -13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, -12888, -12875, -12871, -12860, 28 | -12858, -12852, -12849, -12838, -12831, -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, -12359, -12346, -12320, -12300, 29 | -12120, -12099, -12089, -12074, -12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604, -11589, -11536, -11358, 30 | -11340, -11339, -11324, -11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024, -11020, -11019, -11018, -11014, 31 | -10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331, -10329, -10328, -10322, -10315, -10309, 32 | -10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254}; 33 | public static String[] pystr = new String[] {"a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi", "bian", 34 | "biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan", "chang", "chao", "che", 35 | "chen", "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong", "cou", "cu", "cuan", 36 | "cui", "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", "diu", "dong", "dou", "du", 37 | "duan", "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan", "gang", 38 | "gao", "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", "guo", "ha", "hai", "han", "hang", 39 | "hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian", 40 | "jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke", "ken", 41 | "keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", "lang", "lao", "le", "lei", "leng", 42 | "li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", "luan", "lue", "lun", "luo", "ma", "mai", 43 | "man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", "ming", "miu", "mo", "mou", "mu", "na", "nai", 44 | "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", "nin", "ning", "niu", "nong", "nu", "nv", "nuan", 45 | "nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", "pie", "pin", "ping", "po", "pu", 46 | "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun", "ran", "rang", "rao", "re", 47 | "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang", "sao", "se", "sen", "seng", "sha", 48 | "shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", "shuang", "shui", "shun", 49 | "shuo", "si", "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao", 50 | "tie", "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu", "xi", 51 | "xia", "xian", "xiang", "xiao", "xie", "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang", "yao", "ye", "yi", 52 | "yin", "ying", "yo", "yong", "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze", "zei", "zen", "zeng", "zha", 53 | "zhai", "zhan", "zhang", "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", "zhua", "zhuai", "zhuan", "zhuang", "zhui", 54 | "zhun", "zhuo", "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo"}; 55 | private StringBuilder buffer; 56 | private String resource; 57 | private static CharacterParser characterParser = new CharacterParser(); 58 | 59 | public static CharacterParser getInstance() { 60 | return characterParser; 61 | } 62 | 63 | public String getResource() { 64 | return resource; 65 | } 66 | 67 | public void setResource(String resource) { 68 | this.resource = resource; 69 | } 70 | 71 | /** * 汉字转成ASCII* * @param chs * @return */ 72 | private int getChsAscii(String chs) { 73 | int asc = 0; 74 | try { 75 | byte[] bytes = chs.getBytes("gb2312"); 76 | if (bytes == null || bytes.length > 2 || bytes.length <= 0) { 77 | throw new RuntimeException("illegal resource string"); 78 | } 79 | if (bytes.length == 1) { 80 | asc = bytes[0]; 81 | } 82 | if (bytes.length == 2) { 83 | int hightByte = 256 + bytes[0]; 84 | int lowByte = 256 + bytes[1]; 85 | asc = (256 * hightByte + lowByte) - 256 * 256; 86 | } 87 | } catch (Exception e) { 88 | System.out.println("ERROR:ChineseSpelling.class-getChsAscii(String chs)" + e); 89 | } 90 | return asc; 91 | } 92 | 93 | /** * 单字解析 * * @param str * @return */ 94 | public String convert(String str) { 95 | String result = null; 96 | int ascii = getChsAscii(str); 97 | if (ascii > 0 && ascii < 160) { 98 | result = String.valueOf((char) ascii); 99 | } else { 100 | for (int i = (pyvalue.length - 1); i >= 0; i--) { 101 | if (pyvalue[i] <= ascii) { 102 | result = pystr[i]; 103 | break; 104 | } 105 | } 106 | } 107 | return result; 108 | } 109 | 110 | /** * 词组解析 * * @param chs * @return */ 111 | public String getSelling(String chs) { 112 | String key, value; 113 | buffer = new StringBuilder(); 114 | for (int i = 0; i < chs.length(); i++) { 115 | key = chs.substring(i, i + 1); 116 | if (key.getBytes().length >= 2) { 117 | value = (String) convert(key); 118 | if (value == null) { 119 | value = "unknown"; 120 | } 121 | } else { 122 | value = key; 123 | } 124 | buffer.append(value); 125 | } 126 | return buffer.toString(); 127 | } 128 | 129 | public String getSpelling() { 130 | return this.getSelling(this.getResource()); 131 | } 132 | 133 | } 134 | -------------------------------------------------------------------------------- /src/com/finddreams/sortedcontact/sortlist/SideBar.java: -------------------------------------------------------------------------------- 1 | package com.finddreams.sortedcontact.sortlist; 2 | 3 | 4 | import com.finddreams.sortedcontact.R; 5 | import com.finddreams.sortedcontact.R.color; 6 | import com.finddreams.sortedcontact.R.drawable; 7 | 8 | import android.annotation.SuppressLint; 9 | import android.content.Context; 10 | import android.graphics.Canvas; 11 | import android.graphics.Paint; 12 | import android.graphics.Typeface; 13 | import android.graphics.drawable.ColorDrawable; 14 | import android.util.AttributeSet; 15 | import android.view.MotionEvent; 16 | import android.view.View; 17 | import android.widget.TextView; 18 | /** 19 | * @Description:右侧的sideBar,显示的是二十六个字母以及*,和#号, 20 | * 点击字母,自动导航到相应拼音的汉字上 21 | * @author http://blog.csdn.net/finddreams 22 | */ 23 | public class SideBar extends View { 24 | // 触摸事件 25 | private OnTouchingLetterChangedListener onTouchingLetterChangedListener; 26 | // 26个字母 27 | public static String[] b = { "☆","A", "B", "C", "D", "E", "F", "G", "H", "I", 28 | "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", 29 | "W", "X", "Y", "Z","#"}; 30 | private int choose = -1;// 选中 31 | private Paint paint = new Paint(); 32 | 33 | private TextView mTextDialog; 34 | 35 | public void setTextView(TextView mTextDialog) { 36 | this.mTextDialog = mTextDialog; 37 | } 38 | public SideBar(Context context, AttributeSet attrs, int defStyle) { 39 | super(context, attrs, defStyle); 40 | } 41 | 42 | public SideBar(Context context, AttributeSet attrs) { 43 | super(context, attrs); 44 | } 45 | 46 | public SideBar(Context context) { 47 | super(context); 48 | } 49 | 50 | /** 51 | * 重写这个方法 52 | */ 53 | protected void onDraw(Canvas canvas) { 54 | super.onDraw(canvas); 55 | // 获取焦点改变背景颜色. 56 | int height = getHeight();// 获取对应高度 57 | int width = getWidth(); // 获取对应宽度 58 | int singleHeight = height / b.length;// 获取每一个字母的高度 59 | 60 | for (int i = 0; i < b.length; i++) { 61 | paint.setColor(getResources().getColor(R.color.gray)); 62 | paint.setTypeface(Typeface.DEFAULT_BOLD); 63 | paint.setAntiAlias(true); 64 | paint.setTextSize(20); 65 | // 选中的状态 66 | if (i == choose) { 67 | paint.setColor(getResources().getColor(R.color.yellow)); 68 | paint.setFakeBoldText(true); 69 | } 70 | // x坐标等于中间-字符串宽度的一半. 71 | float xPos = width / 2 - paint.measureText(b[i]) / 2; 72 | float yPos = singleHeight * i + singleHeight; 73 | canvas.drawText(b[i], xPos, yPos, paint); 74 | paint.reset();// 重置画笔 75 | } 76 | 77 | } 78 | 79 | 80 | @SuppressLint("NewApi") 81 | @Override 82 | public boolean dispatchTouchEvent(MotionEvent event) { 83 | final int action = event.getAction(); 84 | final float y = event.getY();// 点击y坐标 85 | final int oldChoose = choose; 86 | final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener; 87 | final int c = (int) (y / getHeight() * b.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数. 88 | 89 | switch (action) { 90 | case MotionEvent.ACTION_UP: 91 | setBackgroundDrawable(new ColorDrawable(0x00000000)); 92 | choose = -1;// 93 | invalidate(); 94 | if (mTextDialog != null) { 95 | mTextDialog.setVisibility(View.INVISIBLE); 96 | } 97 | break; 98 | 99 | default: 100 | setBackgroundResource(R.drawable.sidebar_background); 101 | setAlpha((float) 0.7); 102 | if (oldChoose != c) { 103 | if (c >= 0 && c < b.length) { 104 | if (listener != null) { 105 | listener.onTouchingLetterChanged(b[c]); 106 | } 107 | if (mTextDialog != null) { 108 | mTextDialog.setText(b[c]); 109 | mTextDialog.setVisibility(View.VISIBLE); 110 | } 111 | 112 | choose = c; 113 | invalidate(); 114 | } 115 | } 116 | 117 | break; 118 | } 119 | return true; 120 | } 121 | 122 | /** 123 | * 向外公开的方法 124 | * 125 | * @param onTouchingLetterChangedListener 126 | */ 127 | public void setOnTouchingLetterChangedListener( 128 | OnTouchingLetterChangedListener onTouchingLetterChangedListener) { 129 | this.onTouchingLetterChangedListener = onTouchingLetterChangedListener; 130 | } 131 | 132 | /** 133 | * 接口 134 | * 135 | * @author coder 136 | * 137 | */ 138 | public interface OnTouchingLetterChangedListener { 139 | public void onTouchingLetterChanged(String s); 140 | } 141 | 142 | } -------------------------------------------------------------------------------- /src/com/finddreams/sortedcontact/sortlist/SortModel.java: -------------------------------------------------------------------------------- 1 | package com.finddreams.sortedcontact.sortlist; 2 | 3 | public class SortModel { 4 | 5 | private String name; //显示的数�? 6 | private String sortLetters; //显示数据拼音的首字母 7 | 8 | public String getName() { 9 | return name; 10 | } 11 | public void setName(String name) { 12 | this.name = name; 13 | } 14 | public String getSortLetters() { 15 | return sortLetters; 16 | } 17 | public void setSortLetters(String sortLetters) { 18 | this.sortLetters = sortLetters; 19 | } 20 | } 21 | --------------------------------------------------------------------------------