其他分享
首页 > 其他分享> > Android问题-如何准备通过Fling / Swipe显示100张图像?

Android问题-如何准备通过Fling / Swipe显示100张图像?

作者:互联网

我完全陌生,整整一周了.

有了一个用于2张图像的简单图像查看器应用程序.

特征:
左右滑动将切换图像.死了很简单.

我想做什么:
最多有100张图像.

注意:
我所有的图像都在我的res / drawable文件夹中.
它们被命名为image1.png至image100.png

我显然不想做:

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

一直到i100.

我如何使其变成一个灵活的循环并从drawable文件夹读取所有内容(而不限于100张图像)?

资源:

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

解决方法:

没有脚蹼,我这样重写了您的代码,对我来说就像魅力.

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

}

标签:android-widget,android
来源: https://codeday.me/bug/20191210/2100752.html