11

在一个活动中,我有以下内容:

var flipper = FindViewById<ViewFlipper>(Resource.Id.flipper);
flipper.Touch += flipper_Touch;

触摸处理程序的基本实现如下所示:

float oldTouchValue = 0;

void flipper_Touch(object sender, View.TouchEventArgs e)
{
    var flipper = sender as ViewFlipper;
    switch(e.Event.Action)
    {
        case MotionEventActions.Down:
            oldTouchValue = e.Event.GetX();
            break;

        case MotionEventActions.Up:
            float currentX = e.Event.GetX();
            if (oldTouchValue < currentX)
            {
                flipper.ShowNext();
            }
            else if (oldTouchValue > currentX)
            {
                flipper.ShowPrevious();
            }
            break;      
    }
}

这允许我在不同的视图之间导航,但我想让它向左/向右滑动

我已经看到了一些关于如何使其工作的 Java 示例,但不是将其转换为 c# 的直接方法。

使视图滑动需要什么,有没有办法定义animationin XML
我可以使用animations定义的 inXML和调用来使活动滑入和滑出OverridePendingTransition,但我不确定如何在这里应用这些知识。

4

2 回答 2

20

这允许我在不同的视图之间导航,但我想让它向左/向右滑动

The ViewFlipper has, through its ViewAnimator class, some methods to set the animation for the in and out actions, setInAnimation() and setOutAnimation(). This are in the Android SDK but should have correspondence in MonoDroid(with which unfortunately I'm not familiar so I could be wrong). In order to have the desired animation simply use the two methods above to set the desired animations(either xml file or programmatically built Animation) and then call the showNext/Previous methods like you currently do.

You even have some slide animation in the Android SDK, but again I don't know if they are present in MonoDroid.

Update: Those methods are indeed available in Monodroid and exposed like this:

//Using one of the built in animations:
flipper.setInAnimation(this, Android.Resource.Animation.SlideInLeft);
flipper.setOutAnimation(this, Android.Resource.Animation.SlideOutRight);

//Using custom animations defined in XML
flipper.setInAnimation(this, Resource.Animation.slide_in_right);
flipper.setOutAnimation(this, Resource.Animation.slide_out_left);
于 2013-03-04T11:36:13.667 回答
4

If you want to control ViewFlipper animation through your XML layout file, then add these attributes to the ViewFlipper tag-

    android:inAnimation="@android:anim/slide_out_right"
    android:outAnimation="@android:anim/slide_in_left"

This is a basic example in which the children inside the ViewFlipper slide in and slide out using the default animations provided by android.

You can also provide your own animation files by adding these attributes instead of the ones above-

    android:inAnimation="@anim/slide_in_right"
    android:outAnimation="@anim/slide_in_left"

and then creating these animation files-

In res/anim/slide_in_left.xml:

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

In res/anim/slide_in_right.xml:

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

If you would like to start this animation automatically, then add-

    android:flipInterval="2000"
    android:autoStart="true"

This will start the animation automatically and flip the images (or your views) in every 2 seconds (2000ms).

于 2020-06-15T09:45:17.393 回答