Touch Scroll on View Flipper in Android?

送分小仙女□ 提交于 2019-12-04 10:01:38

问题


I have to achieve that the Touch Scroll on the ViewFlipper. For Example. I have two Images. At First, ViewFlipper shows an First Image. Now I Flung the view from right to left. The First Image view Slide out left and the Second Slide in from Left. I can achieve it By this Post. But I want to Scroll the image. That is, on the Action_Move Event I want to do Touch Scroll. For Example, when I move the touch from right to left it will flung how much the touch moves. on that time the output should show both images partly.

How to do that? What I have to measure the Screen levels(height & width). Example codes are more helpful.


回答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;
        }
    }
}



回答2:


If you need to detect scroll on only viewflipper which is not occupying entire screen, then try the below

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;
        }
  });

and MyGestureDetector will be same as in http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/



来源:https://stackoverflow.com/questions/3202749/touch-scroll-on-view-flipper-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!