码迷,mamicode.com
首页 > 其他好文 > 详细

day4.29总结_友录_设置圆圈和显示联系人的详细信息

时间:2016-05-13 04:26:44      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

一、显示头像

ContactAdapter

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder vh;

if(convertView==null){

convertView = inflater.inflate(R.layout.item_contact_layout, parent,false);

vh = new ViewHolder();

vh.ivAvatar = (ImageView) convertView.findViewById(R.id.iv_item_contact_avatar);

vh.tvName = (TextView) convertView.findViewById(R.id.tv_item_contact_username);

convertView.setTag(vh);

}else{

vh = (ViewHolder) convertView.getTag();

}

Contact contact = getItem(position);

vh.tvName.setText(contact.getName());

//根据contact对象的photo_id属性,去获得头像图片

if(position==0){

//如果是第一个联系人,头像用"加号"

vh.ivAvatar.setImageResource(R.drawable.ic_add_contact);

}else{

vh.ivAvatar.setImageBitmap(biz.getAvatar(contact.getPhoto_id()));

}

return convertView;

}

二、设置圆圈

ContactBiz

/**

 * 业务类

 * 关注所有项目中与“联系人”相关的内容

 * @author pjy

 *

 */

public class ContactBiz {

private Context context;

public ContactBiz(Context context){  传一个上下文,后面会用到

this.context = context;

}

/**

 * 获取设备上所有的联系人信息

 * 并且按照姓名排序

 *

 * @return

 */

public List<Contact> getAllContacts(){

List<Contact> list = YouluUtil.getAllContacts(context);

Collections.sort(list,new Comparator<Contact>() {根据名字排序

 

@Override

public int compare(Contact lhs, Contact rhs) {

return lhs.getName().toUpperCase().compareTo(rhs.getName().toUpperCase());

}

});

Contact contact = new Contact();

contact.setName("添加联系人");    添加符号也要给一个建一个对象

list.add(0,contact);

return list;

}

/**

 * 获得头像图片

 * @param photoId

 * @return

 */

public Bitmap getAvatar(int photoId){

return YouluUtil.getAvatar(context,photoId);

}

 

public void showDetail(Contact contact) {

YouluUtil.showDetail(context,contact);

}

}

YouluUtil

public static Bitmap getAvatar(Context context, int photoId) {获取我的头像

Bitmap bitmap = null;

if(photoId==0){

//没有为联系人设置头像

//手动指定一个头像

//bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);

bitmap = getMyAvatar(context);

}else{

//有头像,DATA数据表中的data15

ContentResolver cr = context.getContentResolver();

Cursor cursor = cr.query(Data.CONTENT_URI,

new String[]{Data.DATA15}, 根据Id查头像

Data._ID + " = ?",

new String[]{String.valueOf(photoId)},

null);

cursor.moveToNext();//指向第一条数据

byte[] bytes = cursor.getBlob(0);

//方形图

Bitmap avatar = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

cursor.close();

bitmap = getCircleAvatar(context,avatar);

}

return bitmap;

}

private static Bitmap getCircleAvatar(Context context, Bitmap avatar) {

Bitmap bitmap = Bitmap.createBitmap(avatar.getWidth(), avatar.getHeight(), Bitmap.Config.ARGB_8888); 位图位数越高代表其可以存储的颜色信息越多,当然图像也就越逼真

Canvas canvas = new Canvas(bitmap);

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

paint.setColor(Color.BLACK);

float radius = Math.min(avatar.getWidth(), avatar.getHeight())/2;获取半径

//混合模式中的DST

canvas.drawCircle(avatar.getWidth()/2, avatar.getHeight()/2, radius , paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));设置前景后景模式

//混合模式中的SRC

canvas.drawBitmap(avatar, 0, 0, paint);

//画白边

paint.setColor(Color.WHITE);

paint.setStyle(Paint.Style.STROKE);

float strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, context.getResources().getDisplayMetrics());屏幕的密度,类似分辨率

paint.setStrokeWidth(strokeWidth);

canvas.drawCircle(avatar.getWidth()/2, avatar.getHeight()/2, radius-strokeWidth/2, paint);

return bitmap;

}

 

/**

 * 手动绘制一个头像出来

 * 深灰色背景,前景白色文字“友录”

 * @return

 */

private static Bitmap getMyAvatar(Context context) {

Bitmap bitmap = Bitmap.createBitmap(150, 150, Bitmap.Config.ARGB_8888);

//绘制图形需要2个对象

//画布Canvas

Canvas canvas = new Canvas(bitmap);

//画笔Paint

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

paint.setColor(Color.DKGRAY);

paint.setStyle(Style.FILL);

canvas.drawCircle(75, 75, 75, paint);

paint.setColor(Color.WHITE);

//sp--屏幕的密度-->px

int sp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, context.getResources().getDisplayMetrics());

paint.setTextSize(sp);

Rect bounds = new Rect();

paint.getTextBounds("友录", 0, 2, bounds );

float x = 75 - bounds.width()/2;

float y = 75 + bounds.height()/2;

canvas.drawText("友录", x, y, paint);

//画白边

paint.setStyle(Paint.Style.STROKE);

float strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, context.getResources().getDisplayMetrics());

paint.setStrokeWidth(strokeWidth);

canvas.drawCircle(75, 75, 75-strokeWidth/2, paint);

return bitmap;

}

}

 

 

 

三、显示联系人的详细信息

YouluUtil

public static void showDetail(final Context context, final Contact contact) {

AlertDialog.Builder builder = new AlertDialog.Builder(context);

//标准的AlertDialog.Builder的写法

//buidler.setIcon/setTitle/setMessage/setPositiveButton/setNegativieButton

//buidler.create().show();

final AlertDialog dialog = builder.create();

dialog.show();

Window window = dialog.getWindow();

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);找到系统的inflater

View view = inflater.inflate(R.layout.detail_layout, null);

window.setContentView(view);

ImageView ivAvatar = (ImageView) view.findViewById(R.id.iv_detail_avatar);

TextView tvName = (TextView) view.findViewById(R.id.tv_detail_name);

TextView tvNumber = (TextView) view.findViewById(R.id.tv_detail_number);

ivAvatar.setImageBitmap(getAvatar(context, contact.getPhoto_id()));

tvName.setText(contact.getName());

tvNumber.setText(contact.getNumber());

ImageView ivEdit = (ImageView) view.findViewById(R.id.iv_detail_edit);

ImageView ivClose = (ImageView) view.findViewById(R.id.iv_detail_close);

ivClose.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

dialog.dismiss();

}

});

ivEdit.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

//发送一个隐式intent,打开系统的联系人界面编辑contact的信息

Intent intent = new Intent(Intent.ACTION_EDIT);

Uri data = ContactsContract.Contacts.getLookupUri(contact.get_id(), contact.getLookupKey());

intent.setDataAndType(data , ContactsContract.Contacts.CONTENT_ITEM_TYPE);

intent.putExtra("finishActivityOnSaveCompleted", true);

context.startActivity(intent);

dialog.dismiss();

}

});

}

 

 

 

 

 

 

 

day4.29总结_友录_设置圆圈和显示联系人的详细信息

标签:

原文地址:http://blog.csdn.net/gsd1602_yingmu/article/details/51336193

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!