2012-08-15 52 views
1

我需要更改Drawable []的第二層的大小。 我有以下代碼:使用2層從Drawable []更改圖層的大小

hLayout.postDelayed(new Runnable() { 

         DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics(); 
         int width = metrics.widthPixels; 
         int height = metrics.heightPixels; 

         @Override 
         public void run() { 

          if (coupons != null) { 
           int coupSize = coupons.size(); 
           final int itemWidth = (width/3); 
           final int itemHeight = (height/3); 
           hLayout.removeAllViews(); 
           for (int i = 0; i < coupSize; i++) { 
            Coupon coupon = coupons.get(i); 
            if (coupon.getImage() != null) { 
             RelativeLayout parent = new RelativeLayout(getActivity()); 
             RelativeLayout.LayoutParams linearparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
             parent.setLayoutParams(linearparams); 
             final CheckBox cb = new CheckBox(getActivity()); 
             final ImageView iv = new ImageView(getActivity()); 
             // iv.setScaleType(ScaleType.CENTER_INSIDE); 
             LayoutParams checkBoxParams = new LayoutParams(); 
             cb.setId(i); 
             checkBoxParams.height = LayoutParams.WRAP_CONTENT; 
             checkBoxParams.width = LayoutParams.WRAP_CONTENT; 
             cb.setOnCheckedChangeListener(changeListener); 
             RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(itemWidth, itemHeight); 
             imageParams.height = itemHeight; 
             imageParams.width = itemWidth; 
             imageParams.addRule(RelativeLayout.BELOW, cb.getId()); 
             if (cb.getId() == 0) { 
              imageParams.topMargin = 72; 
             } 
             parent.addView(cb, checkBoxParams); 
             parent.addView(iv, imageParams); 
             hLayout.addView(parent); 
             //ImageUtil.loadImage(coupon.getImage(), iv, itemWidth, itemHeight, "", false, getActivity()); 
             Bitmap b =Util.generateEAN("9310779300005", getActivity()); 
             Drawable d = new BitmapDrawable(getResources(),b); 
             System.out.println("------------b : "+b); 
             //iv.setImageBitmap(b); 
             Resources r = getResources(); 
             Drawable[] layers = new Drawable[2]; 
             layers[0] = r.getDrawable(R.drawable.coupon1); 

             layers[1] = d; 
             layers[1].setAlpha(200); 

             LayerDrawable layerDrawable = new LayerDrawable(layers); 
             iv.setImageDrawable(layerDrawable); 
            } 
           } 
          } 

         } 
        }, 200);  

我試圖與層[1] .setBounds(左,上,右,下);但沒有運氣,我不知道還有什麼要嘗試。

回答

-1

我用Canvas類來達到這個目的。請看看here

+0

這實在是一條評論,而不是對問題的回答。請使用「添加評論」爲作者留下反饋。 – Thor 2012-08-17 10:20:47

+1

@Thor這是一個答案,只是不是一個非常有用或詳細的答案 – Craigy 2012-08-17 16:38:00

2

我只是這樣做在我的代碼....這裏我改變了layer[0]的大小。有用 。只需更改layerDrawable.setLayerInsert(layer index,left,top,right,bottom)的值,如:

 Resources r = getResources(); 
     Drawable[] layers = new Drawable[2]; 

     layers[0] = r.getDrawable(R.drawable.gi); 
     layers[1] = r.getDrawable(R.drawable.url); 
     LayerDrawable layerDrawable = new LayerDrawable(layers); 
        // left top right bottom 
     layerDrawable.setLayerInset(0, 600, 600, 600, 600); 

     iv1.setImageDrawable(layerDrawable); 
相關問題