Zoomlayout: How to draw a custom view in ZoomLayout

Created on 28 Sep 2020  ·  5Comments  ·  Source: natario1/ZoomLayout

How do I?

How do I draw my custom view which can become zoomable with the use of this library

package com.example.mindmapping;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.otaliastudios.zoom.ZoomLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        MindMap map = new MindMap(this);

        setContentView(R.layout.activity_main);
        ZoomLayout zoomLayout = findViewById(R.id.zoomLayoutContainer);
        zoomLayout.addView(map);
        setContentView(map);

    }
}

Where map is a Custom View , extends View & Overrides onDraw Method , It contains Other elements like TextElement & Other which are also custom views or extending from linear layout and contain a text view : Like This

package com.example.mindmapping;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.widget.Toast;

public class MindMap extends View {

    private Context context;
    private Canvas canvas;
    private Paint brush;

    //Canvas Variables
    private float centerMarginX;
    private float centerMarginY;
    private float mWidth;
    private float mHeight;

    public MindMap(Context context) {
        super(context);
        this.context = context;

        initSetup();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.canvas = canvas;

        initDrawing();
    }

    public void initSetup() {
        brush = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    public void initDrawing() {

        brush.setStyle(Paint.Style.STROKE);
        brush.setStrokeWidth(10);
        brush.setColor(Color.parseColor("black"));
        canvas.drawText("Helo ",50,50,brush);

        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();

        centerMarginX = mWidth / 2;
        centerMarginY = mHeight / 2;

        TextElement textElement = new TextElement(context, "Main Topic");
        textElement.setX(centerMarginX - (textElement.getLayoutWidth() / 2));
        textElement.setY(centerMarginY - (textElement.getLayoutHeight() / 2));
        textElement.draw(canvas);

        TextElement textElement1 = new TextElement(context, "Another One");
        textElement1.setX(textElement.getX() + textElement.getLayoutWidth() + 600);
        textElement1.setY(textElement.getY() - textElement.getLayoutHeight());
        textElement1.draw(canvas);

        Toast.makeText(this.context,"Drawn",Toast.LENGTH_SHORT).show();
    }
}

The custom view (map in this case) is not drawn in the ZoomLayout even though onDraw method is called but when I set map in setContentView instead of the layout file and don't use ZoomLayout , The Map is drawn on the canvas , I want to draw my custom view which should be zoomable with this library

Version used

1.8.0

question

Most helpful comment

Thanks !

I Needed to call setMeasuredDimension in overridden onMeasure , because this is a fully customized view , docs !

My issue has been solved.

All 5 comments

Nothing is drawn on the canvas , not even that helo text , drawn in initDrawing()

that setContentView has been called again to set the map as a view as a test , it was a comment , removing it does't solve the problem

whatever

Your custom view may be drawn with 0 size. Try to debug your layout, either using the developer setting on the device/emulator, or through Android Studio's layout inspector.

Thanks !

I Needed to call setMeasuredDimension in overridden onMeasure , because this is a fully customized view , docs !

My issue has been solved.

Was this page helpful?
5 / 5 - 1 ratings

Related issues

YiHaoHuang picture YiHaoHuang  ·  10Comments

kuoliangkwong picture kuoliangkwong  ·  4Comments

MohamedMedhat1998 picture MohamedMedhat1998  ·  6Comments

lucasrsv picture lucasrsv  ·  3Comments

natario1 picture natario1  ·  13Comments