用于多个设备的android动画视图

时间:2015-04-01 15:10:54

标签: android animation views objectanimator

我的应用程序中有一个小游戏,如果你得到一些正确的气球漂浮在屏幕上,气球是由动画集中的2个ObjectAnimators移动的视图,并且在我的主调试设备上它工作正常但在其他设备上(包括平板电脑在内,它看起来很可怜​​,价值观遍布整个地方,而且从一侧到另一侧,视野都是猛烈的摇摆。  这是我正在做的事情的片段:

//漂浮2个方形气球

            sb = (ImageView)findViewById(R.id.squareballoon);
            sb.setVisibility(View.VISIBLE);

            sb2 = (ImageView)findViewById(R.id.squareballoon2);
            sb2.setVisibility(View.VISIBLE);

            sp.play(inflate, 1, 1, 0, 0, 1);

//左气球

            ObjectAnimator sqbalAnim3 = ObjectAnimator.ofFloat(sb,"x",-500,500);
            sqbalAnim3.setDuration(700);
            sqbalAnim3.setRepeatCount(5);
            sqbalAnim3.setRepeatMode(ValueAnimator.REVERSE);

            ObjectAnimator sqbalAnim = ObjectAnimator.ofFloat(sb,"y",2000,-1800);
            sqbalAnim.setDuration(3000);
            sqbalAnim.setRepeatMode(ValueAnimator.RESTART);

            AnimatorSet animSetXY = new AnimatorSet();
            animSetXY.playTogether(sqbalAnim, sqbalAnim3);
            animSetXY.start();

//右气球

            ObjectAnimator sqbalAnim4 = ObjectAnimator.ofFloat(findViewById(R.id.squareballoon2),"x",-1500,-500);

            sqbalAnim4.setDuration(700);
            sqbalAnim4.setRepeatCount(5);
            sqbalAnim4.setRepeatMode(ValueAnimator.REVERSE);

            ObjectAnimator sqbal2Anim = ObjectAnimator.ofFloat(findViewById(R.id.squareballoon2),"y",1800,-1800);
            sqbal2Anim.setDuration(3000);
            sqbal2Anim.setRepeatMode(ValueAnimator.RESTART);


            AnimatorSet animSetXY2 = new AnimatorSet();
            animSetXY2.playTogether(sqbal2Anim,sqbalAnim4);
            animSetXY2.start();

//气球动画结束

            animSetXY2.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);

                            sp.play(dropSound,1,1,0,0,1);
                            sb.setBackgroundResource(R.drawable.burst);
                            pop = (AnimationDrawable) sb.getBackground();
                            pop.start();
                            sb2.setBackgroundResource(R.drawable.burst);
                            pop = (AnimationDrawable) sb2.getBackground();
                            pop.start();

                }
            });
            return true;}

        //end of square balloons

我读过我可以使用分数而不是显式值,任何人都可以帮我解决这个问题或指向正确的方向, 欢迎任何和所有建议,非常感谢

1 个答案:

答案 0 :(得分:1)

我猜您的问题出在您正在使用的参数(硬编码)中:

ObjectAnimator.ofFloat(sb,"x",-500,500);
ObjectAnimator.ofFloat(sb,"y",2000,-1800);

由于此类值仅适用于您正在测试的设备(实际屏幕)。其他设备有其他屏幕与其他分辨率,你应该考虑它。您也应该将值从dp计算为像素。实际上500和2000是什么?如果它的像素很可能就是问题所在 看看这里(对于dp到px):
What is the difference between "px", "dp", "dip" and "sp" on Android?
Android: 'dp' to 'px' conversion?