Understanding Views

satya - Wednesday, December 24, 2008 1:25:50 PM

Basic View API documentation

Basic View API documentation

satya - Wednesday, December 24, 2008 1:42:04 PM

A simple view


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);
    }
}

satya - Wednesday, December 24, 2008 1:42:47 PM

A simple activity


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

satya - Wednesday, December 24, 2008 1:44:57 PM

The quadrant of a View

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.

satya - Wednesday, December 24, 2008 1:50:19 PM

Here is a better circle draw method


@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);
    }

satya - Saturday, December 27, 2008 10:19:29 AM

SurfaceView api

SurfaceView api

satya - Saturday, December 27, 2008 8:38:42 PM

What is MeasureSpec

What is MeasureSpec

satya - Monday, December 29, 2008 9:43:30 AM

Another exercise with Measure


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;
    }
}

satya - Monday, December 29, 2008 10:19:33 AM

Refactor to a baseclass called BrickView


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;
    }
}

satya - Monday, December 29, 2008 10:20:19 AM

A CircleBrick


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);
    }
}

satya - Monday, December 29, 2008 10:27:10 AM

SquareBrick


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);
    }
}

satya - Monday, December 29, 2008 10:28:04 AM

Exercising bricks


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

satya - Monday, December 29, 2008 1:54:34 PM

When do I need to override onLayout?

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?

satya - Monday, December 29, 2008 1:54:58 PM

How can I draw with OpenGL in Android?

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?

satya - Monday, December 29, 2008 1:55:17 PM

What is the connection between SurfaceView and OpenGL?

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?