如何设置" toXDelta"翻译从Java code动画的价值? [英] How can I set "toXDelta" translate animation value from java code?

查看:255
本文介绍了如何设置" toXDelta"翻译从Java code动画的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以设置从Java code toXDelta价值?我需要先计算它,所以我不能只在XML中使用的最终值。我可以用动画()。translateX对我的看法,但我就不能让动画减慢,直到最后..任何想法?

how can I set toXDelta value from java code? I need to calculate it first so I can't just use final value in xml. I could use animate().translateX on my view but then I can't make animation slowing down till the end.. any ideas?

推荐答案

您可以使用 Viewfiliper 方法。 Refrence是:<一href=\"https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0ahUKEwjGiYq5scLJAhUEYg8KHdBnAOIQFggpMAE&url=http%3A%2F%2Fdeveloper.android.com%2Freference%2Fandroid%2Fwidget%2FViewFlipper.html&usg=AFQjCNFul8SdYdyMNKlvzLqfN4_-fqfhgg\"相对=nofollow> ViewFlipper | Android开发者

You can use Viewfiliper Method. Refrence is : ViewFlipper | Android Developers

有是在这样的一个例子。(<一href=\"http://examples.java$c$cgeeks.com/android/core/widget/viewflipper/android-viewflipper-example/\"相对=nofollow> Android的ViewFlipper例)

There is an example in this.(Android ViewFlipper Example)

和你做一个动画俗在code(请参阅 RES /阿尼姆文件夹)

And you make a custum animation in your code(See res/anim Folder)

这是例子code:

该MainActivity.java code:

The MainActivity.java code:

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>

所有娄code是到 RES /阿尼姆目录:

slide_in_from_left.xml(见到屏幕):

slide_in_from_left.xml (see into the screen):

<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(见出画面):

slide_out_to_left.xml (see out of screen):

  <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(见到屏幕):

slide_in_from_right.xml (see into the screen):

 <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(见出画面):

slide_out_to_right.xml (see out of screen):

   <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>

这篇关于如何设置&QUOT; toXDelta&QUOT;翻译从Java code动画的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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