简体   繁体   English

Android:自定义视图未绘制

[英]Android : Custom View not drawing

I'm getting a problem with a part of my Android application. 我的Android应用程序部分出现问题。 I need to draw a graph in a custom Dialog box but it doesn't work even when I follow all the solutions found on the internet. 我需要在自定义对话框中绘制图形,但是即使遵循互联网上找到的所有解决方案,它也无法正常工作。 My goal is to show a Dialog box when a user is clicking on a button on my main frame. 我的目标是在用户单击主机上的按钮时显示一个对话框。 To do this, I want to draw it in a very simple Dialog Box. 为此,我想在一个非常简单的对话框中绘制它。 My graph need to be "scrollable" because it can be bigger than the Dialog box. 我的图形需要“可滚动”,因为它可以大于“对话框”。 Here are the sources codes : 这是源代码:

public class GraphDialog extends Dialog implements android.view.View.OnClickListener
{
    Canvas canvas;

    public GraphDialog(Context context) 
    {
        super(context);
        System.out.println("Test");
        this.setContentView(R.layout.dialog_graph_layout);
        this.setTitle(R.string.title_dialog_graph);
        ((Button) this.findViewById(R.id.button_ok)).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) 
    {
        Controller.getPollManager().setPoll(null);
        Controller.getPollManager().setTab(null);
        this.cancel();
    }
}

Here is my xml : 这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <ScrollView
        android:id="@+id/scroll_layout"
        android:layout_width="500px"
        android:layout_height="800px"
        android:layout_weight="1"
        android:drawingCacheQuality="low"
        android:scrollbars="vertical" >

        <be.ac.ucl.lfsab1509.proxipoll.GraphView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

        </be.ac.ucl.lfsab1509.proxipoll.GraphView>
    </ScrollView>

    <Button
        android:id="@+id/button_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0"
        android:text="@string/ok" />

</LinearLayout>

And here is my custom View : 这是我的自定义视图:

public class GraphView extends View
{
    Paint paint;

    public GraphView(Context context) 
    {
        super(context);
        System.out.println("test GraphView");
        this.setWillNotDraw(false);
        init();
    }

    public GraphView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        System.out.println("test GraphView");
        this.setWillNotDraw(false);
        init();
    }

    public void init()
    {
        paint = new Paint();

        paint.setColor(Color.BLACK);
    }

    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        System.out.println("test onDraw");
        canvas.drawText(Controller.getPollManager().getPoll().name, 50, 50, paint);
        this.draw(canvas);
    }
}

To check what goes wrong, you can see that I have added some println(). 为了检查出什么问题,您可以看到我添加了一些println()。 When I try to show the Dialog box, I get all lines except the on of the onDraw() method. 当我尝试显示Dialog对话框时,除了onDraw()方法的on之外,我得到了所有其他行。 Does someone know what to do to make it work ? 有人知道该怎么做才能使其正常工作吗?

Thank you 谢谢

Try overriding onMeasure : when I last did any custom charting, I needed to do something like this. 尝试覆盖onMeasure :当我上一次进行任何自定义图表绘制时,我需要执行以下操作。

@Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec )
    {
    viewWidth = MeasureSpec.getSize( widthMeasureSpec );
    viewHeight = MeasureSpec.getSize( heightMeasureSpec );

    if(box.getWidth() != 0){
        // use the screen width: oldViewWidth -> screenWidth, viewWidth -> maxXRange
        boxWidth = box.getWidth() - box.getPaddingRight() - box.getPaddingLeft() - 10;
        if(screenWidth <= 0) viewWidth = boxWidth;
        else viewWidth = (int)((getMaxXRange()*boxWidth)/screenWidth);
        if(viewWidth < boxWidth) viewWidth = boxWidth;
    }
    setMeasuredDimension( viewWidth, viewHeight );
    }

This basically checks the size of the window and tells the graphics layer how big I want it to be. 这基本上检查窗口的大小,并告诉图形层我希望它有多大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM