本章跟上篇文章的区别在于我的通讯录图标上增加显示了有多少联系人。
mainpage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
MainPageActivity
package Cloud.Android.ERP;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class MainPageActivity extends InternalBaseActivity {
String[] strTitle={"我的通讯录","我的课表" };
HashMap<String, Object> DrawalleMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
createShutCut();
}
//生成快捷键
void createShutCut()
{
GridView gridview = (GridView) findViewById(R.id.gridview);
DrawalleMap = new HashMap<String, Object>();
DrawalleMap.put("我的通讯录", R.drawable.inco_1 );//添加图像资源的ID
DrawalleMap.put("我的课表", R.drawable.inco_5 );
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new ItemClickListener());
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return strTitle.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ItemViewCache viewCache;
viewCache = new ItemViewCache();
convertView = LayoutInflater.from(MainPageActivity.this)
.inflate(R.layout.icon_item, null);
viewCache.txttasktitle = (TextView) convertView
.findViewById(R.id.ItemText);
viewCache.imageView = (ImageView) convertView
.findViewById(R.id.ItemImage);
viewCache.txttasktitle.setText(strTitle[position]);
if (DrawalleMap.containsKey(strTitle[position].trim()))
{
Object imgPic=DrawalleMap.get(strTitle[position]);
viewCache.imageView.setImageResource((Integer)(imgPic));//DrawalleMap.get(strTitle[position])
}
if (strTitle[position].equals("我的通讯录")&&getTaskCount()>0)
{
Drawable contactIcon=getResources().getDrawable( (Integer)(DrawalleMap.get(strTitle[position])));
Bitmap icon;
if(contactIcon instanceof BitmapDrawable){
icon=((BitmapDrawable)contactIcon).getBitmap();
}else{
icon= getResIcon(getResources(),(Integer)(DrawalleMap.get(strTitle[position])));
}
Bitmap contactCountIcon=generatorContactCountIcon(icon);
viewCache.imageView.setImageBitmap(contactCountIcon);
}
return convertView;
}
// 元素的缓冲类,用于优化ListView
private class ItemViewCache {
public TextView txttasktitle;
public ImageView imageView;
}
}
class ItemClickListener implements OnItemClickListener
{
public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened
View arg1,//The view within the AdapterView that was clicked
int arg2,//The position of the view in the adapter
long arg3//The row id of the item that was clicked
) {
//arg2=arg3
// HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);
//显示所选Item的ItemText
// setTitle((String)item.get("ItemText"));
String title =strTitle[arg2];
switchMoudel(title);
}
//进入指定模块
void switchMoudel(String strTitle)
{
if(strTitle.equalsIgnoreCase("我的课表"))
enterMoudel(MyClassListActivity.class );
if(strTitle.equalsIgnoreCase("我的通讯录"))
enterMoudel( ContactBookListActivity.class );
}
//进入指定模块 strTitle:模块名称,enterActivityName:进入的activityh名称
void enterMoudel(Class<?> enterActivityName )
{
Intent intent=new Intent(MainPageActivity.this,enterActivityName) ;
startActivity(intent);
}
}
/**
* 在给定的图片的右上角加上联系人数量。数量用红色表示
* @param icon 给定的图片
* @return 带联系人数量的图片
*/
/**
* 在给定的图片的右上角加上联系人数量。数量用红色表示
* @param icon 给定的图片
* @return 带联系人数量的图片
*/
public Bitmap generatorContactCountIcon(Bitmap icon){
//初始化画布
//常量,首页快捷键的图标尺寸
int iconSize=65;
Bitmap contactIcon=Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888);
Canvas canvas=new Canvas(contactIcon);
//拷贝图片
Paint iconPaint=new Paint();
iconPaint.setDither(true);//防抖动
iconPaint.setFilterBitmap(true);//用来对Bitmap进行滤波处理,这样,当你选择Drawable时,会有抗锯齿的效果
Rect src=new Rect(0, 0, icon.getWidth(), icon.getHeight());
Rect dst=new Rect(0, 0, iconSize, iconSize);
canvas.drawBitmap(icon, src, dst, iconPaint);
//在图片上创建一个覆盖的联系人个数
int contacyCount=getTaskCount();
//启用抗锯齿和使用设备的文本字距
Paint countPaint=new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DEV_KERN_TEXT_FLAG);
countPaint.setColor(Color.RED);
countPaint.setTextSize(20f);
countPaint.setTypeface(Typeface.DEFAULT_BOLD);
canvas.drawText(String.valueOf(contacyCount), iconSize-15, 15, countPaint);
return contactIcon;
}
int getTaskCount()
{
return 5;
}
}