How to move a onDraw shape by click direction button - java

I'm doing a simulator on Android to simulate the move of our robot(represented by a circle in this case). The circle should move forward, turn right or turn left accordingly when the forward/ right/ left button is clicked.
But when I run this program and click the buttons, the circle didn't move...
In the main activity I have the onClickListener like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.forward_button).setOnClickListener(mGlobal_OnClickListener);
findViewById(R.id.right_button).setOnClickListener(mGlobal_OnClickListener);
findViewById(R.id.left_button).setOnClickListener(mGlobal_OnClickListener);
}
View.OnClickListener mGlobal_OnClickListener = new View.OnClickListener() {
public void onClick(View v) {
GameView gameView = new GameView(Menu.this);
switch(v.getId()) {
case R.id.forward_button:
gameView.controlRobot(GameView.FORWARD);
break;
case R.id.right_button:
gameView.controlRobot(GameView.RIGHT);
break;
case R.id.left_button:
gameView.controlRobot(GameView.LEFT);
}
}
};
In the GamaView Class which extends View class, I draw a circle like this:
canvas.drawCircle((currentX * cellHeight)+(cellHeight/2), //x of center
(currentY * cellHeight)+(cellHeight/2), //y of center
(cellHeight*3*0.45f), //radius
robot);
Also In the GameView Class I have this controlRobot method to move this circle:
(The move/ rotate methods are correct. I have tested them)
public boolean controlRobot(int keyCode) {
boolean moved = false;
boolean rotated = false;
//move(0):move up on the map
//move(3):move right on the map
//move(6):move down on the map
//move(9):move left on the map
//forward
switch (keyCode) {
case FORWARD:
if(rotate_count%12==0)
moved = maze.move(0);
if(rotate_count%12==3)
moved = maze.move(3);
if(rotate_count%12==6)
moved = maze.move(6);
if(rotate_count%12==9)
moved = maze.move(9);
break;
case RIGHT:
rotated = maze.rotate(Maze.RIGHT,rotate_count);
if(rotated)
rotate_count+=3;
break;
case LEFT:
rotated = maze.rotate(Maze.LEFT,rotate_count);
if(rotated)
rotate_count-=3;
break;
}
if(moved||rotated) {
//the ball was moved so we'll redraw the view
invalidate();
}
return true;
}

when your reference the GameView in your onclick like this
GameView gameView = new GameView(Menu.this);
that is not the GameView in your layout file
R.layout.main
try referencing the GameView in your onClick like you did your buttons
findViewById(R.id.forward_button)
except with the R.id for the GameView in your layout file

Related

Android: How to tell if a user presses up, down, left or right?

I created an onTouchEvent to find where the user touches and move my object to that position. what I would like to do is if the user presses up on the screen, the object moves a certain distance straight up. and the same for the other directions. I know that I need a few if statements to do this but I don't know how to do it. does anyone have any advice or know how to do this, thanks
public boolean onTouchEvent(MotionEvent ev) {
if(ev.getAction() == MotionEvent.ACTION_DOWN) {
// the new image position became where you touched
x = ev.getX();
y = ev.getY();
// if statement to detect where user presses down
if(){
}
// redraw the image at the new position
Draw.this.invalidate();
}
return true;
}
Try this
public boolean onTouchEvent(MotionEvent ev) {
initialise boolean actionDownFlag = false;
if(ev.getAction() == MotionEvent.ACTION_DOWN) {
// the new image position became where you touched
x = (int) ev.getX();
y = (int) ev.getY();
// if statement to detect where user presses down
if(actionDownFlag){
catcher.moveDown();
}
// redraw the image at the new position
Draw.this.invalidate();
}
return true;
}
public void moveDown(){
posX -= //WhereEver you want to move the position (-5);
}
try this:
layout_counter.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event)
{
if (currentState != State.EDIT_MOVE) return false;
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
if (view.getId() != R.id.layout_counter) return false;
switch (event.getAction())
{
case MotionEvent.ACTION_MOVE:
params.topMargin = (int) event.getRawY() - view.getHeight();
params.leftMargin = (int) event.getRawX() - (view.getWidth() / 2);
view.setLayoutParams(params);
break;
case MotionEvent.ACTION_UP:
params.topMargin = (int) event.getRawY() - view.getHeight();
params.leftMargin = (int) event.getRawX() - (view.getWidth() / 2);
view.setLayoutParams(params);
break;
case MotionEvent.ACTION_DOWN:
view.setLayoutParams(params);
break;
}
return true;
}
});
You need to use OnLongClickListener which suits better with drag and drop framework of android:
Observe the following example:
public class MainActivity extends Activity {
private ImageView myImage;
private static final String IMAGEVIEW_TAG = "The Android Logo";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImage = (ImageView)findViewById(R.id.image);
// Sets the tag
myImage.setTag(IMAGEVIEW_TAG);
// set the listener to the dragging data
myImage.setOnLongClickListener(new MyClickListener());
findViewById(R.id.toplinear).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomlinear).setOnDragListener(new MyDragListener());
}
private final class MyClickListener implements OnLongClickListener {
// called when the item is long-clicked
#Override
public boolean onLongClick(View view) {
// TODO Auto-generated method stub
// create it from the object's tag
ClipData.Item item = new ClipData.Item((CharSequence)view.getTag());
String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag( data, //data to be dragged
shadowBuilder, //drag shadow
view, //local data about the drag and drop operation
0 //no needed flags
);
view.setVisibility(View.INVISIBLE);
return true;
}
}
class MyDragListener implements OnDragListener {
Drawable normalShape = getResources().getDrawable(R.drawable.normal_shape);
Drawable targetShape = getResources().getDrawable(R.drawable.target_shape);
#Override
public boolean onDrag(View v, DragEvent event) {
// Handles each of the expected events
switch (event.getAction()) {
//signal for the start of a drag and drop operation.
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
//the drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackground(targetShape); //change the shape of the view
break;
//the user has moved the drag shadow outside the bounding box of the View
case DragEvent.ACTION_DRAG_EXITED:
v.setBackground(normalShape); //change the shape of the view back to normal
break;
//drag shadow has been released,the drag point is within the bounding box of the View
case DragEvent.ACTION_DROP:
// if the view is the bottomlinear, we accept the drag item
if(v == findViewById(R.id.bottomlinear)) {
View view = (View) event.getLocalState();
ViewGroup viewgroup = (ViewGroup) view.getParent();
viewgroup.removeView(view);
//change the text
TextView text = (TextView) v.findViewById(R.id.text);
text.setText("The item is dropped");
LinearLayout containView = (LinearLayout) v;
containView.addView(view);
view.setVisibility(View.VISIBLE);
} else {
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
Context context = getApplicationContext();
Toast.makeText(context, "You can't drop the image here",
Toast.LENGTH_LONG).show();
break;
}
break;
//the drag and drop operation has concluded.
case DragEvent.ACTION_DRAG_ENDED:
v.setBackground(normalShape); //go back to normal shape
default:
break;
}
return true;
}
}
}
The above code taken from here demonstrates how to drag and drop and how to do something while dragging..
I've used something similar as well.

How to rotate image in imageview on button click each time?

This is java code.I am getting image from image gallery.I have one Button and one ImageView. It is rotating only one time.When I again click button it is not rotating image.
public class EditActivity extends ActionBarActivity
{
private Button rotate;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
rotate=(Button)findViewById(R.id.btn_rotate1);
imageView = (ImageView) findViewById(R.id.selectedImage);
String path = getIntent().getExtras().getString("path");
final Bitmap bitmap = BitmapFactory.decodeFile(path);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 510, 500,
false));
rotate.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
imageView.setRotation(90);
}
});
}
Change your onClick() method to
#Override
public void onClick(View v)
{
imageView.setRotation(imageView.getRotation() + 90);
}
Notice, what the docs say
Sets the degrees that the view is rotated around the pivot point. Increasing values result in clockwise rotation.
I'd like to update my answer to show how to use RotateAnimation to achieve the same effect in case you're also targeting Android devices running Gingerbread (v10) or below.
private int mCurrRotation = 0; // takes the place of getRotation()
Introduce an instance field to track the rotation degrees as above and use it as:
mCurrRotation %= 360;
float fromRotation = mCurrRotation;
float toRotation = mCurrRotation += 90;
final RotateAnimation rotateAnim = new RotateAnimation(
fromRotation, toRotation, imageview.getWidth()/2, imageView.getHeight()/2);
rotateAnim.setDuration(1000); // Use 0 ms to rotate instantly
rotateAnim.setFillAfter(true); // Must be true or the animation will reset
imageView.startAnimation(rotateAnim);
Usually one can setup such View animations through XML as well. But, since you have to specify absolute degree values in there, successive rotations will repeat themselves instead of building upon the previous one to complete a full circle. Hence, I chose to show how to do it in code above.

Java Android on Ecplise - double gameover screen

I'm struggeling with following problem: I'm developing a small 2D Adroid game on Eclipse ( I'm a greenhorn). The basic idea is too keep a figure on the screen and when it leaves it , it's Game Over. The basic classes are the GameActivity, the GameView, the Figure and a GameOverActivity. Since my gaming-background is drawn on a simple RecF I check when the figure leaves the screen by
public class Figure{
public RectF getBounds(){
return new RectF(x, y, x + width, y+ height);
}
}
---------------------------------
public class GameView extends SurfaceView {
private GameActivity theGameActivity = new GameActivity();
private RectF figurebounds;
public void checkifout() {
figurebounds = figure.getBounds();
if (!(background.intersect(figurebounds))) {
theGameActivity.GameOver();
}
}
------------------------------------------
public class GameActivity extends Activity {
public void GameOver() {
Intent theNextIntent = new Intent(getApplicationContext(), GameOverActivity.class);
startActivity(theNextIntent);
this.finish();
}
}
The GameOverActivity is just a Layout with 2 buttons which allows to get back to the menu or replay
public class GameOverActivity extends Activity implements OnClickListener {
Button bReplay;
Button bBack;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameoverscreen);
initialize();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.bReplay:
Intent newGameScreen= new Intent(this, GameActivity.class);
startActivity(newGameScreen);
this.finish();
break;
case R.id.bBack:
Intent newGameScreen2 = new Intent(this, MenuActivity.class);
startActivity(newGameScreen2);
this.finish();
break;
}
}
public void initialize(){
bReplay = (Button) findViewById(R.id.bReplay);
bReplay.setOnClickListener(this);
bBack = (Button) findViewById(R.id.bBack);
bBack.setOnClickListener(this);
}
At first I had the checkifout()-method within my onTouch-method (which is used to create another figures which have no influence on this issue) in the GameView-class , so I had to touch the screen after the figure fell off to get to the Gameover-screen, but at least it worked all fine:
public boolean onTouchEvent(MotionEvent event) {
if (System.currentTimeMillis() - lastClick > 300) {
lastClick = System.currentTimeMillis();
synchronized (getHolder()) {
xcor = event.getX();
ycor = event.getY();
checkifout();
}
}
return true;
}
But I'd like the GameOver-screen appear without another Touchevent.
Now when I call the checkifout()-method within my draw-method (also within the GameView class):
public void draw(Canvas canvas) {
drawBackGround(canvas);
figure.draw(canvas);
checkifout();
}
I get following problem: When my figure falls off the screen I do get the GameOver-screen, but after about a second it displays another time and when I go back to the main menu and try to exit the game I get back to the GameOverscreen, while replay stills works fine. I don't get those issues when I implement checkifout() in the OnTouchevent...
I thought it might be a problem of calling the checkifout()-method multiple times, so I tried to fix the issue by using a boolean like this :
private boolean gameover = false;
public void draw(Canvas canvas) {
drawBackGround(canvas);
figure.draw(canvas);
if (!gameover)
checkifout();
}
public void checkifout() {
figurebounds = figure.getBounds();
if (!(background.intersect(figurebounds))) {
theGameActivity.GameOver();
gameover=true;
}
But that gave me a NullPointerException.I also tried to create the checkifout-method within the Figure class and call it in the draw-method of the Figure-class, but that caused the same errors. So I hope someone has got an idea or just knows the mistake I've done here. Thanks for taking the time.
PS: Here is the MenuActivity (Layout with 2 buttons) in case it's needed:
public class MenuActivity extends Activity implements OnClickListener {
ImageButton bPlay;
ImageButton bExit;
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bPlay:
Intent newGameScreen = new Intent(this, GameActivity.class);
startActivity(newGameScreen);
this.finish();
break;
case R.id.bExit:
this.finish();
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
initialize();
}
public void initialize(){
bPlay = (ImageButton) findViewById(R.id.bPlay);
bPlay.setOnClickListener(this);
bExit = (ImageButton) findViewById(R.id.bExit);
bExit.setOnClickListener(this);
}
}
fixed, just seemed to be a temporal bug somewhere...

Draw rectangle on google map

I am extend a SupportMapFragment which contain google map here, I lay my google map on a frameLayout according to How to handle onTouch event for map in Google Map API v2?. Ondraw()method is called , but the problem is rectangle is never been showed, even when I use setWillNotDraw(false); here.
I already got the right position of the rectangle, the only problem is it is not showed. Following is my most part of code:
Add-on:When I remove the google map it works as I expect, actually the rectangle drawed on the background frameLayout, so it will be covered by the map, now the way I solve the question is add a image view as a rectangle and dynamic change its size when touch moving. I have no idea how to draw directly on the top of this viewgroup(framelayout) at the moment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
//stash reference to google map
mGoogleMap = getMap();
//show the location
mGoogleMap.setMyLocationEnabled(true);
mTouchView = new TouchableWrapper(getActivity());
mTouchView.addView(view);
return mTouchView;
}
private class TouchableWrapper extends FrameLayout {
//box is used to illustrate the drawing box, the painter is used to set the color of the box.
private Box mCurrentBox = null;
private Paint mBoxPaint;
public TouchableWrapper(Context context) {
super(context);
mBoxPaint = new Paint();
mBoxPaint.setColor(0xffff0000);
// mBoxPaint.setStrokeMiter(20);
mBoxPaint.setStrokeWidth(8);
this.setWillNotDraw(false);
setWillNotDraw(false);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// decide when the child should hold the motion event.
return true; //always let the relative layout to hold the motion event.
}
#Override
public boolean onTouchEvent(MotionEvent event) {
PointF curr = new PointF(event.getX(), event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mMapIsTouched = true;
mCurrentBox = new Box(curr);//original code for draw Rectangle.
Log.d(TAG, "action.down box is"+mCurrentBox);
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG, "action_move");
if (mCurrentBox != null) {
mCurrentBox.setmCurrent(curr);
Log.d(TAG, "action.move box is"+mCurrentBox);
this.invalidate(); //TODO
}
break;
case MotionEvent.ACTION_UP:
Log.i(TAG, "action_up");
mMapIsTouched = false;
mCurrentBox = null;
break;
}
return true;
}
#Override
protected void onDraw(Canvas canvas) {
float left = 0;
float right = 0;
float top = 0;
float bottom = 0;
if (mCurrentBox!=null) {
left = Math.min(mCurrentBox.getmOrigin().x, mCurrentBox.getCurrent().x) ;
right = Math.max(mCurrentBox.getmOrigin().x, mCurrentBox.getCurrent().x) ;
top = Math.min(mCurrentBox.getmOrigin().y, mCurrentBox.getCurrent().y) ;
bottom = Math.max(mCurrentBox.getmOrigin().y, mCurrentBox.getCurrent().y) ;
canvas.drawRect(left, top, right, bottom, mBoxPaint);
}
Log.i(TAG, "value is"+left+right+top+bottom);
}
You could wrap the Map in a FrameLayout containing also a Drawable (your rectangle). In that way, once you find where the rectangle shall be, you could hide and show it without having to redraw it.
You can see here for an example.

Android Animation Bug wont let me click after animation move to the right

So, here's my problem? I have it where it will slide out and then it will stay in that position and then you should be able to click on it again and it will slide back to the original location.
But instead I have to click on where it FIRST was at. Even when it's slided out. I want to make it after the animation I can click it any where, that it slided out.
Here's my code
public void sideBar()
{
ImageView sidebar = (ImageView)findViewById(R.id.sidebar);
if(out == 0)
{
mSlideInRight = AnimationUtils.loadAnimation(this, R.anim.slide_in_right);
mSlideInRight.setFillAfter(true);
sidebar.startAnimation(mSlideInRight);
out= 1;
}
else if(out == 1)
{
mSlideInLeft = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
sidebar.startAnimation(mSlideInLeft);
out=0;
}
}
and this is the code for when you click on it
public void onClick(View v) {
ImageView sidebar = (ImageView)findViewById(R.id.sidebar);
ImageView popup = (ImageView)findViewById(R.id.popup);
ImageView popup2 = (ImageView)findViewById(R.id.popup2);
switch(v.getId())
{
case R.id.sidebar:
sideBar();
break;
}
}
Animations only affect how your sidebar ImageView is drawn, not its actual position. After your animation completes, you should update the sidebar's layout parameters to your desired position. You can do this in an AnimationListener if you want the position to be updated as the animation runs or after it is complete, or right after your call to startAnimation if you want the position change to happen as the animation starts.
mSlideInRight.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation anim) {};
#Override
public void onAnimationRepeat(Animation anim) {};
#Override
public void onAnimationEnd(Animation anim) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(...);
// Set properties of layoutParams here
sidebar.setLayoutParams(layoutParams);
};
});
Replace RelativeLayout with whatever layout your ImageView is a child of.

Categories