【Android源码 栏目提醒】:网学会员--在 Android源码 编辑为广大网友搜集整理了:ContactPhotoBinder java android源码 - 开发文档绩等信息,祝愿广大网友取得需要的信息,参考学习。
/ Copyright C 2010 The Android Open Source Project Licensed under the Apache License Version 2.0 the License you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing software distributed under the License is distributed on an AS IS BASIS WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied. See the License for the specific language governing per
missions and limitations under the License. / package com.example.android.xmladapters import android.content.ContentUris import android.content.Context import android.content.res.Resources import android.database.Cursor import android.graphics.Bitmap import
android.graphics.BitmapFactory import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.Drawable import android.net.Uri import android.provider.ContactsContract import android.view.View import android.widget.TextView import java.io.InputStream import java.util.HashMap / This custom cursor binder is used by the adapter defined in res/
xml to bind contacts photos to their respective list item. This binder simply queries a contacts photo based on the contacts id and sets the photo as a compound drawable on the TextView used to display the contacts name. / public class ContactPhotoBinder extends Adapters.CursorBinder private static final int PHOTO_SIZE_DIP 54 private final Drawable mDefault private final HashMap mCache private final Resources mResources private final int mPhotoSize public ContactPhotoBinderContext context Adapters.CursorTransformation transformation supercontext transformation mResources mContext.getResources // Default picture used when a contact does not provide one mDefault mResources.getDrawableR.drawable.ic_contact_picture // Cache used to avoid re-querying contacts photos every time mCache new HashMap // Compute the size of the photo based on the displays density mPhotoSize int PHOTO_SIZE_DIP mResources.getDisplayMetrics.density 0.5f Override public boolean bindView view Cursor cursor int columnIndex final long id cursor.getLongcolumnIndex // First check whether we have already cached the contacts
photo Drawable d mCache.getid if d null // If the photo wasnt in the cache ask the contacts provider for // an input stream we can use to load the photo Uri uri ContentUris.withAppendedIdContactsContract.Contacts.CONTENT_URI id InputStream stream ContactsContract.Contacts.openContactPhotoInputStream mContext.getContentResolver uri // Creates the drawable for the contacts photo or use our fallback drawable if stream null // decoding the bitmap could be done in a worker thread too. Bitmap bitmap BitmapFactory.decodeStreamstream d new BitmapDrawablemResources bitmap else d mDefault d.setBounds0 0 mPhotoSize mPhotoSize TextView view.setCompoundDrawablesd null null null // Remember the photo associated with this contact mCache.putid d else TextView view.setCompoundDrawablesd null null null return true