Touch Scroll on View Flipper in Android?

后端 未结 2 1094
别那么骄傲
别那么骄傲 2021-02-06 15:50

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.

2条回答
  •  暖寄归人
    2021-02-06 15:59

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

提交回复
热议问题