简体   繁体   中英

How to translate view with smooth animation in android

I have written a peace of code that perform animation. for that i have written a animation file in anim folder

my animone.xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/linear_interpolator"
     android:fillAfter="true">
     <translate
         android:fromYDelta="0%p"
         android:toYDelta="-5%p"
         android:duration="1500"/></set>

and here i apply the animation on the view that is a LinearLayout

Animation animate= AnimationUtils.loadAnimation(context,R.anim.animone);
            subProfile.setVisibility(View.VISIBLE);//subProfile is a linear layout
            subProfile.startAnimation(animate);

so when i run this code . it will suddenly change the position of view without smooth sliding after specified duration. i want to make it smooth. please help.

----------------here is my complete code---------------- Main Fragment file

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.videosapp.Fragments.HomeFragment"
android:background="@drawable/background">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/my_pofile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_centerInParent="true"   android:visibility="gone">

            <ImageView
                android:id="@+id/dshImg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_centerHorizontal="true"
                android:background="@drawable/dashboard"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Dashboard"
                android:layout_below="@id/dshImg"
                android:textColor="@color/colorText"
                android:textSize="18sp"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>
        <LinearLayout android:id="@+id/podcast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_centerInParent="true"   android:visibility="gone">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/podcast"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Podcast"
                android:textColor="@color/colorText"
                android:textSize="18sp" />
        </LinearLayout>
        <LinearLayout android:id="@+id/photo_gallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_centerInParent="true"   android:visibility="gone">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/photo_gallery"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Photo Gallery"
                android:textColor="@color/colorText"
                android:textSize="18sp" />
        </LinearLayout>
        <LinearLayout android:id="@+id/social"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_centerInParent="true"   android:visibility="gone">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/social"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Social"
                android:textColor="@color/colorText"
                android:textSize="18sp" />
        </LinearLayout>
        <LinearLayout android:id="@+id/about_us"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_centerInParent="true"   android:visibility="gone">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/aboutus"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Know US"
                android:textColor="@color/colorText"
                android:textSize="18sp" />
        </LinearLayout>
        <LinearLayout android:id="@+id/contact_us"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_centerInParent="true"   android:visibility="gone">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/contact_us"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Contact Us"
                android:textColor="@color/colorText"
                android:textSize="18sp" />
        </LinearLayout>
        <LinearLayout android:id="@+id/setting"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_centerInParent="true"   android:visibility="gone">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/settings"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Settings"
                android:textColor="@color/colorText"
                android:textSize="18sp"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>
        <LinearLayout android:id="@+id/exit_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_centerInParent="true"   android:visibility="gone">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/exit"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Exit"
                android:textColor="@color/colorText"
                android:textSize="18sp" />
        </LinearLayout>
        <ImageView
            android:id="@+id/menu_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/center_menu"
            android:layout_centerInParent="true"   />
    </RelativeLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
     <!--On following  LinearLayout i want to apply animation-->
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_centerInParent="true"
        android:id="@+id/profile"
        android:visibility="gone"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/my_profile_icon"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="My Profile"
            android:gravity="center_vertical"
            android:textColor="@color/colorText"
            android:textSize="20sp"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:orientation="horizontal"
        android:layout_centerInParent="true"
        android:id="@+id/programs"
        android:visibility="gone"
        >
        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/folder_icon"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="My Programs"
            android:gravity="center_vertical"
            android:textColor="@color/colorText"
            android:textSize="20sp"
            />
    </LinearLayout>
</RelativeLayout>

----------------------animation xml file----------------------

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromYDelta="0%"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:toYDelta="-100%" /></set>

-----------java file in which i apply the animation on click event---------

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.my_pofile:
            animate= AnimationUtils.loadAnimation(ctx,R.anim.animone);
            subProfile.setVisibility(View.VISIBLE);
            subProfile.startAnimation(animate);
    break;
    //other cases
}}

Try this:

  <?xml version="1.0" encoding="utf-8"?>

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromYDelta="0%"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:toYDelta="-100%" />

Try to

reduce the resolution or size of all images inside that layout

It will fix the smooth problem.

I also experience this problem and searched on internet but nothing found, then I felt that everything was slow on my layout, then I reduced the resolution and size of all images including backgrounds and you what everything starts working so smooth.

This will fix 100%, It doesn't matter what you use for animation such as AnimationUtils or ObjectAnimator. If it doesn't work smoothly just reduce the resolution of images you are using.

Thanks

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