如何为摇动事件中的文本设置动画

时间:2019-07-15 17:41:22

标签: java android android-studio sensor android-sensors

我目前有一个允许用户摇动手机的应用程序,并且数组中出现了不同的文本。但是,文本始终显示得太快,并且如果用户不断晃动电话,文本也会不断变化。我该如何执行以下操作:

1)对文本进行动画处理,这样在摇动手机后,文本会逐渐淡化显示吗?

  1. 我该如何在晃动上实施延迟,以使用户在摇动手机之前等待几秒钟?

我已经为单击的按钮实现了此功能,但是似乎与Sensor Manager无关。

protected final SensorEventListener sensorListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {


            float x = sensorEvent.values[0];
            float y = sensorEvent.values[1];
            float z = sensorEvent.values[2];

            acelLast = acelVal;
            acelVal = (float) Math.sqrt((double) (x*x + y*y + z*z));
            float delta = acelVal - acelLast;
            shake = shake * 0.9f + delta;

            if (shake > 12) {

                int randomNum = new Random().nextInt(answersArray.length);
                ball_text.setText(answersArray[randomNum]);
                ball_text.setVisibility(View.VISIBLE);


            }





        }



        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

    // Sensor Section //

1 个答案:

答案 0 :(得分:0)

  1. 您需要为textview的alpha值更改设置动画。
  2. 您需要跟踪用户上次摇晃的时间并将其用于检查。

以下几行应该会起作用,

long previousTime = 0;

protected final SensorEventListener sensorListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            float x = sensorEvent.values[0];
            float y = sensorEvent.values[1];
            float z = sensorEvent.values[2];

            acelLast = acelVal;
            acelVal = (float) Math.sqrt((double) (x*x + y*y + z*z));
            float delta = acelVal - acelLast;
            shake = shake * 0.9f + delta;

            if (shake > 12) {
              //Fade in text if difference between previous and current time is > 2 seconds
              if (System.currentTimeMillis() - previousTime > 2000) {
                previousTime = System.currentTimeMillis();
                //Use AlphaAnimation
                Animation fadeInAnimation = new AlphaAnimation(0, 1);
                fadeInAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        int randomNum = new Random().nextInt(answersArray.length);
                        ball_text.setText(answersArray[randomNum]);
                        ball_text.setVisibility(View.VISIBLE);
                    }
                    @Override
                    public void onAnimationEnd(Animation animation) {}
                    @Override
                    public void onAnimationRepeat(Animation animation) {}
                });
                fadeInAnimation.setInterpolator(new DecelerateInterpolator());
                fadeInAnimation.setDuration(1000);
                ball_text.startAnimation(fadeInAnimation);
                /*
                //This should work, but it doesn't, I'll prob tinker with it on some weekend, I'll update the answer if I figure out the issue
                ball_text.animate()
                   .withStartAction(new Runnable() {
                            @Override
                            public void run() {
                                int randomNum = new Random().nextInt(answersArray.length);
                                ball_text.setText(answersArray[randomNum]);
                                ball_text.setAlpha(0f);
                                ball_text.setVisibility(View.VISIBLE);
                            }
                        })
                   .alpha(1f)
                   //Fade in over a duration of 1 second
                   .setDuration(1000)
                   .start()

                */
              } else {
                //If it's less than 2 seconds since last shake, inform user to wait
                //For eg,
                Toast.makeText(context, "Wait mate", Toast.LENGTH_SHORT).show();
              }
            }
        }
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };