Android的 - 使翻译和objectAnimator在同一个XML文件 [英] Android - Making translations and objectAnimator on the same XML file

查看:173
本文介绍了Android的 - 使翻译和objectAnimator在同一个XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图做一个3D立方体旋转效果,同时从一个片段滑动到另一台。 首先我使用的是翻译的影响(对XML)调用与 FragmentTransaction.setCustomAnimations(...)然后,打开/关闭的片段时,我在玩相机CLASSE使旋转

I've been trying to make a 3D Cube rotation effect while sliding from one fragment to another. First i was using a translate effect (on XML) calling with FragmentTransaction.setCustomAnimations(...) and then, when opening/closing the fragment, i was playing with the Camera classe to make the rotation.

这是工作的罚款,但似乎我有太多(不要问我为什么)使用所有只使用XML文件这部动画。经过长时间的搜索,我发现,我应该使用objectAnimator进行旋转。

This was working FINE, but seems that I HAVE TOO (don't ask me why) use all of this animation using only XML file. After a long search i found out that i should use objectAnimator to make the rotation.

随后的谷歌样品和我设法使翻转动画。现在我需要翻译的碎片使它们滑动并滑出。看来,我不能用objectAnimator并翻译在同一个XML文件的效果。由于出现此错误:

Followed the Google sample and i manage to make the flip animation. Now i need to translate the fragments making them sliding in and sliding out. Seems that i can't use objectAnimator and translate effect on the same XML file. Since this error appears:

java.lang.RuntimeException: Unknown animator name: translate at (...)

我如何可以使滑动效果和使用objectAnimator在同一时间任何想法?

Any ideas on how i can make the sliding effect and use the objectAnimator on the same time?

感谢您的时间!

code我一直在使用:

Code i've been using:

card_flip_right_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Before rotating, immediately set the alpha to 0. -->
    <objectAnimator
        android:duration="0"
        android:propertyName="alpha"
        android:valueFrom="1.0"
        android:valueTo="0.0" />

    <!-- Rotate. -->
    <objectAnimator
        android:duration="@integer/card_flip_time_full"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:propertyName="rotationY"
        android:valueFrom="180"
        android:valueTo="0" />

    <!-- Half-way through the rotation (see startOffset), set the alpha to 1. -->
    <objectAnimator
        android:duration="1"
        android:propertyName="alpha"
        android:startOffset="@integer/card_flip_time_half"
        android:valueFrom="0.0"
        android:valueTo="1.0" />

</set>

片段调用另一个片段:(立方体旋转应在可见这2)

private void launchArticle(int prev, int pos){
        ArticleFragment newFragment = new ArticleFragment();
        Bundle args = new Bundle();
        args.putString("pos", pos);
        args.putInt("prev", prev);
        newFragment.setArguments(args);
        android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
        Fragment currFrag = (Fragment)getFragmentManager().findFragmentById(R.id.headlines_fragment);
        if (currFrag != null) {
                transaction.hide(currFrag);
        }
        transaction.setCustomAnimations(
                R.animator.card_flip_right_in,
                R.animator.card_flip_right_out,
                R.animator.card_flip_left_in,
                R.animator.card_flip_left_out
                );

        transaction.replace(R.id.fragment_container, newFragment, pos);
        transaction.addToBackStack(null);

        transaction.commit();
}

更新:

我用管理延伸我的,我使用

I've manage to solve the previous problem using a class that extends my framelayout of the fragments i'm using

SlidingFrameLayout.java

public class SlidingFrameLayout extends FrameLayout
{
    private static final String TAG = SlidingFrameLayout.class.getName();
    public SlidingFrameLayout(Context context) {
        super(context);
    }

    public SlidingFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public float getXFraction()
    {
        final int width = getWidth();  
        if(width != 0) return getX() / getWidth();  
        else return getX();  
    }

    public void setXFraction(float xFraction) {
        final int width = getWidth();  
        setX((width > 0) ? (xFraction * width) : -9999);  
    }

    public float getYFraction()
    {
        final int height = getHeight();  
        if(height != 0) return getY() / getHeight(); else return getY();   
    }

    public void setYFraction(float yFraction) {
        final int height = getHeight();  
        setY((height > 0) ? (yFraction * height) : -9999);  
    }
}

和通过将这个给objectAnimator:

and by adding this to the objectAnimator:

<!-- Move -->
    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="@integer/card_flip_time_full"
        android:interpolator="@android:anim/linear_interpolator"
        android:propertyName="xFraction"
        android:valueFrom="-1"
        android:valueTo="0" />

这是更好的工作,但rottation轴的的FrameLayout的中间,这不是使一个立方体的错觉......是否有可能设置旋转轴线上某一点?

This is working better, but the rottation axes are in the middle of the FrameLayout and it's not making the illusion of a cube... Is it possible to set the rotation axes on a certain point?

推荐答案

通过创建我自己的方法对扩展的FrameLayout问题解决了。下面是从扩展的FrameLayout的code:

Solved the problem by creating my own methods on the extended FrameLayout. Here's the code from the extended FrameLayout:

//Rotate from Left to Right turning visible
    public float getRotateLeftRightIn(){
        return getRotationY();
    }
    public void setRotateLeftRightIn(int rotateLeftRightIn){
        setPivotX(getWidth());
        setPivotY(getHeight()/2);
        setRotationY(rotateLeftRightIn);
    }

和对XML:

<!-- Rotate. -->
<objectAnimator
    android:duration="@integer/card_flip_time_full"
    android:interpolator="@android:interpolator/accelerate_decelerate"
    android:propertyName="rotateLeftRightIn"
    android:valueFrom="@integer/card_flip_rotation_off"
    android:valueTo="0" 
    android:valueType="intType"/>

在这种情况下, @整数/ card_flip_time_full 代表的是整个动画的持续时间和 @整数/ card_flip_rotation_off 代表着度(在此情况下〜90%)。

In this case, @integer/card_flip_time_full stands for the duration of the entire animation and @integer/card_flip_rotation_off stands for the degrees (in this case -90%).

在此,我需要做的,使这个动画的工作,开始的片段时,在自定义动画设置的XML文件

After this, all I need to do to make this animation to work is, when starting the fragment, set the xml files in the custom animation

transaction.setCustomAnimations(enter,exit,popEnter,popExit);

希望这是很有用的一些一^^

Hope this can be useful to some one ^^

这篇关于Android的 - 使翻译和objectAnimator在同一个XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆