开发者

onDraw not getting called for a custom view derived from view in layout

I have a custom view .........

package com.yasir.canvasTest;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.Log;
import android.view.View;


public class GraphView extends View {

    private static final String TAG = "GraphView";
    public static boolean BAR = true;
    public static boolean LINE = false;

    private Paint paint;
    private float[] values;
    private String[] horlabels;
    private String[] verlabels;
    private String title;
    private boolean type;
    public Canvas tmpCanvas = null;

    public GraphView(Context context, float[] values, String title, String[] horlabels, String[] verlabels) {
        super(context);
        //setScrollContainer(true);
        Log.v(TAG,"On GraphView==>");




        paint = new Paint();

        setWillNotDraw(false);
        requestLayout();
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {




        Log.v(TAG,"<==On Draw");

/// drawing code here
        invalidate();
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.v(TAG,"On onMeasure==>");
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMea开发者_如何转开发sureSpec);
        setMeasuredDimension(
                parentWidth * 2, parentHeight);
    }

}

............... Activity: onCreate ...............

setContentView(R.layout.horscroll);

HorizontalScrollView hsView = (HorizontalScrollView) findViewById(R.id.hzsv);

LinearLayout ll = (LinearLayout) findViewById(R.id.linearParent);
ll.removeAllViews();


// create custom view and add to laywout
GraphView graphView = new GraphView(this, values, "GraphViewDemo",horlabels, verlabels, GraphView.LINE);


hsView.requestLayout();
hsView.setWillNotDraw(false);

ll.requestLayout();
ll.setWillNotDraw(false);
ll.addView(graphView);

..................... Xml ....................

<?xml version="1.0" encoding="utf-8"?>

<HorizontalScrollView
 android:id="@+id/hzsv"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:id="@+id/linearParent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
 </HorizontalScrollView>

..................

Regards, Yasir Perwez


Try checking with Hierarchy Viewer what's going on. I suspect your view is not getting drawn because it has a 0 width, and this could be for two reasons (either or both):

  • you're not setting any LinearLayout.LayoutParams on your custom view before adding it to the layout, and I'm not sure if the default ones are of the "fill parent" kind;
  • your onMeasure() is incomplete, it should take into account also the mode (UNSPECIFIED, EXACTLY, AT_MOST) wich will depend from the layout params too. There's no guarantee that getSize() returns parent's size, and as the framework is meant, you shouldn't even know if that happens.

(@slund's suggestion will probably be useful too)


Your GraphView is getting measured with a width of 0 (zero). You are then requesting that it be set to 2*width, which is still zero. The draw logic will optimize not asking a zero width view to draw.

Probably what you want is to ask the HorizontalScrollView to fill the full width of the view with android:fillViewport="true", ie:

<?xml version="1.0" encoding="utf-8"?>

 <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hzsv"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">
    <LinearLayout 
        android:id="@+id/linearParent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </LinearLayout>
</HorizontalScrollView>

ALso see @bigstones answer. You really need to handle all MeasureSpec types (UNSPECIFIED, AT_MOST, EXACTLY). Setting fillViewport="true" gets your onMeasure called with type EXACTLY and the width of the viewport.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜