我怎样才能设置" toXDelta"从java代码翻译动画值?

时间:2015-12-04 13:41:18

标签: android android-animation translate-animation

如何从java代码设置XDelta值?我需要先计算它,这样才能在xml中使用最终值。我可以在我的视图中使用animate()。translateX但是我不能让动画减速到最后......任何想法?

3 个答案:

答案 0 :(得分:1)

在翻译动画中,您可以在制作对象时发送该值。

TranslateAnimation trans = new TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta);

你在这里读到::

  

public TranslateAnimation(float fromXDelta,float toXDelta,float fromYDelta,float toYDelta)

     

在API级别1中添加   从代码

构建TranslateAnimation时使用的构造方法      

参数

     

fromXDelta更改X坐标以应用于动画的开头

     

toXDelta更改X坐标以应用于动画结尾

     

fromYDelta更改Y坐标以应用于动画的开头

     

toYDelta更改Y坐标以应用于动画结尾

我希望这是你的要求

答案 1 :(得分:1)

您可以使用 Viewfiliper 方法。参考是:ViewFlipper | Android Developers

这里有一个例子。(Android ViewFlipper Example

你在代码中制作一个custum动画(参见 res / anim 文件夹)

这是示例代码:

MainActivity.java代码:

package com.javacodegeeks.android.viewflipperapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {
    private ViewFlipper viewFlipper;
    private float lastX;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
    }

    // Using the following method, we will handle all screen swaps.
    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction()) {

        case MotionEvent.ACTION_DOWN: 
            lastX = touchevent.getX();
            break;
        case MotionEvent.ACTION_UP: 
            float currentX = touchevent.getX();

            // Handling left to right screen swap.
            if (lastX < currentX) {

                // If there aren't any other children, just break.
                if (viewFlipper.getDisplayedChild() == 0)
                    break;

                // Next screen comes in from left.
                viewFlipper.setInAnimation(this, R.anim.slide_in_from_left);
                // Current screen goes out from right. 
                viewFlipper.setOutAnimation(this, R.anim.slide_out_to_right);

                // Display next screen.
                viewFlipper.showNext();
             }

            // Handling right to left screen swap.
             if (lastX > currentX) {

                 // If there is a child (to the left), kust break.
                 if (viewFlipper.getDisplayedChild() == 1)
                     break;

                 // Next screen comes in from right.
                 viewFlipper.setInAnimation(this, R.anim.slide_in_from_right);
                // Current screen goes out from left. 
                 viewFlipper.setOutAnimation(this, R.anim.slide_out_to_left);

                // Display previous screen.
                 viewFlipper.showPrevious();
             }
             break;
         }
         return false;
    }
}

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#f5f5f5" >

        <ViewFlipper
            android:id="@+id/viewflipper"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="6dp" >


            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:text="Windows PC"
                    android:textColor="#b7102f"
                    android:textSize="25dp">
                </TextView>

                <ImageView
                    android:layout_marginTop="15dp"
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/windows_pc" />
            </LinearLayout>


            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:orientation="vertical" >

                <TextView
                    android:layout_marginTop="15dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Ubuntu PC"
                    android:textColor="#191975"
                    android:textSize="25dp"
                    android:textStyle="italic" >
                </TextView>

                <ImageView
                    android:layout_marginTop="15dp"
                    android:id="@+id/imageView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ubuntu_pc" />

            </LinearLayout>
        </ViewFlipper>
</LinearLayout>

以下所有代码都在 res / anim 目录中:

slide_in_from_left.xml(参见屏幕):

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="-100%"
        android:toXDelta="0%"
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:duration="1500" />
</set>

slide_out_to_left.xml(见屏幕外):

  <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
      <translate
        android:fromXDelta="0%"
        android:toXDelta="-100%"
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:duration="1500"/>
</set>

slide_in_from_right.xml(见屏幕):

 <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="100%"
        android:toXDelta="0%"
        android:fromYDelta="0%"
        android:toYDelta="0%"
        android:duration="1500" />
</set>

slide_out_to_right.xml(见屏幕外):

   <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
      <translate
          android:fromXDelta="0%"
          android:toXDelta="100%"
          android:fromYDelta="0%"
          android:toYDelta="0%"
          android:duration="1500"/>
</set>

答案 2 :(得分:0)

下载nineolddroidshttp://nineoldandroids.com/)图书馆并阅读文档...

您应该能够使用xxBy方法完成任务。

样本用法:

private void sortCardsAndShadowAnim() {
//        float bottom = getHeight();
    float activeCardTop;
    if (activeCardIndex == getChildCount() - 1) {
        activeCardTop = topHeaderDown;
        ViewPropertyAnimator.animate(getChildAt(activeCardIndex)).y(activeCardTop).setDuration(SORT_ANIMATION_DURATION).start();
        ViewPropertyAnimator.animate(shadows.get(activeCardIndex)).alpha(getShadowAlpha(activeCardTop)).setDuration(SORT_ANIMATION_DURATION).start();
    } else {
        for (int i = getChildCount() - 1; i > activeCardIndex + 1; i--) {
            ViewPropertyAnimator.animate(getChildAt(i)).y(bottom).setDuration(SORT_ANIMATION_DURATION).start();
            ViewPropertyAnimator.animate(shadows.get(i)).alpha(getShadowAlpha(bottom)).setDuration(SORT_ANIMATION_DURATION).start();
        }

此代码实际上通过Y轴移动视图(卡片),并在移动时将其淡出。