Android java app, replacing image on touch
Android java app, replacing image on touch
I just want to say I am very new to java app development.
So I have this image floating around the screen and when I touch the screen I want the image to change colors (switch from image to image1)
I got this working but the image gets moved to starting point, I want the transition to be smooth and to take place where the image is at certain point.
I have this code in my GameView.java class
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private MainThread thread;
private CharacterSprite characterSprite;
private int col = 0;
public GameView(Context context)
super(context);
getHolder().addCallback(this);
thread = new MainThread(getHolder(),this);
setFocusable(true);
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
@Override
public void surfaceCreated(SurfaceHolder holder)
characterSprite = new CharacterSprite(BitmapFactory.decodeResource(getResources(),R.drawable.avdgreen), 100, 100);
thread.setRunning(true);
thread.start();
@Override
public void surfaceDestroyed(SurfaceHolder holder)
boolean retry = true;
while(retry)
try
thread.setRunning(false);
thread.join();
catch(InterruptedException e)
e.printStackTrace();
retry = false;
public void update()
characterSprite.update();
public boolean onTouchEvent(MotionEvent event)
int X = (int) event.getX();
int Y = (int) event.getY();
int eventaction = event.getAction();
switch (eventaction)
case MotionEvent.ACTION_DOWN:
if(col == 0)
int ox = characterSprite.getX();
int oy = characterSprite.getY();
characterSprite.setX(ox);
characterSprite.setX(oy);
characterSprite = new CharacterSprite(BitmapFactory.decodeResource(getResources(),R.drawable.avdgreen1), ox, oy);
Toast.makeText(getContext(), "ACTION_UP "+"X: "+ox+" Y: "+oy, Toast.LENGTH_LONG).show();
col = 1;
else
int ox = characterSprite.getX();
int oy = characterSprite.getY();
characterSprite.setX(ox);
characterSprite.setX(oy);
characterSprite = new CharacterSprite(BitmapFactory.decodeResource(getResources(),R.drawable.avdgreen), ox, oy);
Toast.makeText(getContext(), "ACTION_UP "+"X: "+ox+" Y: "+oy, Toast.LENGTH_LONG).show();
col = 0;
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
return true;
@Override
public void draw(Canvas canvas)
super.draw(canvas);
if(canvas!=null)
characterSprite.draw(canvas);
This is where most of work is taking place, I also have CharacterSprite.java class which basicly creates the 'image' and makes it float around the screen:
public class CharacterSprite {
private Bitmap image;
private int x,y;
private int xVelocity = 10;
private int yVelocity = 5;
private int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
private int screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
public CharacterSprite(Bitmap bmp, int x, int y)
image = bmp;
public int getX()
return x;
public int getY()
return y;
public void setX(int nx)
x = nx;
public void setY(int ny)
y = ny;
public void draw(Canvas canvas)
canvas.drawBitmap(image,x,y,null);
public void update()
if(x < 0 && y < 0)
x = screenWidth / 2;
y = screenHeight / 2;
else
as You can see I tried to get current x and y and set it back when creating new character, but that did not work, my guess is that this is happening because for a second the image is not on the screen and the whole thing reset but 1. I am not sure if thats right 2. I have no idea how I could fix that.
If anyone could point me where I should search that would be awesome.
@edit:
I changed to this Toast.makeText(getContext(), "ACTION_UP "+"X: "+characterSprite.getX()+" Y: "+characterSprite.getY(), Toast.LENGTH_LONG).show();
To debug and the output I am getting is ACTION_UP X: 0 Y: 0
Toast.makeText(getContext(), "ACTION_UP "+"X: "+characterSprite.getX()+" Y: "+characterSprite.getY(), Toast.LENGTH_LONG).show();
ACTION_UP X: 0 Y: 0
1 Answer
1
ANSWER
Added
public void updateD(Canvas canvas)
canvas.drawBitmap(image,x,y,null);
To CharacterSprite.java
and changed:
CharacterSprite.java
if(col == 0)
int ox = 550; //characterSprite.getX();
int oy = 550;//characterSprite.getY();
// characterSprite.setX(ox);
//characterSprite.setX(oy);
characterSprite = new CharacterSprite(BitmapFactory.decodeResource(getResources(),R.drawable.avdgreen1));
Toast.makeText(getContext(), "ACTION_UP "+"X: "+characterSprite.getX()+" Y: "+characterSprite.getY(), Toast.LENGTH_LONG).show();
col = 1;
To:
if(col == 0)
characterSprite.setImg(BitmapFactory.decodeResource(getResources(),R.drawable.avdgreen1));
Toast.makeText(getContext(), "ACTION_UP "+"X: "+characterSprite.getX()+" Y: "+characterSprite.getY(), Toast.LENGTH_LONG).show();
col = 1;
@ʍѳђઽ૯ท yes, my bad forgot to add it in the first line.
– hDDen
Sep 10 '18 at 18:28
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Is that an answer?
– Mohsen
Sep 10 '18 at 18:27