1

Hello guys i am trying to get random number in my object animator. I want to get random number for "TranslationX" `

           imagebutton1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
           counter++;
            final TextView score = (TextView)findViewById(R.id.textView1);
            score.setText("Score:"+counter);

            ObjectAnimator anim = ObjectAnimator.ofFloat(imagebutton1, "translationX", 100f, 100f);


            anim.setDuration(3600);
            anim.start();;
            anim.setInterpolator(new AccelerateDecelerateInterpolator());
            anim.setRepeatMode(Animation.REVERSE);
            anim.setRepeatCount(5);`
Boldbayar
  • 862
  • 1
  • 9
  • 22

3 Answers3

2

First of all get the Width of Screen which may give you limit till where you can translate X

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;

Include Random() to get the random translation from the maximum width of screen.

Random r = new Random();
int translationX = r.nextInt(width)

Use these random generated in translating your view.

public void onClick(View v) {
       ...
        ObjectAnimator anim = ObjectAnimator.ofFloat(imagebutton1, translationX, 100f, 100f);
 }

Translating view over your screen.

You can get the view current position with it's getLeft(), getRight(), getTop() and getBottom(). Use these combination to get the view current position and translate from current position to new random position which is within the screen.

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;

Random r = new Random();
int translationX = r.nextInt(width);
int translationY = r.nextInt(height)

TranslateAnimation anim = new TranslateAnimation( currentX, translationX , currentY, translationY ); //Use current view position instead of `currentX` and `currentY`
anim.setDuration(1000);
anim.setFillAfter(true);

Apply animation on View which you can post with Handler on schedule interval of time.

 view.startAnimation(anim);
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
0
Random rand = new Random();

int  n = rand.nextInt(50) + 1;

where nextInt value is the Maximum value so in this example its from 1- 50

MDMalik
  • 3,951
  • 2
  • 25
  • 39
  • Wow sir you saved my day i was wondering if i could use it at that line but it works thank you for helping sir . Sharing is caring i try to help people like you ^^. – Boldbayar Mar 02 '14 at 07:50
  • Accept my answer.. that would be caring ;) – MDMalik Mar 02 '14 at 07:51
0

First, import the class

import java.util.Random;

Then create a randomizer:

Random r = new Random();

Then call the randomizer with the various next... methods. For example,

int locX = r.nextInt(600) + 50;
int locY = r.nextInt(1024) + 50;

The randomizer creates a pseudo-random number in the range of 0 to 1 (including 0, but not including 1). When you call the nextFloat or nextInt method, it multiplies this random number by the parameter and then coerces the result into the appropriate type.

See details of the randomizer at http://developer.android.com/reference/java/util/Random.html

Hutch
  • 301
  • 2
  • 5