使用2层从Drawable []更改图层的大小

时间:2012-08-15 11:37:07

标签: android layout resources drawable layer

我需要更改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(左,上,右,下);但没有运气,我不知道还有什么可以尝试。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:-2)

我使用Canvas类来实现此目的。请看一下here