Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android Gridview生成程序快捷鍵的復雜方法

android Gridview生成程序快捷鍵的復雜方法

編輯:關於Android編程

 本章跟上篇文章的區別在於我的通訊錄圖標上增加顯示了有多少聯系人。
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;
         
      }
   
    }
作者:guozh

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved