Understanding Views

Basic View API documentation


public class FirstView extends View 
{
	public FirstView(Context context)
	{
		super(context);
	}
    @Override protected void onDraw(Canvas canvas) 
    {
        Paint paint = new Paint();
        
        canvas.drawColor(Color.WHITE);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(3);

        canvas.drawCircle(10, 10, 20, paint);
    }
}

public class BasicViewActivity extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(new FirstView(this));
    }
}//eof-class

In an x-y plot, the view quadrant is the 4th quadrant or the bottom right. So the circle above partially shows in the fourth quadrant.


@Override protected void onDraw(Canvas canvas) 
    {
        Paint paint = new Paint();
        
        canvas.drawColor(Color.WHITE);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(3);

        int height = canvas.getHeight();
        int width = canvas.getWidth();
        
        int centerX = width/2;
        int centerY = height/2;
        int radius = centerX;
        
        canvas.drawCircle(centerX, centerY, centerX, paint);
    }

SurfaceView api

What is MeasureSpec


public class FirstView extends View 
{
   public FirstView(Context context)
   {
      super(context);
   }
    @Override protected void onDraw(Canvas canvas) 
    {
        Paint paint = new Paint();
        
        //Give a background color of Blue
        canvas.drawColor(Color.BLUE);
        
        //Give the paint a color of RED
        paint.setColor(Color.RED);
        paint.setStrokeWidth(3);
        
        Log.d("fv:","canvas width:" + canvas.getWidth());
        Log.d("fv:","canvas height:" + canvas.getHeight());
      
        int mh = this.getMeasuredHeight();
        int mw = this.getMeasuredWidth();
        
        Log.d("fv:","view width:" + mw);
        Log.d("fv:","view height:" + mh);
        
        int centerX = mw/2;
        int centerY = mh/2;
        int radius = Math.min(centerX,centerY);
        
        canvas.drawCircle(centerX,centerY,radius, paint);
    }
    
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)     
    {
       int width=40;
       int height=40;
       measureWidth(widthMeasureSpec);
       setMeasuredDimension(width,height);
    }
    private int measureWidth(int wSpec)
    {
       int spec = MeasureSpec.getMode(wSpec);
       int size = MeasureSpec.getSize(wSpec);
       Log.d("fv:", "width Spec:" + spec);
       if (spec == MeasureSpec.AT_MOST)
       {
          Log.d("fv:", "width spec: AT_MOST");
       }
       else if (spec == MeasureSpec.EXACTLY)
       {
          Log.d("fv:", "width spec: EXACTLY");
       }
       else
       {
          Log.d("fv:", "width spec: unspecified");
       }
       Log.d("fv:", "width size:" + size);
       return size;
    }
}

public class BrickView extends View 
{
   private int suggestedWidth = 40;
   private int suggestedHeight = 40;
   public BrickView(Context context, int width, int height)
   {
      super(context);
      this.suggestedHeight = height;
      this.suggestedWidth = width;
   }
   public BrickView(Context context)
   {
      this(context,40,40);
   }
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)     
    {
       measureWidth(widthMeasureSpec);
       setMeasuredDimension(suggestedWidth,suggestedHeight);
    }
    private int measureWidth(int wSpec)
    {
       int spec = MeasureSpec.getMode(wSpec);
       int size = MeasureSpec.getSize(wSpec);
       Log.d("fv:", "width Spec:" + spec);
       if (spec == MeasureSpec.AT_MOST)
       {
          Log.d("fv:", "width spec: AT_MOST");
       }
       else if (spec == MeasureSpec.EXACTLY)
       {
          Log.d("fv:", "width spec: EXACTLY");
       }
       else
       {
          Log.d("fv:", "width spec: unspecified");
       }
       Log.d("fv:", "width size:" + size);
       return size;
    }
}

public class CircleBrick extends BrickView 
{
   public CircleBrick(Context context)
   {
      super(context);
   }
    @Override protected void onDraw(Canvas canvas) 
    {
        Paint paint = new Paint();
        
        //Give a background color of Blue
        canvas.drawColor(Color.BLUE);
        
        //Give the paint a color of RED
        paint.setColor(Color.RED);
        paint.setStrokeWidth(3);
        
        Log.d("fv:","canvas width:" + canvas.getWidth());
        Log.d("fv:","canvas height:" + canvas.getHeight());
      
        int mh = this.getMeasuredHeight();
        int mw = this.getMeasuredWidth();
        
        Log.d("fv:","view width:" + mw);
        Log.d("fv:","view height:" + mh);
        
        int centerX = mw/2;
        int centerY = mh/2;
        int radius = Math.min(centerX,centerY);
        
        canvas.drawCircle(centerX,centerY,radius, paint);
    }
}

public class SquareBrick extends BrickView 
{
   public SquareBrick(Context context)
   {
      super(context);
   }
    @Override protected void onDraw(Canvas canvas) 
    {
        Paint paint = new Paint();
        
        //Give a background color of Blue
        canvas.drawColor(Color.BLUE);
        
        //Give the paint a color of RED
        paint.setColor(Color.RED);
        paint.setStrokeWidth(3);
        
        int mh = this.getMeasuredHeight();
        int mw = this.getMeasuredWidth();
        
        canvas.drawRect(0,0,mw,mh,paint);
    }
}

public class BasicViewActivity extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(getView());
    }
    private View getView()
    {
       LinearLayout ll = new LinearLayout(this);
       ll.setOrientation(LinearLayout.VERTICAL);
       ll.addView(new CircleBrick(this));
       ll.addView(new SquareBrick(this));
       ll.addView(new CircleBrick(this));
       return ll;
    }
}//eof-class

When do I need to override onLayout?

Search Google for: When do I need to override onLayout?

Search Android Developers Group for: When do I need to override onLayout?

Search Android Beginers Group for: When do I need to override onLayout?

Search Google Code for: When do I need to override onLayout?

Search Android Issues Database for: When do I need to override onLayout?

How can I draw with OpenGL in Android?

Search Google for: How can I draw with OpenGL in Android?

Search Android Developers Group for: How can I draw with OpenGL in Android?

Search Android Beginers Group for: How can I draw with OpenGL in Android?

Search Google Code for: How can I draw with OpenGL in Android?

Search Android Issues Database for: How can I draw with OpenGL in Android?

What is the connection between SurfaceView and OpenGL?

Search Google for: What is the connection between SurfaceView and OpenGL?

Search Android Developers Group for: What is the connection between SurfaceView and OpenGL?

Search Android Beginers Group for: What is the connection between SurfaceView and OpenGL?

Search Google Code for: What is the connection between SurfaceView and OpenGL?

Search Android Issues Database for: What is the connection between SurfaceView and OpenGL?