简体   繁体   English

如何在Android中使用平滑的动画翻译视图

[英]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 为此,我在anim文件夹中编写了一个动画文件

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 在这里我将动画应用到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文件----------------------

<?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--------- -----------我在点击事件中应用动画的Java文件---------

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. 这将修复100%,无论您使用什么动画(例如AnimationUtils或ObjectAnimator)都没有关系。 If it doesn't work smoothly just reduce the resolution of images you are using. 如果无法正常工作,请降低所使用图像的分辨率。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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