2013-01-10 47 views
0

我有一個奇怪的問題產生圖像,修改佈局利潤率從這個佈局

我有一個活動,這個活動我有一個佈局內我想讓它的一個形象,以分享社會網絡。 此佈局包含不同的動態圖像和文本。這就是爲什麼我不能將它作爲靜態圖像存儲並按需分享。當用戶觸摸分享按鈕時,我需要生成圖像。

問題是我需要在共享之前調整佈局,我該怎麼做?

ImageView profilPic = (ImageView)dashboardView.findViewById(R.id.profilePic); 
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)profilPic.getLayoutParams(); 
    params.setMargins(10, 62, 0, 0); 
    profilPic.invalidate(); 
    profilPic.requestLayout(); 

首先我修改我的佈局的邊緣,然後我把它

Bitmap bitmap = null; 
     try { 
      bitmap = Bitmap.createBitmap(dashboardView.getWidth(), 
        dashboardView.getHeight(), Bitmap.Config.ARGB_4444); 
      dashboardView.draw(new Canvas(bitmap)); 
     } catch (Exception e) { 
      // Logger.e(e.toString()); 
     } 

     FileOutputStream fileOutputStream = null; 
     File path = Environment 
       .getExternalStorageDirectory(); 
     File file = new File(path, "wayzupDashboard" + ".png"); 
     try { 
      fileOutputStream = new FileOutputStream(file); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); 
     bitmap.compress(CompressFormat.PNG, 100, bos); 
     try { 
      bos.flush(); 
      bos.close(); 
      fileOutputStream.flush(); 
      fileOutputStream.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

圖像最後我分享。

基本上它的工作原理除了它在捕獲佈局之前使用修改後的layoutParams重新繪製佈局。 我需要捕捉此佈局一次,只有當佈局重繪後考慮新的佈局參數時。

如果我刪除捕獲代碼,那麼它工作正常,當我觸摸分享按鈕時,我看到佈局移動。但是當我擁有捕獲代碼時,它只是在修改邊距之前捕獲佈局。

如何確保佈局在捕獲前重繪?

回答

0

爲了記錄,只有在視圖設置完成後才能正確捕獲佈局,我需要先設置視圖,然後在延遲的線程中捕獲佈局。這是我發現使它工作的唯一途徑。

public void onShareButton() { 
     dashboardController.setupViewBeforeSharing(); 

     new Timer().schedule(new TimerTask() {   
      @Override 
      public void run() { 
       Bitmap bitmap = null; 
       try { 
        bitmap = Bitmap.createBitmap(dashboardView.getWidth(), 
          dashboardView.getHeight(), Bitmap.Config.ARGB_4444); 
        dashboardView.draw(new Canvas(bitmap)); 
       } catch (Exception e) { 
        // Logger.e(e.toString()); 
       } 

       FileOutputStream fileOutputStream = null; 
       File path = Environment 
         .getExternalStorageDirectory(); 
       File file = new File(path, "wayzupDashboard" + ".png"); 
       try { 
        fileOutputStream = new FileOutputStream(file); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 
       BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); 
       bitmap.compress(CompressFormat.PNG, 100, bos); 
       try { 
        bos.flush(); 
        bos.close(); 
        fileOutputStream.flush(); 
        fileOutputStream.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       Intent share = new Intent(Intent.ACTION_SEND); 
       share.setType("image/png"); 
       share.putExtra(Intent.EXTRA_TEXT, R.string.addPassengerButton); 
       share.putExtra(Intent.EXTRA_STREAM, 
         Uri.parse("file://" + file.getAbsolutePath())); 
       startActivity(Intent.createChooser(share, "Share image")); 

       MainActivity.this.runOnUiThread(new Runnable() { 
        public void run() { 
         dashboardController.setupViewAfterSharing(); 
        } 
       }); 

      } 
     }, 300); 
    } 
0

更簡單的方法,就是建立用於在dashboardView視圖佈局傳遞的監聽器。
設置聽衆使用View.addOnLayoutChangeListener() - addOnLayoutChangeListener
設置聽衆之前更改佈局參數並將其刪除之後圖像被保存爲持久性。
希望它會增加任何改進。