Hi all - I'm trying to get up to speed with custom views, and I took most of this code from examples, but I cannot get it to do anything. When I run the code below in the emulator I just get a blank screen, and when I set debug breakpoints it shows that onDraw() never gets called! It's got to be something dumb and simple, but I don't see it. I'm just trying to get a circle painted for starters. I even tried adding an "mv.invalidate()" call to the onCreate() method to try and get it to redraw but nothing changed. Help!
Thanks in advance! [main.xml] <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.examples.customview.myView android:id="@+id/myView" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> [/main.xml] [myActivity.java] package com.examples.customview; import android.app.Activity; import android.os.Bundle; public class Compass extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myView mv = (myView)this.findViewById(R.id.myView); } } [/myActivity.java] [myView.java] package com.examples.customview; import android.content.Context; import android.graphics.*; import android.view.*; import android.util.AttributeSet; import android.content.res.Resources; public class myView extends View { [...private members...] public myView(Context context){ super(context); initView(); } public myView(Context context, AttributeSet attrs){ super(context, attrs); initView(); } public myView(Context context, AttributeSet attrs, int defaultStyle){ super(context, attrs, defaultStyle); initView(); } protected void initView(){ setFocusable(true); circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); circlePaint.setColor(R.color.background_color); circlePaint.setStrokeWidth(1); circlePaint.setStyle(Paint.Style.FILL_AND_STROKE); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // The compass is a circle that fills as much screen as possible. // Set the measured dimensions to the shortest screen boundary. int measuredWidth = measure(widthMeasureSpec); int measuredHeight = measure(heightMeasureSpec); int d = Math.min(measuredWidth, measuredHeight); //setMeasuredDimension must be called by onMeasure setMeasuredDimension(d, d); } private int measure(int measureSpec){ int result = 0; //Decode the spec value int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(specMode); if(specMode == MeasureSpec.UNSPECIFIED){ //Return a default value result = 200; } else { //Return full bounds since we are filling available space result = specSize; } return result; } @Override protected void onDraw(Canvas canvas){ //Find the center point int px = getMeasuredWidth() / 2; int py = getMeasuredHeight() / 2; int radius = Math.min(px, py); //Draw the background canvas.drawCircle(px, py, radius, circlePaint); } } [/myView.java] --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---