In a little Project of mine I try to set a Point to another Point in OnTouchEvent(). Both points are created in the Constructor like this:
package com.example.samuel.truespeedgame;
import android.content.Context;
import android.graphics.Point;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private SurfaceViewThread surfaceViewThread;
public Point clickPoint, playerPoint, startPoint;
GamePanel(Context context) {
super(context);
maxSpeed = 20;
playerPoint = new Point(200,200); //very normal instanciating of Points i guess
startPoint = new Point(200,200);
clickPoint = new Point(200,200);
}
My OnTouchEvent() looks like this:
#Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
// Player has touched the screen
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if(clickPoint != null)
clickPoint.set((int)motionEvent.getX(),(int)motionEvent.getY());
else if(clickPoint == null) clickPoint = new Point((int)motionEvent.getX(),(int)motionEvent.getY());
startPoint = playerPoint; //Here gets the error thrown
break;
case MotionEvent.ACTION_UP:
}
return true;
}
It was a NullPointerException before I rebuild the Project. Now the playerPoint.x and .y is always 0.
If you don't instantiate GamePanel your playerPoint will be null.
Related
I have a class that draws a circle at the point where the user touches. However, the circle disappears whenever a new point on the same surface is touched. I would like to keep the circle and draw a new one instead. Here is my current code.
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import java.util.Random;
public class ArtSurface extends SurfaceView implements Runnable, View.OnTouchListener {
private SurfaceHolder aHolder;
private Thread aThread;
private boolean aFlag=false;
private float aX;
private float aY;
private Paint aPaint;
private Random cRandom;
public int randomColor(){
if (cRandom==null){
cRandom=new Random();
}
int randomCol=0xff000000+256*256*cRandom.nextInt(256)+256*cRandom.nextInt(256)+cRandom.nextInt(256);
return randomCol;
}
public ArtSurface(Context context, AttributeSet attrs) {
super(context, attrs);
aHolder=getHolder();
aX=-100;
aY=-100;
aPaint=new Paint();
aPaint.setColor(randomColor());
}
public void resume(){
aThread= new Thread(this);
aFlag=true;
aThread.start();
}
public void pause(){
aFlag=false;
}
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
aX= motionEvent.getX();
aY=motionEvent.getY();
break;
case MotionEvent.ACTION_UP:
//Leave art where it is
break;
}
return true;
}
#Override
public void run() {
while(aFlag){
if(!aHolder.getSurface().isValid())
continue;
Canvas canvas=aHolder.lockCanvas();
canvas.drawARGB(255,255,255,255);
canvas.drawCircle(aX,aY,50,aPaint);
aHolder.unlockCanvasAndPost(canvas);
}
}
}```
The way the code is currently, every new touch event causes a new circle to be drawn at the new point of touch. The previous circle is removed and I can only draw one circle at a time. My main aim is to have a new circle with a random color whenever the user touches a new point on the screen. All the other previous circles should remain where they were initially rendered. Thank you.
The line
canvas.drawARGB(255,255,255,255);
keeps drawing over the rendered circle. Removing it means that each new circle is visible on the canvas.
So I have followed this tutorial and the code works perfectly. However I have some trouble understanding how OnTouchListener and OnTouch work together. I have spent a long time trauling this forum, websites and documentation to understand but still, I do not.
In this code, a OnTouchListener is set for ourSurfaceView, and then the onTouch is called for the activity?!
Can someone please explain the relationship of OnTouchListener and OnTouch across different activities and views. Many Thanks!
package com.games.michael.waterproofme;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends Activity implements OnTouchListener{
MySurface ourSurfaceView;
float x,y,sX, sY, fX, fY;
Bitmap test, plus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ourSurfaceView = new MySurface(this);
ourSurfaceView.setOnTouchListener(this);
x = 0;
y = 0;
sX = 0;
sY = 0;
fX = 0;
fY = 0;
test = BitmapFactory.decodeResource(getResources(), R.drawable.sportsball);//draw ball
plus = BitmapFactory.decodeResource(getResources(), R.drawable.plus);
setContentView(ourSurfaceView);
}
#Override
protected void onPause() {
super.onPause();
ourSurfaceView.pause();
}
#Override
protected void onResume() {
super.onResume();
ourSurfaceView.resume();
}
#Override
public boolean onTouch(View v, MotionEvent event) {
x = event.getX();
y = event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
sX = event.getX();
sY = event.getY();
break;
case MotionEvent.ACTION_UP:
fX = event.getX();
fY = event.getY();
break;
}
return true;//false = finished dont loop through. true = loop through
}
public class MySurface extends SurfaceView implements Runnable{
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = false;
public MySurface(Context context){
super(context);
ourHolder = getHolder();
ourThread = new Thread(this);
ourThread.start();
}
public void pause(){
isRunning = false;
while(true){
try {
ourThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
ourThread = null;
}
public void resume(){
isRunning = true;
ourThread = new Thread(this);
ourThread.start();
}
public void run() {
while(isRunning){
if(!ourHolder.getSurface().isValid()) {/
continue;}
Canvas canvas = ourHolder.lockCanvas();
canvas.drawRGB(02, 02, 150);
if (x != 0 && y != 0){
canvas.drawBitmap(test, x-(test.getWidth()/2), y-(test.getHeight()/2), null);//bitmap, left, top, paint
}
if (sX != 0 && sY != 0){
canvas.drawBitmap(plus, sX-(plus.getWidth()/2), sY-(plus.getHeight()/2), null);//bitmap, left, top, paint
}
if (fX != 0 && fY != 0){
canvas.drawBitmap(plus, fX-(plus.getWidth()/2), fY-(plus.getHeight()/2), null);//bitmap, left, top, paint
}
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
}
OnTouchListener is interface - class that implements it must override its methods. For android.view.View.OnTouchListener this is one method: boolean onTouch(View v, MotionEvent event)
When touch event occurs in your SurfaceView there is check if onTouchListener is set and if so its onTouch method is called
Can someone please explain the relationship of OnTouchListener and OnTouch across different activities and views.
Touch events on views are invoked if you register any callback to them. SurfaceView extends the View class.
ourSurfaceView.setOnTouchListener(this);
setContentView(ourSurfaceView);
So you just set the ourSurfaceView to the activity as their content view and registered the View.OnTouchListener. That means the abstract method onTouch is invoked on the ourSurfaceView instance and not the activity.
In this code, a OnTouchListener is set for ourSurfaceView, and then the onTouch is called for the activity?!
No, it delegates any touch event from ourSurfaceView instance as their content view to your MainActivity class because you registered the View.OnTouchListener
Just a simple Java example:
public class Main {
public static void main(String[] args){
SurfaceClass surfaceClass = new SurfaceClass();
ActivityClass activityClass = new ActivityClass();
surfaceClass.setOnFartListener(activityClass);
//Touch event :D
surfaceClass.fart();
}
public static class ActivityClass implements SurfaceClass.OnFartListener{
#Override
public void onFart(String kindOfFart) {
System.out.println(kindOfFart);
}
}
public static class SurfaceClass{
private SurfaceClass.OnFartListener onFartListener;
public void fart(){
if(onFartListener != null){
onFartListener.onFart("Huge One!!");
}
}
public void setOnFartListener(SurfaceClass.OnFartListener onFartListener){
this.onFartListener = onFartListener;
}
public interface OnFartListener{
void onFart(String kindOfFart);
}
}}
See touch event below :D
javac Main.java && java Main
Hello all,
in my app, can i make an character (like, cartoon character...) to animate when i touch the screen?
i have another bitmaps (static not animated) on the screen.
i have all this character (10) bitmaps in all animation position (like a cartoon)
this is the code.. ( I NEED TO ANIMATE IT FOR EACH TOUCH )
`
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
public class Show extends Activity
{
private Bitmap imageOne;
private Bitmap imageTwo;
private Bitmap imageThree;
private List<Bitmap> images;
private ExplainView myView;
private AnimationDrawable animation;
private ImageView image;`
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(0x00000400, 0x00000400);
setContentView(R.layout.frame_layout);
image = new ImageView(this);
image.setBackgroundResource(R.drawable.tom_anim);
animation = (AnimationDrawable) rocketImage.getBackground();
images = new ArrayList<Bitmap>();
praper();
myView = new ExplainView(this,images,1); <------ this is the surface view class
FrameLayout fl = (FrameLayout)findViewById(R.id.frameLayout);
fl.addView(myView);
fl.addView(image);
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
new Runnable() {
#Override
public void run() {
image.setVisibility(View.VISIBLE);
animation.stop();
animation.start();
}
};
}
return true;
}
private void praper()
{
imageOne = BitmapFactory.decodeResource(getResources(), R.drawable.trans_ar_alpha_1);
imageTwo = BitmapFactory.decodeResource(getResources(), R.drawable.trans_ar_alpha_2);
imageThree = BitmapFactory.decodeResource(getResources(), R.drawable.trans_ar_alpha_3);
images.add(0,imageOne);
images.add(1, imageTwo);
images.add(2, imageThree);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus)
{
animation.start();
}
else
{
animation.stop();
}
}
}`
of course you can simply by using
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
x = arg1.getX();
y = arg1.getY();
switch(arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
sx=arg1.getX();
sy = arg1.getY();
break;
case MotionEvent.ACTION_UP:
fx=arg1.getX();
fy = arg1.getY();
break;
}
I want to write a program which creates a button, whenever I'm touching the screen, the button should be created on the place where the screen was touched.
I wrote a program which creates circles, whenever and where the screen is touched, can anybody explain me how to make buttons instead of circles?
Thx.
Main Activity
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View.OnTouchListener;
public class SingleTouchActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SingleTouchEventView(this, null));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.single_touch, menu);
return true;
}
}
Touch Activity
import java.util.ArrayList; //not all imports are necessary
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.view.View.OnClickListener;
public class SingleTouchEventView extends RelativeLayout {
private Paint paint = new Paint();
List<Point> points = new ArrayList<Point>();
public SingleTouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE: // a pointer was moved
case MotionEvent.ACTION_UP:
Point p = new Point();
p.x = (int)event.getX();
p.y = (int)event.getY();
points.add(p);
Button button = new Button(getContext());
int buttonHeight = 50;
int buttonWidth = 50;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(buttonWidth, buttonHeight);
params.leftMargin = p.x;
params.topMargin = p.y;
addView(button, params);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//DO SOMETHING! {RUN SOME FUNCTION ... DO CHECKS... ETC}
}
});
case MotionEvent.ACTION_CANCEL: {
break;
}
}
invalidate();
return true;
}
// Do something
}
First make your class extends a ViewGroup like RelativeLayout and FrameLayout in your case.
Then, on the touch event, create a Button (or a ImageView and setOnClickListener()), adjust the position of the button with margins on the view LayoutParams and add the button to the layout via addView().
Edit: The button creation will be like this on onTouchEvent():
Point p = new Point();
p.x = (int)event.getX();
p.y = (int)event.getY();
Button button = new Button(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(buttonWidth, buttonHeight);
params.leftMargin = p.x;
params.topMargin = p.y;
addView(button, params);
Also change this to be inside ACTION_UP to prevent creating multiple buttons on long pressing.
This is not possible. You can draw a circle because that is a graphic. If you were to make button appear then you would need to add it to XML. I assume you are making a game that requires elements to randomly appear. I suggest if this is the case to use and engine a spawn random circles and when touched do what you need. Hope this helps
I am working on a game and have run into some issues. My architecture is something like this:
class GameView is used to draw bitmaps on my surfaces
class GameLoopThread is used to implement my game loop (if it wasn't obvious...)
class MovementUtils is used to hold all of my utilities related to moving objects
I want to house methods like gravity and movement controls in MovementUtils, but I'm having trouble actually updating the values in GameView. I tried using an intent, to no avail. I'll post my code, and maybe someone can show me what I should do. Also, ignore the Accelerometer lines, that's another problem entirely...
GameView.java
package com.example.connorgame;
import java.util.EventObject;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
public class GameView extends SurfaceView {
private Bitmap platform;
private Bitmap character;
private Bitmap background;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private MainActivity mainactivity;
private MovementUtils moveutil;
public float charX = 0;
public float charY = 0;
private boolean isFalling = true;
private boolean isJumping = false;
private float initialJumpY = 0;
private int initialFrame;
private int currentFrame;
private int frameDifference;
public GameView(Context context) {
super(context);
gameLoopThread = new GameLoopThread(this);
mainactivity = (MainActivity) context;
moveutil = new MovementUtils();
holder = getHolder();
holder.addCallback(new Callback() {
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
});
platform = BitmapFactory.decodeResource(getResources(), R.drawable.platform);
character = BitmapFactory.decodeResource(getResources(), R.drawable.character);
background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(background, 0, 0, null);
canvas.drawBitmap(platform, 30, 700, null);
canvas.drawBitmap(character, charX, charY, null);
moveutil.gravity(charY, isFalling);
if (charY > getHeight() - character.getHeight()) {
initialFrame = gameLoopThread.numFrames;
initialJumpY = charY;
isFalling = false;
isJumping = true;
}
if (isJumping == true && isFalling == false) {
currentFrame = gameLoopThread.numFrames;
frameDifference = (currentFrame - initialFrame);
charY = charY - 5;
if (charY == initialJumpY - 100) {
isJumping = false;
isFalling = true;
}
}
}
}
MovementUtils.java
package com.example.connorgame;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class MovementUtils {
public void gravity (float charY, boolean isFalling) {
if(isFalling == true){
charY = charY + 5;
}
}
}
If I understand what you're doing correctly, you're trying to have charY in the GameView class modified by the gravity function. The issue is that float is a primitive, so it is based by value and only the local copy will be modified. If you passed an object instead, the object would be modified.
One solution is probably to just return the new Y position instead of trying to make gravity() change the Y position.
Another solution is to pass the GameView to MovementUtils and let MovementUtils modify it.
In other words, you would pass the GameView like this new MovementUtils(this);
And the gravity function would call a void setCharY(int y); in GameView.
public class MovementUtils {
private GameView gameView;
public MovementUtils(GameView view) {
this.gameView = view;
}
public void gravity (float charY, boolean isFalling) {
if(isFalling == true){
charY = charY + 5;
gameView.setCharY(charY);
}
}
}
Java only supports passing by value
So when you call moveutil.gravity(charY, isFalling);
and change it within the function, e.g.
public void gravity (float charY, boolean isFalling) {
if(isFalling == true){
charY = charY + 5;
}
Such change only occur in the local copy of the variable charY and does not affect the instance variable charY defined in your GameView class
To solve this, you can define your variables as objects as well as the argument to your functions, e.g.
public void gravity (Float charY, boolean isFalling) {}
And in your GameView class:
private Float charX = 0; // why you define them public !?
private Float charY = 0;
Alternatively, you can pass an instance of your GameView class or create a ValueObject class (similar to Context object in android) and use it to pass arguments