在Android上的Viewflipper上触摸滚动?

时间:2010-07-08 10:43:04

标签: android viewflipper android-gallery

我必须在ViewFlipper上实现Touch Scroll。例如。我有两张图片。首先,ViewFlipper显示第一张图像。现在我从右到左掠过视野。第一个图像视图向左滑动,第二个幻灯片向左滑动。我可以通过Post来实现它。但我想滚动图像。也就是说,在Action_Move事件中我想做Touch Scroll。例如,当我从右向左移动触摸时,它会甩掉触摸移动的程度。在那个时候输出应该部分显示两个图像。

怎么做?我需要测量屏幕级别(高度和宽度)。示例代码更有帮助。

2 个答案:

答案 0 :(得分:1)

package com.appaapps.flipper;

import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ViewFlipper;
//------------------------------------------------------------------------------
// Flipper - Philip R Brenan at gmail.com
//------------------------------------------------------------------------------
public class FlipperActivity extends Activity {
    ViewFlipper f;
    DrawView a, b, c;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        f = new ViewFlipper(this);
        a = new DrawView(this, "aaaaa");
        b = new DrawView(this, "BBBBB");
        c = new DrawView(this, "ccccc");
        f.addView(a);
        f.addView(b);
        f.addView(c);
        setContentView(f);
    }
    //------------------------------------------------------------------------------
    // Draw 
    //------------------------------------------------------------------------------
    class DrawView extends View implements View.OnTouchListener {
        final String text;
        DrawView(Context Context, String Text) {
            super(Context);
            text = Text;
            setOnTouchListener(this);
        }
        public void onDraw(Canvas Canvas) {
            super.onDraw(Canvas);
            Paint p = new Paint();
            p.setColor(0xffffffff);
            p.setTextSize(20);
            Canvas.drawText(text, 0, 20, p);
        }
        public boolean onTouch(View v, MotionEvent event) {
            final int a = event.getAction();
            if (a == MotionEvent.ACTION_DOWN) {
                final int i = f.getDisplayedChild(), n = f.getChildCount();
                f.setDisplayedChild((i + 1) % n);
            }
            return true;
        }
    }
}

答案 1 :(得分:0)

如果您需要仅检测不占整个屏幕的viewflipper上的滚动,请尝试以下

gestureDetector = new GestureDetector(new MyGestureDetector());

viewFlipper.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return false;
            }
            return true;
        }
  });

和MyGestureDetector将与http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/

中的相同