-1

I wonder how to animate current Activity, not transition between activities. For example, I want to shift the activity slightly to the left and then move it back, so that it looks like a little shake effect. Any ideas? Thanks

Bibek Shah
  • 419
  • 4
  • 19
Konstantin Konopko
  • 5,229
  • 4
  • 36
  • 62

3 Answers3

1

Finally I have found a solution:

    Animation animation = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, -0.05f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
    animation.setDuration(100);
    animation.setInterpolator(new AccelerateInterpolator());
    animation.setRepeatCount(1);
    animation.setRepeatMode(Animation.REVERSE);

    View viewActivity = findViewById(android.R.id.content);
    if (viewActivity != null)
        viewActivity.startAnimation(animation);
Konstantin Konopko
  • 5,229
  • 4
  • 36
  • 62
0

you need to create animation instance like below

 Animation startAnimation = AnimationUtils.loadAnimation(context, R.anim.shaking_animation);

then u need to start that animation with your parent layout

   parent_layout.startAnimation(startAnimation);

you can get shake animation here shaking / wobble view animation in android

finally you can clear animation like below

parent_layout.clearAnimation();
PushpikaWan
  • 2,437
  • 3
  • 14
  • 23
0

The basic idea is to get access to the root layout of the activity and then do some animations on it, e.g. if you use DataBindung it is just like this (but not the best idea for the kind of animation you want):

    binding.getRoot().animate().rotation(-5).setDuration(500).setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            binding.getRoot().animate().rotation(5).setDuration(1000).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    binding.getRoot().animate().rotation(0).setDuration(500);
                }
            });
        }
    });
Norbert
  • 1,204
  • 1
  • 9
  • 13