編輯:關於 Android 程式設計
先前介紹聯絡人(Contact)時,頭像多半是從資料庫取得,方式如下:
cursor = context.getContentResolver().query(ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, photoId), new String[] { Photo.PHOTO }, null, null, null);
其實若在本地通訊錄中為聯絡人加上圖庫或拍照的圖片,系統會另外存一份清晰的原圖。要取得這張原圖,需要 contactId。
在清單中點擊項目時取得 contactId 並傳給詳情頁:
getListView().setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { try { Cursor cursor = (Cursor) getListAdapter().getItem(position); if (cursor == null) { return; } int contactId = cursor.getInt(Personal.ID_COLUMN_INDEX); Intent intent = new Intent(); intent.setClass(ContactsList.this, ContactDetail.class); intent.putExtra("contactId", contactId); startActivity(intent); } catch (Exception ex) { ex.printStackTrace(); } } });
若希望頭像帶圓角,可用 FrameLayout 疊加邊框 ImageView 來實現:
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"> <FrameLayout android:id="@+id/contacts_logo_layout" android:layout_width="@dimen/incall_photo_view_size" android:layout_height="@dimen/incall_photo_view_size"> <ImageView android:id="@+id/contact_photo" android:layout_width="@dimen/incall_photo_view_size" android:layout_height="@dimen/incall_photo_view_size" /> <ImageView android:id="@+id/contacts_logoborder" android:layout_width="@dimen/incall_photo_view_size" android:layout_height="@dimen/incall_photo_view_size" android:background="@drawable/images_box" /> </FrameLayout> </LinearLayout>
Java 程式碼以 AsyncTask 在背景執行緒載入原圖並設定到 ImageView:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.contact_detail); int contactId = getIntent().getIntExtra("contactId", 0); contact_photo = (ImageView) findViewById(R.id.contact_photo); loadTask task = new loadTask(contactId); task.execute(); } private class loadTask extends AsyncTask { public loadTask(int id) { contactId = id; } private int contactId; @Override protected Bitmap doInBackground(Void... params) { InputStream inputStream = openDisplayPhoto(contactId); BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inSampleSize = 1; Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, opt); return bitmap; } @Override protected void onPostExecute(Bitmap result) { if (result != null) { contact_photo.setImageBitmap(result); } super.onPostExecute(result); } } public InputStream openDisplayPhoto(long contactId) { Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); Uri displayPhotoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.DISPLAY_PHOTO); try { AssetFileDescriptor fd = this.getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r"); return fd.createInputStream(); } catch (IOException e) { e.printStackTrace(); return null; } }
其中 openDisplayPhoto 即為取得清晰原圖 inputStream 的關鍵程式碼。成果如下:

Android 定位服務工具類實作:判斷 GPS、網路、WiFi 狀態與跳轉設定頁
定位服務的基本認識 Location Service 能取得移動裝置的所在位置,是所有地圖類應用的基礎。Android 主要提供兩種定位來源: GPS_PROVIDER
Android 餐飲管理 App 實作筆記(二):用 SlidingMenu 搭出側滑菜單
這個專案要做的,是一套跑在 Android 平板或手機上的小型餐飲管理系統:錄入菜品、點餐開單、數據統計,還要能連小米或佳博的藍牙小票機。功能不少,但整個 App 幾乎都
Android 重力感應實作示例:讀取加速度感測器三軸數值
本文以實例講解 Android 中重力感應效果的實現,供大家參考。 Android 上許多遊戲都運用了重力感應技術,於是便深入研究了一下。我們以螢幕的左下方為原點,箭頭指
打造自訂縮放 ImageView(上):Matrix、手勢偵測與自由縮放
看小圖點開大圖、兩指放大、拖到想看的位置——這套操作已經是使用者的肌肉記憶。圖片預覽功能做不好,體驗立刻扣分。要自己實作,得先認識四個東西:Matrix、GestureD