开发者

Android question - how to prep 100 images to be shown via Fling/Swipe?

I'm totally new to this, been tinkering around for a week.

Came up with a simple image viewer app for 2 images.

Feature: Left and right swipes will switch the images. Dead simple.

What i'd like to do: Have up to 100 images.

note: All my images are in my res/drawable folder.

They're named image1.png to image100.png

I obviously don't want to do:

    ImageView i = new ImageView(this);
    i.setImageResource(R.drawable.image1);
    viewFlipper.addView(i);
    ImageView i2 = new ImageView(this);
    i2.setImageResource(R.drawable.image2);
    viewFlipper.addView(i2);
    ImageView i3 = new ImageView(this);
    i3.setImageResource(R.drawable.image3);
    viewFlipper.addView(i3);

all the way to i100.

how do I make this into a loop, which is flexible and reads everything from the drawable folder ( and not be limited to 100 images)?

source:

    public class ImageViewTest extends Activity {
        private static final String LOGID = "CHECKTHISOUT";

        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_MAX_OFF_PATH = 250;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;
        private GestureDetector gestureDetector;
        View.OnTouchListener gestureListener;

        private Animation slideLeftIn;
        private Animation slideLeftOut;
        private Animation slideRightIn;
        private Animation slideRightOut;
        private ViewFlipper viewFlipper;

        String message = "Initial Message"; 
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            //Set up viewflipper
            viewFlipper = new ViewFlipper(this);       
            ImageView i = new ImageView(this);
            i.setImageResource(R.drawable.sample_1);
            ImageView i2 = new ImageView(this);
            i2.setImageResource(R.drawable.sample_2);
            viewFlipper.addView(i);
            viewFlipper.addView(i2);

            //set up animations
            slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
            slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
            slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
            slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);

            //put up a brownie as a starter
            setContentView(viewFlipper);

            gestureDetector = new GestureDetector(new MyGestureDetector());
        }

        public class MyGestureDetector extends SimpleOnGestureListener {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                try {
                    if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                        return false;
                    // right to left swipe
                    if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        Log.v(LOGID,"right to left swipe detected");
                        viewFlipper.setInAnimation(slideLeftIn);
                        viewFlipper.setOutAnimation(slideLeftOut);
                        viewFlipper.showNext();
                        setContentView(viewFlipper);

                    } // left to right swipe 
                    else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        Log.v(LOGID,"left to right swipe detected");                    
                        viewFlipper.setInAnimation(slideRightIn开发者_C百科);
                        viewFlipper.setOutAnimation(slideRightOut);
                        viewFlipper.showPrevious();
                        setContentView(viewFlipper);

                    }
                } catch (Exception e) {
                    // nothing
                }
                return false;
            }
        }

        // This doesn't work
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)){
                Log.v(LOGID,"screen touched");
                return true;
            }
            else{
                return false;
            }
        }
    }


without flippers I rewrte your code like this and works like charm for me.

package com.mayuonline.swipe;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class SwipetestActivity extends Activity{
    private static final String LOGID = "CHECKTHISOUT";
    public int[] list = {R.drawable.a,R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e };
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;
    public int i = 0;

    String message = "Initial Message"; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        ImageView imageview = (ImageView)findViewById(R.id.imageView1);
        imageview.setImageResource(list[0]);

        gestureDetector = new GestureDetector(new MyGestureDetector());
    }

    public class MyGestureDetector extends SimpleOnGestureListener implements OnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    ImageView imageview = (ImageView)findViewById(R.id.imageView1);
                    if(list.length>i){
                        i++;
                    imageview.setImageResource(list[i]);
                    }
                    Log.v(LOGID,"right to left swipe detected");


                } // left to right swipe 
                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    ImageView imageview = (ImageView)findViewById(R.id.imageView1);
                    if(i>0){
                        i--;
                    imageview.setImageResource(list[i]);
                    }
                    Log.v(LOGID,"left to right swipe detected");                    


                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }

    // This doesn't work
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event)){
            Log.v(LOGID,"screen touched");
            return true;
        }
        else{
            return false;
        }
    }

}


What you need to do - is a write an adapter which will load the image and returs it on getview. Example of how to do it here - http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/Animation2.html.


I think what you need is not a ViewFlipper, what you need is a PageViewer. With 50 images works like a charm on my Galaxy S!

With Mayu's code the images are changed with animation, but until the gesture is finished (finger is removed from the screen) the animation does not start. With PageViewer the animation follows the position of your finger.

PageViewer is not in the Android SDK, but it is included in the Android Support Package.

In this link you can find more information:

http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜