【问题标题】:Android view animation tweens choppy / jerkyAndroid视图动画补间波涛汹涌/生涩
【发布时间】:2014-09-17 23:03:39
【问题描述】:

我正在为我的应用程序制作一个相当基本的主菜单屏幕,我的两个按钮从左侧滑入,一个图像视图通过提高 alpha 值在它们后面弹出。到目前为止,我已经很好地完成了这项工作,但是所有的动画都非常生涩/断断续续,而且我使用的任何调整似乎都无法修复它。

奇怪的是,动画在我的 10 英寸平板电脑上非常流畅,但在我较新的手机上,动画是生涩的。我在所有设备上使用完全相同的图像(在可绘制文件夹中,没有特殊的高分辨率版本)所有用于滑动按钮和图像视图的图像都是小于 100k 大小的 PNG,都具有透明度。

基本上,我正在尝试找出如何平滑这些动画,以及为什么它们在我的旧平板电脑上会如丝般流畅,但在新手机上却很生涩。我的平板电脑可以完美运行是三星 Galaxy Tab 2 10.1" 和我的动画生涩的手机是 Google Nexus 4 和 Nexus 5。

动画 XML 1:

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" 
 android:interpolator="@android:anim/accelerate_interpolator" 
 android:duration="700"
 android:repeatCount="0"/>
</set>

还有第二个动画XML:

<?xml version="1.0" encoding="utf-8"?>
<set>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromXDelta="-100%" android:toXDelta="0%"  android:duration="1000"  />
</set>

主菜单的XML模板:

<RelativeLayout 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"
android:background="@drawable/menuimage1med"
android:orientation="vertical"
android:scrollbarStyle="outsideOverlay"
tools:context=".MainActivity" >

     <ImageView
        android:id="@+id/menu_mask"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/menumask1" 
        android:scaleType="fitXY"/>

     <RelativeLayout
      android:id="@+id/relativeLayout1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical" >

     <Button
        android:id="@+id/menuButton3"
        android:layout_width="140dp"
        android:layout_height="75dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/menuButton1"
        android:layout_marginTop="5dp"
        android:background="@drawable/extrasbutton" />


     <Button
        android:id="@+id/menuButton1"
        android:layout_width="170dp"
        android:layout_height="90dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/menuButton2"
        android:background="@drawable/continuebutton" />

     <Button
        android:id="@+id/menuButton2"
        android:layout_width="fill_parent"
        android:layout_height="120dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/newstory" /> 
    </RelativeLayout>

   <Button
    android:id="@+id/musicButton"
    android:layout_width="65dp"
    android:layout_height="65dp"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/relativeLayout1"
    android:background="@drawable/musicicon" />

</RelativeLayout>

还有我的代码:

    //Main Menu view animations  
    ImageView imageView= (ImageView)findViewById(R.id.menu_mask);
    Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    imageView.startAnimation(fadeInAnimation); 

    Button menuButton1 = (Button) findViewById(R.id.menuButton1);
    Button menuButton3 = (Button) findViewById(R.id.menuButton3);

    Animation flyInAnimation = AnimationUtils.loadAnimation(this, R.anim.fly_in);
    menuButton1.startAnimation(flyInAnimation);
    menuButton3.startAnimation(flyInAnimation);

此代码不在 onCreate 方法中,因为我知道这可能会导致潜在问题。

编辑:我检查了 Logcat,当返回主菜单并播放动画时,它显示“跳过 56 帧!应用程序可能在其主线程上做了太多工作。我不认为滑动按钮的轻动画会很麻烦?如果是这样的话,为什么它会在劣质平板电脑上顺利运行仍然没有意义。

【问题讨论】:

  • 您找到解决方案了吗?

标签: android xml animation view


【解决方案1】:

我的猜测与高端手机的操作系统版本未设置为默认使用硬件加速有关。将此添加到您的清单中:

android:hardwareAccelerated="true"

在您尝试制作动画的活动中。

【讨论】:

  • 感谢您的建议。我将此行添加到相应活动的清单中,但不幸的是发现性能没有变化。
  • @josephus,因为 regomar 注意到没有区别。原因是(如您在developer.android.com 中看到的)如果您将minSdkVersiontargetSdkVersion 设置为“14”或更高,则android:hardwareAccelerated 属性默认为true。
猜你喜欢
  • 1970-01-01
  • 2015-12-20
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 2012-02-21
  • 2014-08-02
相关资源
最近更新 更多