有时需对已安装的应用进行管理 , 这里我简单的写了获取应用程序列表。

代码如下:

package com.lml.applicationtest;

import java.util.List;

import android.app.Activity;

import android.content.Context;

import android.content.pm.PackageInfo;

import android.content.pm.PackageManager;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Matrix;

import android.graphics.PixelFormat;

import android.graphics.drawable.BitmapDrawable;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

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 {

private ListView lv;

private List pakageinfos;

private PackageManager pm;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

lv = (ListView) findViewById(R.id.list);

makeList();

}

private void makeList() {

pm = getPackageManager();

pakageinfos = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);

lv.setAdapter(new MyAdapter(this));

}

private class MyAdapter extends BaseAdapter {

private Context context;// 用于接收传递过来的Context对象

public MyAdapter(Context context) {

this.context = context;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return pakageinfos.size();

}

@Override

public Object getItem(int arg0) {

// TODO Auto-generated method stub

return arg0;

}

@Override

public long getItemId(int arg0) {

// TODO Auto-generated method stub

return arg0;

}

@Override

public View getView(int arg0, View arg1, ViewGroup arg2) {

// TODO Auto-generated method stub

PackageInfo info = pakageinfos.get(arg0);

View view = View.inflate(context, R.layout.item, null);

Drawable drawable=info.applicationInfo.loadIcon(pm);

((ImageView) view.findViewById(R.id.iv)).setImageDrawable(zoomDrawable(drawable,50,50));

((TextView) view.findViewById(R.id.tv)).setText(info.applicationInfo.loadLabel(pm));

return view;

}

}

private Bitmap drawableToBitmap(Drawable drawable)// drawable 转换成bitmap

{

int width = drawable.getIntrinsicWidth(); // 取drawable的长宽

int height = drawable.getIntrinsicHeight();

Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

: Bitmap.Config.RGB_565; // 取drawable的颜色格式

Bitmap bitmap = Bitmap.createBitmap(width, height, config); // 建立对应bitmap

Canvas canvas = new Canvas(bitmap); // 建立对应bitmap的画布

drawable.setBounds(0, 0, width, height);

drawable.draw(canvas); // 把drawable内容画到画布中

return bitmap;

}

private Drawable zoomDrawable(Drawable drawable, int w, int h)

{

int width = drawable.getIntrinsicWidth();

int height= drawable.getIntrinsicHeight();

Bitmap oldbmp = drawableToBitmap(drawable);// drawable转换成bitmap

Matrix matrix = new Matrix();   // 创建操作图片用的Matrix对象

float scaleWidth = ((float)w / width);   // 计算缩放比例

float scaleHeight = ((float)h / height);

matrix.postScale(scaleWidth, scaleHeight);         // 设置缩放比例

Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);       // 建立新的bitmap,其内容是对原bitmap的缩放后的图

return new BitmapDrawable(getResources() ,newbmp);       // 把bitmap转换成drawable并返回

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐