JavaBeginner
JavaBeginner

Reputation: 27

How to move a image across the screen on Android

How to move an image across the screen android?

Guys, im creating MoleMash game for My Coursework and I really need help!. I want to try to move a image (the mole) across the screen in different and random coordinates so that it can be touched by the user to get a score. I created the background which I made the image view. I have the mole, Is it another image view? or is it a Image button?

And how do I move this across to different parts of the screen for the user interact?

I would greatly appreciate your help!

Upvotes: 2

Views: 18374

Answers (3)

Ranjithkumar
Ranjithkumar

Reputation: 18356

Try this code,

declare following variables

private Random random=new Random();

private int screenwidth, screenhgt;

In onCreate():

screenwidth= getyourScreenWidth;

screenhgt=getyourscreenheight;

Pass your view to this method(In my case I animate textview.. you pass the imageview)

private void screenRandomAnimator(final TextView textView) {

    final AnimatorSet mAnimatorSet = new AnimatorSet();
    mAnimatorSet.playTogether(ObjectAnimator.ofFloat(textView, "x", (float) random.nextInt(screenwidth), (float) random.nextInt(screenwidth)),
            ObjectAnimator.ofFloat(textView, "y", (float) random.nextInt(screenhgt), (float) random.nextInt(screenhgt)), ObjectAnimator.ofFloat(textView, "rotation", 360)
        /*    ObjectAnimator.ofFloat(textView, "scaleX", 1, 0.8f, 1, 1.1f, 1), ObjectAnimator.ofFloat(textView, "scaleY",  1, 0.8f, 1, 1.1f, 1)*/);
    int Low = 1500;
    int High = 2500;
    int Result = random.nextInt(High - Low) + Low;
    mAnimatorSet.setDuration(Result);
    mAnimatorSet.start();
    mAnimatorSet.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            mAnimatorSet.playTogether(ObjectAnimator.ofFloat(textView, "x", textView.getX(), (float) random.nextInt(screenwidth)),
                    ObjectAnimator.ofFloat(textView, "y", textView.getY(), (float) random.nextInt(screenhgt)));
            int Low = 1500;
            int High = 2500;
            int Result = random.nextInt(High - Low) + Low;
            mAnimatorSet.setDuration(Result);
            mAnimatorSet.start();
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
}

Upvotes: 0

Lijap
Lijap

Reputation: 625

The mole would be an image button so it could be clicked, but to make it move you would have to use animation. Keep in mind that I am a beginner in Android animation as well :). You would probably use TranslateAnimation and set the x and y in the parameters to random variables, the duration could be when a time/counter variable reaches a certain point after the game has started. This post has more information on animation: How to move an image from left to right in android.

Upvotes: 0

Lumis
Lumis

Reputation: 21639

For games development you need to learn how to use canvas and SurfaceView: How can I use the animation framework inside the canvas?

Using onTouch event listener you would compare the touch location with the location of your animated images...

Upvotes: 2

Related Questions