复选框的onClick方法将图标转换为位图

时间:2013-12-14 02:13:44

标签: android android-bitmap

我有一个用户安装的应用程序的listView。在每个列表项中,它具有应用程序名称,包名称,图标和末尾的复选框。选中该复选框后,我希望它从该listView项目中获取应用程序图标(即,如果选中“新闻”的应用程序,它将采用“新闻”应用程序的图标),然后将其转换为位图。

请注意我正在加载必要的信息:

/ load controls from layout resources
    ImageView ivAppIcon = (ImageView) v.findViewById(R.id.ivIcon);
    TextView tvAppName = (TextView) v.findViewById(R.id.tvName);
    TextView tvPkgName = (TextView) v.findViewById(R.id.tvPack);
    final CheckBox addCheckbox = (CheckBox) v
            .findViewById(R.id.addCheckbox);
    Log.d("AppInfoAdapter", "Controls from layout Resources Loaded");

    // set data to display
    ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
    tvAppName.setText(entry.activityInfo.loadLabel(mPackManager));
    tvPkgName.setText(entry.activityInfo.packageName);

    Log.d("AppInfoAdapter", "Data Set To Display");

我已经尝试过这种方式,但我怀疑它的运行方式与我认为不同,所以我想要第二种意见。以下是我目前正在做的事情:

addCheckbox
            .setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (addCheckbox.isChecked()) {
                        System.out.println("Checked");
                        PackageManager pm = mContext.getPackageManager();
                        Drawable icon = null;
                        try {
                            icon = pm
                            .getApplicationIcon(entry.activityInfo.packageName);
                        } catch (NameNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        Drawable default_icon = pm.getDefaultActivityIcon();
                        if (icon instanceof BitmapDrawable
                                && default_icon instanceof BitmapDrawable) {
                            BitmapDrawable icon_bd = (BitmapDrawable) icon;
                            Bitmap icon_b = icon_bd.getBitmap();
                            BitmapDrawable default_bd = (BitmapDrawable) pm
                                    .getDefaultActivityIcon();
                            Bitmap default_b = default_bd.getBitmap();
                            if (icon_b == default_b) {
                                // It's the default icon
                                scaleDownBitmap(default_b, 100, v.getContext());
                                Log.d("AppInfoAdapter", "Scale Bitmap Chosen");

但在考虑之后,我可以这样做吗?:

if (addCheckbox.isChecked()) {
                        System.out.println("Checked");
                        ImageView imgV = (ImageView) findViewById(R.id.ivIcon);
                        imgV.buildDrawingCache();
                        Bitmap default_b = imgV.getDrawingCache();
                                // It's the default icon
                                scaleDownBitmap(default_b, 100, v.getContext());
                                Log.d("AppInfoAdapter", "Scale Bitmap Chosen");

甚至只是这样的事情:

if (addCheckbox.isChecked()) {
                        System.out.println("Checked");
                        Bitmap default_b = ((BitmapDrawable)ivAppIcon.getDrawable()).getBitmap();
                                // It's the default icon
                                scaleDownBitmap(default_b, 100, v.getContext());
                                Log.d("AppInfoAdapter", "Scale Bitmap Chosen");

我需要就如何做到这一点提出第二意见。请帮忙!

1 个答案:

答案 0 :(得分:0)

我相信您在使用Drawable进行位图转换时遇到了问题?我不能(还)发表评论并问你这个问题。因此,我会冒一个完整的答案:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final int   DEST_IMAGE_WIDH = 100;
    final int DEST_IMAGE_HEIGHT = 100;
    PackageManager           pm = getPackageManager();
    ApplicationInfo     appInfo = getApplicationInfo();
    Drawable            appIcon = pm.getApplicationIcon(appInfo);
    Bitmap              appBmp  = Bitmap.createBitmap(
              DEST_IMAGE_WIDH
            , DEST_IMAGE_HEIGHT
            , Config.ARGB_8888
            ); 
    // Creates a new canvas based on the image specifications
    // create just above.
    Canvas canvas               = new Canvas(appBmp);
    // (optional) Fills the entire canvas
    canvas.drawColor(Color.WHITE);
    // You need to set bounds otherwise a 0,0 sized image would be draw.
    appIcon.setBounds(0, 0, DEST_IMAGE_WIDH, DEST_IMAGE_HEIGHT );
    appIcon.draw(canvas);

    /// Let's save to a .jpg file ...
    File file = new File(getFilesDir().getAbsolutePath() + "/test2.jpg");
    FileOutputStream out;
    try
    {
        file.createNewFile();
        out = new FileOutputStream(file);
        appBmp.compress(Bitmap.CompressFormat.JPEG, 80, out);
        out.close();

        // Load back the image file to confirms it works
        Bitmap bitmap = BitmapFactory.decodeFile( file.getAbsolutePath() );
        ImageView imageView1 = (ImageView)findViewById(R.id.imageView1);
        imageView1.setImageBitmap( bitmap );            
    }
    catch (FileNotFoundException e1)
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    catch (IOException e2 ) 
    {
        // TODO Auto-generated catch block
        e2.printStackTrace();           
    }
}