Android 以编程方式更新视图和切换视图时,ViewSwitcher不显示初始视图

Android 以编程方式更新视图和切换视图时,ViewSwitcher不显示初始视图,android,Android,下面是我正在试验的一个概念的测试项目。其目的是动态创建两个视图并从一个视图滑动到另一个视图 但是,ViewSwitcher从不显示初始视图 活动 布局XML 动画:在\u right.xml中滑动\u 动画:slide_out_left.xml 对于图片,我只使用了谷歌的鱼图片和火图片 问题:鱼的图像永远不可见:- 取而代之的是,视图为白色,火灾图像滑入。鱼从不溜出去 我该怎么做才能让鱼滑出呢?我已经弄明白了-解决办法是给观赏时间!别着急,哈哈! 下面是更新的mButton.setOnClick

下面是我正在试验的一个概念的测试项目。其目的是动态创建两个视图并从一个视图滑动到另一个视图

但是,ViewSwitcher从不显示初始视图

活动

布局XML

动画:在\u right.xml中滑动\u

动画:slide_out_left.xml

对于图片,我只使用了谷歌的鱼图片和火图片

问题:鱼的图像永远不可见:- 取而代之的是,视图为白色,火灾图像滑入。鱼从不溜出去


我该怎么做才能让鱼滑出呢?

我已经弄明白了-解决办法是给观赏时间!别着急,哈哈! 下面是更新的mButton.setOnClickListener的代码

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity {

    private ViewSwitcher mViewSwitcher;
    private Button mButton;
    private Animation mAnimSlideInRight;
    private Animation mAnimSlideOutLeft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher1);
        mButton = (Button) findViewById(R.id.button1);

        mAnimSlideInRight = AnimationUtils.loadAnimation(this, R.anim.slide_in_right);
        mAnimSlideOutLeft = AnimationUtils.loadAnimation(this, R.anim.slide_out_left);

        // Fish image
        final ImageView imageView1 = new ImageView(MainActivity.this);
        imageView1.setImageResource(R.drawable.fish_one);
        imageView1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        // Fire image
        final ImageView imageView2 = new ImageView(MainActivity.this);
        imageView2.setImageResource(R.drawable.fire);
        imageView2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));


        mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                mViewSwitcher.removeAllViews();

                mViewSwitcher.addView(imageView1, 0);
                mViewSwitcher.addView(imageView2, 1);

                mViewSwitcher.setInAnimation(mAnimSlideInRight);
                mViewSwitcher.setOutAnimation(mAnimSlideOutLeft);

                mViewSwitcher.showNext();
            }
        });

    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Flip" />

    <ViewSwitcher
        android:id="@+id/viewSwitcher1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </ViewSwitcher>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%" android:toXDelta="0%"
            android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>
<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0%" android:toXDelta="-100%"
            android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>
mButton.setOnClickListener(new OnClickListener()
{

    // Button callback
    @Override
    public void onClick(View v)
    {
        mViewSwitcher.post(new Runnable()
        {
            // Runnable that removes all views and places the images in the view
            @Override
            public void run()
            {
                mViewSwitcher.removeAllViews();

                mViewSwitcher.addView(imageView1, 0);
                mViewSwitcher.addView(imageView2, 1);

                // Nested Runnable that triggers the sliding animation
                mViewSwitcher.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        mViewSwitcher.setInAnimation(mAnimSlideInRight);
                        mViewSwitcher.setOutAnimation(mAnimSlideOutLeft);
                        mViewSwitcher.showNext();
                    }
                });// end of nested Runnable

            }
        });// end of Runnable

    }
});// end of OnClickListener