Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to draw a custom view in ZoomLayout #181

Closed
wakaztahir opened this issue Sep 28, 2020 · 5 comments
Closed

How to draw a custom view in ZoomLayout #181

wakaztahir opened this issue Sep 28, 2020 · 5 comments

Comments

@wakaztahir
Copy link

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

@wakaztahir
Copy link
Author

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

@wakaztahir
Copy link
Author

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

@wakaztahir
Copy link
Author

whatever

@markusressel
Copy link
Collaborator

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.

@wakaztahir
Copy link
Author

Thanks !

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

My issue has been solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants