Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. Problem: Canvas onDraw Android

Problem: Canvas onDraw Android

Scheduled Pinned Locked Moved Android
androidxmlhelp
1 Posts 1 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pancakeleh
    wrote on last edited by
    #1

    Hello! I have been trying to draw different rectangles on the canvas after several times of button click. It should display different colored rectangle and the rectangle should remain on canvas after every button click. The rectangles should be able to move around the canvas. I have 4 buttons on my main.xml file. After invoking the first button, a blue colored rectangle shld appear on the canvas. If the first button is invoked the second time, another blue colored rectangle shld appear as well however the code i have right now only appear one blue rectangle regardless the number of times the button is invoked. Here are my codes:

    public class DrawRectangle extends View {

    public DrawRectangle(Context context){

    super(context);
    

    }

    protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    	Rect ourRect = new Rect();
    	
    	ourRect.set(0, 0, canvas.getWidth()/4, canvas.getHeight()/4);
    	
    	Paint paintColor = new Paint();
    	
    	 // make the entire canvas white
        paintColor.setColor(Color.WHITE);
        canvas.drawPaint(paintColor);
    	
         
    	
    	if(MainActivity.isBlue){
            paintColor.setColor(Color.BLUE);
        }
    
        paintColor.setStyle(Paint.Style.FILL);
    
        //Draw to actual canvas
        canvas.drawRect(ourRect, paintColor);
        canvas.save();
        
        if(MainActivity.isRed){
            paintColor.setColor(Color.RED);
        }
    
        paintColor.setStyle(Paint.Style.FILL);
    
        //Draw to actual canvas
        canvas.drawRect(ourRect, paintColor);
        canvas.save();
        
        if(MainActivity.isYellow){
            paintColor.setColor(Color.YELLOW);
        }
    
        paintColor.setStyle(Paint.Style.FILL);
        canvas.save();
    
        //Draw to actual canvas
        canvas.drawRect(ourRect, paintColor);
        
        if(MainActivity.isGreen){
            paintColor.setColor(Color.GREEN);
        }
    
        paintColor.setStyle(Paint.Style.FILL);
    
        //Draw to actual canvas
        canvas.drawRect(ourRect, paintColor);
        canvas.save();
        
        
        }
        
    } 
    

    and this is my code for my first button:

    public static boolean isBlue, isRed, isYellow, isGreen;
    DrawRectangle dv;

    bluebutton.setOnClickListener(new View.OnClickListener() {

    	@Override
    	public void onClick(View arg0) {
    		
    		// TODO Auto-generated method stub
    		isBlue = true;
            isRed = false;
            isGreen = false;
            isYellow = false;
       
             
            dv.inv
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups