【问题标题】:Change size of a layer from Drawable[] with 2 layers使用 2 层从 Drawable[] 更改层的大小
【发布时间】:2012-08-15 11:37:07
【问题描述】:

我需要更改 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);    

我尝试使用 layers[1].setBounds(left, top, right, bottom);但没有运气,我不知道还能尝试什么。

【问题讨论】:

    标签: android layout resources drawable layer


    【解决方案1】:

    我只是在我的代码中执行此操作....这里我更改了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);
    

    【讨论】:

      【解决方案2】:

      我使用Canvas 类来实现这个目的。请看here

      【讨论】:

      • 这真的是评论,而不是问题的答案。请使用“添加评论”为作者留下反馈。
      • @Thor 这是一个答案,只是不是很有帮助或详细的答案
      猜你喜欢
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      相关资源
      最近更新 更多