編輯:關於Android編程
/** 聯系人名稱 **/ private ArrayListmContacts = new ArrayList (); private static final String[] PHONES_PROJECTION = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER, Phone.PHOTO_ID, Phone.CONTACT_ID }; /** 聯系人顯示名稱 **/ private static final int PHONES_DISPLAY_NAME_INDEX = 0; /** 電話號碼 **/ private static final int PHONES_NUMBER_INDEX = 1; /** 頭像ID **/ private static final int PHONES_PHOTO_ID_INDEX = 2; /** 聯系人的ID **/ private static final int PHONES_CONTACT_ID_INDEX = 3; private void getPhoneContacts() { ContentResolver resolver = getContentResolver(); try { // 獲取手機聯系人 Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null); if (phoneCursor != null) { while (phoneCursor.moveToNext()) { // 得到手機號碼 String phoneNumber = phoneCursor .getString(PHONES_NUMBER_INDEX); // 當手機號碼為空的或者為空字段 跳過當前循環 if (TextUtils.isEmpty(phoneNumber)) continue; // 得到聯系人名稱 String contactName = phoneCursor .getString(PHONES_DISPLAY_NAME_INDEX); // 得到聯系人ID Long contactid = phoneCursor .getLong(PHONES_CONTACT_ID_INDEX); // 得到聯系人頭像ID Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX); // 得到聯系人頭像Bitamp Bitmap contactPhoto = null; // photoid 大於0 表示聯系人有頭像 如果沒有給此人設置頭像則給他一個默認的 if (photoid > 0) { Uri uri = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, contactid); InputStream input = ContactsContract.Contacts .openContactPhotoInputStream(resolver, uri); contactPhoto = BitmapFactory.decodeStream(input); } else { contactPhoto = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher); } ContactEntity mContact = new ContactEntity(contactName, phoneNumber, contactPhoto); mContacts.add(mContact); } phoneCursor.close(); } } catch (Exception e) { e.printStackTrace(); } }
package com.yqy.yqy_testtxl; import android.graphics.Bitmap; /** * 聯系人信息 * @author YQY * */ public class ContactEntity { /** 聯系人名稱 **/ private String name; /** 聯系人號碼 **/ private String number; /** 聯系人頭像 **/ private Bitmap photo; @Override public String toString() { return ContactEntity [name= + name + , number= + number + , photo= + photo + ]; } public ContactEntity(String name, String number, Bitmap photo) { super(); this.name = name; this.number = number; this.photo = photo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Bitmap getPhoto() { return photo; } public void setPhoto(Bitmap photo) { this.photo = photo; } }
private void initList() { ListView lv = (ListView) findViewById(R.id.listView1); lv.setAdapter(new MyAdapter()); } class MyAdapter extends BaseAdapter { @Override public int getCount() { if (mContacts != null && mContacts.size() > 0) { return mContacts.size(); } else { return 0; } } @Override public Object getItem(int position) { if (mContacts != null && mContacts.size() > 0) { return mContacts.get(position); } else { return null; } } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(MainActivity.this).inflate( R.layout.item_contact, null); holder.name = (TextView) convertView.findViewById(R.id.tv_name); holder.number = (TextView) convertView .findViewById(R.id.tv_number); holder.photo = (ImageView) convertView .findViewById(R.id.iv_photo); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } ContactEntity contact = mContacts.get(position); holder.name.setText(contact.getName()+); holder.number.setText(contact.getNumber()+); holder.photo.setImageBitmap(contact.getPhoto()); return convertView; } class ViewHolder { TextView name; TextView number; ImageView photo; } }
package com.yqy.yqy_testtxl; import java.io.InputStream; import java.util.ArrayList; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentUris; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getPhoneContacts(); Log.e(YQY, mContacts.size() + -------------mContacts---------); initList(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } /** 聯系人名稱 **/ private ArrayListmContacts = new ArrayList (); private static final String[] PHONES_PROJECTION = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER, Phone.PHOTO_ID, Phone.CONTACT_ID }; /** 聯系人顯示名稱 **/ private static final int PHONES_DISPLAY_NAME_INDEX = 0; /** 電話號碼 **/ private static final int PHONES_NUMBER_INDEX = 1; /** 頭像ID **/ private static final int PHONES_PHOTO_ID_INDEX = 2; /** 聯系人的ID **/ private static final int PHONES_CONTACT_ID_INDEX = 3; private void getPhoneContacts() { ContentResolver resolver = getContentResolver(); try { // 獲取手機聯系人 Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null); if (phoneCursor != null) { while (phoneCursor.moveToNext()) { // 得到手機號碼 String phoneNumber = phoneCursor .getString(PHONES_NUMBER_INDEX); // 當手機號碼為空的或者為空字段 跳過當前循環 if (TextUtils.isEmpty(phoneNumber)) continue; // 得到聯系人名稱 String contactName = phoneCursor .getString(PHONES_DISPLAY_NAME_INDEX); // 得到聯系人ID Long contactid = phoneCursor .getLong(PHONES_CONTACT_ID_INDEX); // 得到聯系人頭像ID Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX); // 得到聯系人頭像Bitamp Bitmap contactPhoto = null; // photoid 大於0 表示聯系人有頭像 如果沒有給此人設置頭像則給他一個默認的 if (photoid > 0) { Uri uri = ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, contactid); InputStream input = ContactsContract.Contacts .openContactPhotoInputStream(resolver, uri); contactPhoto = BitmapFactory.decodeStream(input); } else { contactPhoto = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher); } ContactEntity mContact = new ContactEntity(contactName, phoneNumber, contactPhoto); mContacts.add(mContact); } phoneCursor.close(); } } catch (Exception e) { e.printStackTrace(); } } private void initList() { ListView lv = (ListView) findViewById(R.id.listView1); lv.setAdapter(new MyAdapter()); } class MyAdapter extends BaseAdapter { @Override public int getCount() { if (mContacts != null && mContacts.size() > 0) { return mContacts.size(); } else { return 0; } } @Override public Object getItem(int position) { if (mContacts != null && mContacts.size() > 0) { return mContacts.get(position); } else { return null; } } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(MainActivity.this).inflate( R.layout.item_contact, null); holder.name = (TextView) convertView.findViewById(R.id.tv_name); holder.number = (TextView) convertView .findViewById(R.id.tv_number); holder.photo = (ImageView) convertView .findViewById(R.id.iv_photo); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } ContactEntity contact = mContacts.get(position); holder.name.setText(contact.getName()+); holder.number.setText(contact.getNumber()+); holder.photo.setImageBitmap(contact.getPhoto()); return convertView; } class ViewHolder { TextView name; TextView number; ImageView photo; } } }
說到圓角濾鏡(效果)很多人會想到app的圖標,沒錯,就是圖標。圓角化的圖片用來做圖標很美觀,這是事實。國人喜愛的iPhone的應用圖標采用的就是圓角化,很多Android
前言相信大家都知道知道,在AndroidOS中,提供了五中數據存儲方式,分別是:ContentProvider存儲、文件存儲、SharedPreference存儲、SQL
屬性動畫---res/animator屬性動畫故名思議就是通過動畫的方式改變對象的屬性了,我們首先需要了解幾個屬性:Duration動畫的持續時間,默認300ms。Tim
AlertDialog生成的對話框可分為4個區域:圖標區,標題區,內容區,按鈕區結構如圖:AlertDialog對話框的使用:1,創建AlertDialog.Builde