Graphics Android




CS185c

Chris Pollett

Apr 4, 2012

Outline

Basic Graphics Android

GraphicsTestView Screenshot

Image of a red rectangle

Drawing with XML

Making Shapes

Screenshot XML Graphics

Image of a four ovals with different borders

Animation

Driver Class

Screenshot AnimationTest

Image of last screen in animation test

A Second Android Animation example

package org.pollett;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.ArcShape;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

public class AnimationTest3 extends Activity {

	protected static final int GUIUPDATEID = 0x1FF;
	AnimRunnable animRunnable;
	GraphicsTestView myGraphicsView;
	
	Handler myGUIUpdateHandler = new Handler() {
		public void handleMessage(Message msg) 
		{			
			switch(msg.what) 
			{
				case AnimationTest3.GUIUPDATEID:
					myGraphicsView.invalidate();
				break;
			}
		}
	};
 
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        myGraphicsView = new GraphicsTestView(this);
        setContentView(myGraphicsView);
        
        new Thread(new AnimRunnable(this)).start();
    }
    
    private static class AnimRunnable implements Runnable
    {
    	AnimationTest3 myAnimation;
    	public AnimRunnable(AnimationTest3 myAnim)
    	{
    		myAnimation = myAnim;
    	}
		public void run() 
		{
                   while(!Thread.currentThread().isInterrupted()) 
                   {
                      Message message = new Message();
                      message.what = AnimationTest3.GUIUPDATEID;
                      myAnimation.myGUIUpdateHandler.sendMessage(message);

                      try
                      {
                         Thread.sleep(100);
                    
                      } 
                      catch(InterruptedException e)
                      {
                         Thread.currentThread().interrupt();
                      }
                   }
		}
    	
    }
    private static class GraphicsTestView extends View
    {
    	private ShapeDrawable myDrawable;
    	int ovalX;
    	int ovalY;
    	int direction;
    	
    	public GraphicsTestView (Context context) 
    	{
    	   super(context);
    	   setFocusable(true);

	       OvalShape oval = new OvalShape();
	       myDrawable = new ShapeDrawable(oval);
    	   myDrawable.getPaint().setColor(0xFFFF0000);
    	   ovalX = 10;
    	   ovalY = 10;
    	   direction = 5;
    	}
    
	    @Override
	    protected void onDraw(Canvas canvas)
	    // the onDraw method is where a view draws itself
	    {
	    	int width = 50;
	    	int height = 20;
	    	myDrawable.setBounds(ovalX, ovalY, ovalX+ width, ovalY + height);
	
	    	myDrawable.draw(canvas);
	    	if(ovalX < 10 || ovalX > 250) 
	    	{
	    		direction = -direction;
	    	}
	    	ovalX +=direction;
	    }
    }
    
}