如何获得Android上的动画对象的随机数 [英] How to get random number on Android Object Animator
问题描述
您好,大家好我试图让我的对象动画随机数。
我想获得的随机数为TranslationX
`
imagebutton1.setOnClickListener(新OnClickListener(){ 公共无效的onClick(查看为arg0){
反++;
最终的TextView分数=(的TextView)findViewById(R.id.textView1);
score.setText(分数:+计数器); ObjectAnimator动画= ObjectAnimator.ofFloat(imagebutton1,translationX,100F,100F);
anim.setDuration(3600);
anim.start();;
anim.setInterpolator(新AccelerateDecelerateInterpolator());
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(5);`
首先获得屏幕,这可能给你限制,直到在那里你可以翻译X
<的宽度/ p>
DisplayMetrics displaymetrics =新DisplayMetrics();
。getWindowManager()getDefaultDisplay()getMetrics(displaymetrics)。
INT宽度= displaymetrics.widthPixels;
包含随机()
从屏幕的最大宽度得到随机的翻译。
随机R =新的随机();
INT translationX = r.nextInt(宽)
使用这些随机产生的翻译你的看法。
公共无效的onClick(视图v){
...
ObjectAnimator动画= ObjectAnimator.ofFloat(imagebutton1,translationX,100F,100F);
}
在你的屏幕翻译视图。
您可以查看当前位置与它的 getLeft()
, GetRight时()
,共达()
和 getBottom()
。使用这些组合来获取视图当前位置和从当前位置到新的随机位置也就是屏幕内平移。
DisplayMetrics displaymetrics =新DisplayMetrics();
。getWindowManager()getDefaultDisplay()getMetrics(displaymetrics)。
INT宽度= displaymetrics.widthPixels;
INT高度= displaymetrics.heightPixels;随机R =新的随机();
INT translationX = r.nextInt(宽);
INT translationY = r.nextInt(高度)TranslateAnimation阿尼姆=新TranslateAnimation(currentX,translationX,currentY,translationY); //使用当前视图位置,而不是`currentX`和`currentY`
anim.setDuration(1000);
anim.setFillAfter(真);
涂抹于查看
您可以与处理器张贴的时间表间隔动画。
view.startAnimation(阿尼姆);
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);`
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);
这篇关于如何获得Android上的动画对象的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!