AppCompat v22:通过XML引用新的插值器

3
我一直在尝试利用支持库v22中现有的新插值器(如FastOutLinearIn等)。更具体地说,我想通过XML引用它们。
我尝试使用@android:interpolator/fast_out_linear_in,但LINT告诉我那是v21+版本。我还试图在其他地方搜索,但没有找到任何看起来能够帮助我的内容。
我还尝试创建自定义插值器,就像这样:(在my_linear_out_slow_in.xml中)
<?xml version="1.0" encoding="utf-8"?>
<linearOutSlowInInterpolator />

并且

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.animation.LinearOutSlowInInterpolator          
   xmlns:android="http://schemas.android.com/apk/res/android" />

但是它们在Lollipop或之前的版本中不起作用:活动转换不再出现,导致应用程序处于旧活动仍可见但无响应状态,我猜测是因为新活动被视为活动状态但未被渲染。 在升级之前,当v21+样式使用新的(非兼容)插值器时,它可以正常工作。

1个回答

2
很遗憾,无法在xml中使用设计支持库的插值器。这是因为AnimationUtils如何处理创建插值器的方式。
摘自Gingerbread(API 9)源码http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.2_r1/android/view/animation/AnimationUtils.java?av=f
 private static Interpolator createInterpolatorFromXml(Context c, XmlPullParser parser)
        throws XmlPullParserException, IOException {

    Interpolator interpolator = null;

    // Make sure we are on a start tag.
    int type;
    int depth = parser.getDepth();

    while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
           && type != XmlPullParser.END_DOCUMENT) {

        if (type != XmlPullParser.START_TAG) {
            continue;
        }

        AttributeSet attrs = Xml.asAttributeSet(parser);

        String  name = parser.getName();


        if (name.equals("linearInterpolator")) {
            interpolator = new LinearInterpolator(c, attrs);
        } else if (name.equals("accelerateInterpolator")) {
            interpolator = new AccelerateInterpolator(c, attrs);
        } else if (name.equals("decelerateInterpolator")) {
            interpolator = new DecelerateInterpolator(c, attrs);
        }  else if (name.equals("accelerateDecelerateInterpolator")) {
            interpolator = new AccelerateDecelerateInterpolator(c, attrs);
        }  else if (name.equals("cycleInterpolator")) {
            interpolator = new CycleInterpolator(c, attrs);
        } else if (name.equals("anticipateInterpolator")) {
            interpolator = new AnticipateInterpolator(c, attrs);
        } else if (name.equals("overshootInterpolator")) {
            interpolator = new OvershootInterpolator(c, attrs);
        } else if (name.equals("anticipateOvershootInterpolator")) {
            interpolator = new AnticipateOvershootInterpolator(c, attrs);
        } else if (name.equals("bounceInterpolator")) {
            interpolator = new BounceInterpolator(c, attrs);
        } else {
            throw new RuntimeException("Unknown interpolator name: " + parser.getName());
        }

    }

    return interpolator;

}

它们遵循相同的代码路径,但在棒棒糖中添加了一个新的插值器PathInterpolator,它是所有新插值器的“基类”。

您可以使用新的插值器,但是它们需要在xml的版本化资源目录中。一种解决方法是创建自己的“AnimationUtils”,但这意味着每当您找到接受动画id的方法时,都需要包含一个方法+您的R.anim.*。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接