更光滑的ImageSwitcher - 如何?

时间:2013-08-10 20:23:30

标签: android swipe imageswitcher

我正在尝试一个简单的项目,您只需在图像之间滚动,但现在动画真的很糟糕。

当我滑动以在图像之间切换时,当我滑动时它不会“附着”到我的手指上,而是笨拙地淡出并且下一个图像滑动。我认为它与我使用的动画参数有关,但我真的不知道还有什么可以使用。

我想要实现的是在应用程序中的选项卡之间水平滚动,而一个图像滑出,另一个图像在它旁边滑动,就像它们相互连接一样。

以下是代码:

的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageSwitcher
        android:id="@+id/splashScreenImages"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
</ImageSwitcher>

</RelativeLayout>

的java:

package com.test.imageswitchertest02;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity implements ViewFactory {

    ImageSwitcher splashSwitcher;
    Integer[] screensList = { R.drawable.splash_screen1, R.drawable.splash_screen2, R.drawable.splash_screen3 };
    int curIndex = 0;
    int downX, upX;

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

        splashSwitcher = (ImageSwitcher) findViewById(R.id.splashScreenImages);
        splashSwitcher.setFactory(this);
        splashSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        splashSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));

        splashSwitcher.setImageResource(screensList[curIndex]);
        splashSwitcher.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    downX = (int) event.getX();
                    Log.i("event.getX()", " downX " + downX);
                    return true;
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    upX = (int) event.getX();
                    Log.i("event.getX()", "upX" + downX);

                    if (upX - downX > 100) {
                        // curIndex is the current image index being viewed
                        curIndex--;
                        if (curIndex < 0) {
                            curIndex = screensList.length - 1;
                        }

                        splashSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_in_left));
                        splashSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.slide_out_right));

                        splashSwitcher.setImageResource(screensList[curIndex]);
                    } else if (downX - upX > -100) {
                        curIndex++;
                        if (curIndex > 2) {
                            curIndex = 0;
                        }
                        splashSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.slide_in_right));
                        splashSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.slide_out_left));

                        splashSwitcher.setImageResource(screensList[curIndex]);

                    }

                    return true;
                }
                return false;
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public View makeView() {
        ImageView i = new ImageView(this);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        return i;
    }

}

0 个答案:

没有答案