动态添加自定义ImageView未显示在RelativeLayout中

时间:2018-06-15 15:59:01

标签: java android

我在这里通过此功能使用JSON创建自定义TextViewTextView正确显示)和ImageView,但ImageView未显示在相对布局中。帮我解决这个问题。 BubbleTextView是我的自定义ImageView,其StickerView属性

public RelativeLayout getViewByJson(final Context context, RelativeLayout viewGroup, String jsonArray) {
        try {
            JSONArray array = new JSONArray(jsonArray);
            JSONObject jObjMain = array.getJSONObject(0);
            JSONObject jObj = jObjMain.getJSONObject("canvas");
            int canvasWidth = jObj.optInt("width");
            int canvasHeight = jObj.optInt("height");
            String canvasBackgroundPath = jObj.optString("background_path");
            String canvasFramePath = jObj.optString("frame_path");
            int canvasFilterAlpha = jObj.optInt("filter_alpha");
            boolean canvasFilterEnabled = jObj.optBoolean("filter_enabled");
            boolean canvasBackgroundBlured = jObj.optBoolean("background_blur");
            JSONArray jsonArrayComponents = jObjMain.optJSONArray("components");
            if (jsonArrayComponents.length() != 0 && jsonArrayComponents != null) {
                for (int i = 0; i < jsonArrayComponents.length(); i++) {
                    JSONObject object = jsonArrayComponents.getJSONObject(i);
                    String componentType = object.getString("type");
                    if (componentType.equals("text_view")) {
                        int componentWidth = object.optInt("width");
                        int componentHeight = object.optInt("height");
                        int componentPositionX = object.optInt("position_x");
                        int componentPositionY = object.optInt("position_y");
                        int componentRotationAngle = object.optInt("rotation_angle");
                        int componentOpacity = object.optInt("opacity");
                        String componentViewState = object.optString("view_state");
                        String componentText = object.optString("text");
                        String componentTextAlign = object.optString("text_align");
                        int componentTextSize = object.optInt("text_size");
                        String componentTextColor = object.optString("text_color");
                        int componentLineSpacing = object.optInt("line_spacing");
                        String componentTypeFacePath = object.optString("typeface_path");
                        int componentReflection = object.optInt("reflection");
                        boolean componentShadowEnabled = object.optBoolean("shadow_enabled");
                        int componentShadowColor = object.optInt("shadow_color");
                        int componentShadowRadius = object.optInt("shadow_radius");
                        int componentShadowDx = object.optInt("shadow_dx");
                        int componentShadowDy = object.optInt("shadow_dy");
                        int componentGradientColorOne = object.optInt("gradient_color_one");
                        int componentGradientColorTwo = object.optInt("gradient_color_two");
                        String componentGradientType = object.optString("gradient_type");
                        String componentPatternBitmapPath = object.optString("pattern_bitmap_path");
                        String componentColorType = object.optString("color_type");
                        boolean componentHighlightEnabled = object.optBoolean("highlight_enabled");
                        int componentHighlightColor = object.optInt("highlight_color");
                        int componentHighlightAlpha = object.optInt("highlight_alpha");
                        boolean componentStrokeEnabled = object.optBoolean("stroke_enabled");
                        int componentStrokeColor = object.optInt("stroke_color");
                        int componentStrokeWidth = object.optInt("stroke_width");
                        boolean componentUnderline = object.optBoolean("underline");
                        boolean componentStrikethrough = object.optBoolean("strike_thought");
                        boolean componentBold = object.optBoolean("bold");

                        //Custom TextView
                        StickerTextView stv = new StickerTextView(context);
                        stv.setText(componentText);
                        stv.setId(i);
                        stv.setTextViewWidth(componentWidth);
                        stv.setTextViewHeight(componentHeight);
                        stv.setTextSize(componentTextSize);
                        stv.setTextPosotion(componentPositionX, componentPositionY);
                        if (componentViewState.equals("false")) {
                            stv.setBorderVisiblity(false);
                        }
                        viewGroup.addView(stv);
                        final int finalI1 = i;
                        stv.setOperationListener(new StickerView.OperationListener() {
                            @Override
                            public void onClick() {

                            }

                            @Override
                            public void onSingleClick() {

                            }

                            @Override
                            public void onDelete() {

                            }
                        });
                    } else if (componentType.equals("image_view")) {
                        int componentWidth = object.optInt("width");
                        int componentHeight = object.optInt("height");
                        int componentPositionX = object.optInt("position_x");
                        int componentPositionY = object.optInt("position_y");
                        int componentRotationAngle = object.optInt("rotation_angle");
                        int componentOpacity = object.optInt("opacity");
                        final String componentViewState = object.optString("view_state");
                        String componentBitmapPath = object.optString("bitmap_path");
                        int componentShadowRadius = object.optInt("shadow_radius");
                        int componentShadowX = object.optInt("shadow_x");
                        int componentShadowY = object.optInt("shadow_y");
                        int componentShadowColor = object.optInt("shadow_color");
                        int componentShadowOpacity = object.optInt("shadow_opacity");
                        boolean componentIsColoreble = object.optBoolean("is_coloreble");
                        int componentColoreableColor = object.optInt("coloreable_color");

                        RelativeLayout.LayoutParams imageViewParam = new RelativeLayout.LayoutParams(
                                RelativeLayout.LayoutParams.MATCH_PARENT,
                                RelativeLayout.LayoutParams.MATCH_PARENT);

                        //Custom ImageView
                        BubbleTextView bubbleTextView = new BubbleTextView(context,
                                Color.BLACK, 0, "");
                        bubbleTextView.setLayoutParams(imageViewParam);
                        // bubbleTextView.
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = true;
                        int w = WIDTH_PX, h = HEIGHT_PX;
                        Bitmap.Config conf = Bitmap.Config.ARGB_8888;// see other conf types
                        Bitmap bmp = Bitmap.createBitmap(w, h, conf);// this creates a MUTABLE bitmap
                        Canvas canvasBlank = new Canvas(bmp);
                        Paint mPaint = new Paint();
                        int color = Color.argb(0, 0, 0, 0);
                        mPaint.setColor(color);
                        canvasBlank.drawRoundRect(new RectF(100, 100, 100, 100), 2, 2, mPaint);
                        bubbleTextView.setImageResource(R.drawable.apphub);
                        bubbleTextView.setBitmap(bmp);
                        if (componentViewState.equals("false")) {
                            bubbleTextView.setInEdit(false);
                        }

                        viewGroup.addView(bubbleTextView);
                        //  viewGroup.bringChildToFront(bubbleTextView);

                        bubbleTextView.setOperationListener(new BubbleTextView.OperationListener() {
                            @Override
                            public void onDeleteClick() {

                            }

                            @Override
                            public void onEdit(BubbleTextView bubbleTextView) {

                            }

                            @Override
                            public void onClick(BubbleTextView bubbleTextView) {

                            }

                            @Override
                            public void onTop(BubbleTextView bubbleTextView) {

                            }

                            @Override
                            public void onChangeFont() {

                            }
                        });
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("Exception JSON", e.getMessage());
        }

        return viewGroup;
    }

我在View添加RelativeLayout

RelativeLayout relativeLayout = new RelativeLayout(ImageSecondActivity.this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(containerLayout.getWidth(), containerLayout.getHeight());
relativeLayout.setLayoutParams(layoutParams);
containerLayout.addView(renderViewByJSON.getViewByJson(ImageSecondActivity.this, relativeLayout, jsonString));

0 个答案:

没有答案