總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年3月14日 星期三

查詢&開啟應用程式列表 - getInstalledApplications & getLaunchIntentForPackage (二)

在此範例加入AP icon的資訊, 會讓user更容易辨識要尋找的AP


新增
./res/layout/layout_gv_item.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" >

    <ImageView
        android:id="@+id/adapter_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
  
    <TextView
        android:id="@+id/adapter_tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

因GridView中不只包含一個TextView資訊←可以用android.R.layout.simple_list_item_1
而是每個Item有TextView及ImageView←故建立一個類別extends BaseAdapter
./src/com.tsots.allap/GVAdapter.java
package com.tsots.allap;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GVAdapter extends BaseAdapter 
{
    LayoutInflater mInflater;
    ArrayList<String> adapter_apName;
    ArrayList<Bitmap> adapter_apIcon;

    public GVAdapter(Context context, ArrayList<String> list_apName, ArrayList<Bitmap> list_apIcon)
    {
        mInflater = LayoutInflater.from(context);
        adapter_apName = list_apName;
        adapter_apIcon = list_apIcon;
    }

    @Override
    public int getCount() {
        return adapter_apIcon.size();
    }

    @Override
    public Object getItem(int position) {
        return adapter_apIcon.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       
        convertView = mInflater.inflate(R.layout.layout_gv_item,null);
        ImageView adapter_iv = (ImageView) convertView.findViewById (R.id.adapter_iv);
        TextView adapter_tv = (TextView) convertView.findViewById (R.id.adapter_tv);
        adapter_iv.setImageBitmap(adapter_apIcon.get(position));
        adapter_tv.setText(adapter_apName.get(position).toString());
        return convertView;
    }
}

主程式部份則多抓一個ApplicationInfo.loadIcon(packageManager)=AP icon的值
./src/com.tsots.allap/Activity_ALLAP.java
GVAdapter gvadapter;
gvadapter = new GVAdapter(context, list_apName, list_apIcon);
GridView gridView1;
gridView1 = (GridView) findViewById (R.id.gridView1);
PackageManager packageManager = context.getPackageManager();
//取得Device中包括系統預設與手動安裝的所有AP
List<ApplicationInfo> allappInfo = packageManager.getInstalledApplications(PackageManager.GET_ACTIVITIES);
for(int i = 0 ; i <allappInfo.size() ; i ++)
{
            ApplicationInfo appAI = allappInfo.get(i);
            //取得Device中可以Launch的AP
            Intent intent = packageManager.getLaunchIntentForPackage(appAI.packageName);
            if (intent != null)
            {
                String finalAPName = appAI.loadLabel(packageManager).toString();
                Drawable drawableAPIcon = appAI.loadIcon(packageManager);   
                // ApplicationInfo取得的icon為Drawable, 需轉成Bitmap            
                Bitmap bitmapAPIcon = DrawableToBitmap(drawableAPIcon);
                // 調整icon為相同pixel
                Bitmap finalAPicon = fixSize(bitmapAPIcon);
                list_apName.add(finalAPName);
                list_apIcon.add(finalAPicon);
            }
}

gvadapter = new GVAdapter(context, list_apName, list_apIcon);
gridView1.setAdapter(gvadapter);

public Bitmap DrawableToBitmap(Drawable drawableAPIcon)
{
            BitmapDrawable  bd = (BitmapDrawable)drawableAPIcon;
            Bitmap bitmapAPIcon = bd.getBitmap();

        return bitmapAPIcon;
}

public Bitmap fixSize(Bitmap bitmapAPIcon)
{
        int iconWidth = 0;
        int iconHeight = 0;
        int newIconWidth = 100;
        int newIconHeight = 100;
        iconWidth = bitmapAPIcon.getWidth();
        iconHeight = bitmapAPIcon.getHeight();
        float scaleIconWidth = ((float) newIconWidth) / iconWidth;
        float scaleIconHeight = ((float) newIconHeight) / iconHeight;
        Matrix iconMatrix = new Matrix();
        iconMatrix.postScale(scaleIconWidth, scaleIconHeight);
        Bitmap finalAPicon = Bitmap.createBitmap(bitmapAPIcon, 0, 0,iconWidth, iconHeight, iconMatrix, true);
        return finalAPicon;
}

沒有留言:

張貼留言