2013-12-14 49 views
0

我有一個listView用戶安裝的應用程序。在每個列表項中,它的末尾都有應用程序名稱,軟件包名稱,圖標和複選框。當選中該複選框時,我希望它從該listView項目中獲取應用程序圖標(即,如果選中「News」應用程序,它將採用「News」應用程序的圖標),然後將其轉換爲位圖。onClick方法的複選框將圖標變成位圖

請注意,我加載像這樣必要的信息:

/ 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"); 

我需要關於如何做到這一點的第二個意見。請幫忙!

回答

0

我相信你在繪製到位圖轉換時遇到問題?我不能(還)發表評論並問你這個問題。因此,我會冒全部答案:

@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();   
    } 
} 
+0

是的,那是我遇到的問題(很好......它的一部分)。但是,我的確完全贊同完整的答案,因爲這幫助我完成了我完全想做的事情(唯一的區別是我從另一個類的流中獲取位圖)。 我將無法在明天之前對此進行測試,所以我會隨時更新。 再次感謝! :) – user2909006