简体   繁体   中英

Animate view from top to center of screen

i am trying to move image view from top of screen to center or middle of the screen using translation animation ie when activity is started image view is start moving from top of screen to middle or center of screen where top of image view space and bottom of image view space shows equal on screen exactly as we do like in relative layout use tag center In Parent true in xml file of layout. generally we find these kind of animation in facebook and whatsapp application they have used for images to translate or move image view animation.i have tried lots of SO question and answer also googling but not find proper solution. what i have done so far as following.Please help me to solve these issue.thanks.

public class MainActivity extends AppCompatActivity {

    ImageView imageview;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageview = (ImageView) findViewById(R.id.imagview);

        RelativeLayout root = (RelativeLayout) findViewById(R.id.rel);


        TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 50);
        anim.setInterpolator((new AccelerateDecelerateInterpolator()));
        anim.setAnimationListener(new MyAnimationListener());
        anim.setDuration(500);
        anim.setFillAfter(true);
        imageview.startAnimation(anim);


            }
        });


    }

Assuming you have a View which is position on top left corner of screen, we want to animate it to the centre of screen.

Layout file:

<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/root">

    <View
            android:id="@+id/animated_view"
            android:layout_width="50dp"
            android:background="#6eed57"
            android:layout_height="50dp"/>

</FrameLayout>

In onCreate() :

final View root = findViewById(R.id.root);
final View animatedView = findViewById(R.id.animated_view);

root.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {
        animatedView.animate()
                    .translationX((root.getWidth() - animatedView.getWidth()) / 2)
                    .translationY((root.getHeight() - animatedView.getHeight()) / 2)
                    .setInterpolator(new AccelerateInterpolator())
                    .setDuration(500);
    }
}); 

Result:

在此处输入图片说明

You don't start an animation in onCreate() because the screen isn't completely visible for the user. You also don't need to get root view if your root view uses the entire area of the screen.

boolean hasAnimationStarted;

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus && !hasAnimationStarted) {
        hasAnimationStarted=true;
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        ObjectAnimator translationY = ObjectAnimator.ofFloat(imageview, "y", metrics.heightPixels / 2 - imageview.getHeight() / 2); // metrics.heightPixels or root.getHeight()
        translationY.setDuration(500);
        translationY.start();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM