How to get random number on Android Object Animator

主宰稳场 提交于 2019-12-24 12:13:52

问题


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);`

回答1:


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);



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/22125075/how-to-get-random-number-on-android-object-animator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!