Trying to draw on a canvas when the screen is touched

Problems with Canvas, OpenGL, etc...

Trying to draw on a canvas when the screen is touched

Postby SolidSora » Wed Oct 20, 2010 2:46 am

I'm new to Android development, and I'm trying to make it so that it draws a rect on the screen when it is touched.

What would I need to fix for it to work?

Here is code to one of my classes.
Code: Select all
package org.games.pizza;


import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class Pizza extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
   private Button startButton;
   private PizzaView pizzaView;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        startButton = (Button) findViewById(R.id.start_button);
        startButton.setOnClickListener(this);
       
    }
   
    public void onClick(View v){
       pizzaView = new PizzaView(this);
      setContentView(pizzaView);
      pizzaView.requestFocus();
    }
}


and my other class
Code: Select all
package org.games.pizza;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View.OnTouchListener;
import android.view.MotionEvent;

public class PizzaView extends View implements OnTouchListener{

   private boolean touching;
   float touchX;
   float touchY;
   private float width;
   private float height;
   
   public PizzaView(Context context){
      super(context);
      setFocusable(true);
      setFocusableInTouchMode(true);
   }
   
   @Override
   protected void onSizeChanged(int w, int h, int oldw, int oldh){
      width = w/9f;
      height = h/9f;
      super.onSizeChanged(w, h, oldw, oldh);
   }
   
   @Override
   protected void onDraw(Canvas canvas){
      Paint myPaint = new Paint();
      myPaint.setColor(R.color.paint);
      
      //if(touching){
         canvas.drawRect(0, 0, getWidth(), getHeight(), myPaint);
      //}
   }
   
   @Override
   public boolean onTouch(View v, MotionEvent event){

   switch (event.getAction()&MotionEvent.ACTION_MASK){
   case MotionEvent.ACTION_DOWN:
      touchX = event.getX();
      touchY = event.getY();
      touching = true;
      break;
   }
   return true;
   }
}
SolidSora
Junior Developer
Junior Developer
 
Posts: 24
Joined: Tue Oct 12, 2010 12:56 am

Top

Re: Trying to draw on a canvas when the screen is touched

Postby Tage Nielsen » Wed Oct 20, 2010 8:03 pm

You need to call invalidate() after the user touches the screen, as it will cause your view, PizzaView, to call its method onDraw().
Tage Nielsen
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Oct 13, 2010 8:02 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 10 guests