android 使用左右滑动更改图像

Tho*_*mas 1 android uiimageview

我正在制作一个 Android 应用程序,每当应用程序运行时,就会出现一个包含多个组件的活动屏幕。现在,这项活动有几个不同的组成部分。其中之一是图像视图,显示图库中的不同图像。目前,这些图像通过触摸手势来改变。但我想使用左右滑动来实现相同的效果。这是我的代码:

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;


import java.util.Collection;

public class pictures extends ContentResolverAnimatedViewContainer 
{
    public static final String IMAGE_NAME = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera";
    public static final String IMAGE_ID = getBucketId(CAMERA_IMAGE_BUCKET_NAME);
    public static String getBucketId(String path) 
    {
        return String.valueOf(path.toLowerCase().hashCode());   }
    static Context cc;
    String cp;
    int picheight;

    public PhotoExample(final Context context) {
        super(context, 9);
        cc = context;
        picheight = getResources().getDimensionPixelSize(R.dimen.photo_example_height);
        final String[] projection = { Images.Media.DATA };
        final String selection = Images.Media.BUCKET_ID + " = ?";
        final String[] selectionArgs = { IMAGE_ID };
        createContentResolverLoop(Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null);   }
    @Override
    public void onCreateData(ContentResolver contentResolver, Cursor dataCursor) {
        final int column_data = dataCursor.getColumnIndexOrThrow(Images.Media.DATA);
        cp = dataCursor.getString(column_data); }

    @Override
    public void onCreateViewContent(LayoutInflater layoutInflater, ViewGroup parentGroup, View[] containerViews, int index) {
        containerViews[index] = layoutInflater.inflate(R.layout.example_photo, parentGroup, false);
        ImageView photoView = (ImageView)containerViews[index].findViewById(R.id.photo);
        photoView.setImageBitmap(decodeSampledBitmapFromFile(cp, 0, picheight));    }

    public static Bitmap decodeSampledBitmapFromFile(String path, int Width, int Height) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        options.Size = calculateInSampleSize(options, Width, Height);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);}

    public static int calculateInSampleSize(BitmapFactory.Options options, int Width, int Height) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int Size = 1;

        if (height > reqHeight || width > reqWidth) {
            final int height_half = height / 2;
            final int width_half = width / 2;

            while ((height_half / Size) > Height && (width_half / Size) > Width) {
                Size = Size * 2;
            }    }

        return Size;
    }


    @Override
    public void cleanup() { }

}
Run Code Online (Sandbox Code Playgroud)

有什么想法我应该如何实施?

谢谢。

小智 5

import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener (Context ctx){
    gestureDetector = new GestureDetector(ctx, new GestureListener());
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
                result = true;
            } 
            else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
                result = true;

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
}
}
Run Code Online (Sandbox Code Playgroud)

然后使用:

yourView.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeTop() {
    Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight() {
    Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
}
public void onSwipeLeft() {
    Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
}
public void onSwipeBottom() {
    Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
}

public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}
});
Run Code Online (Sandbox Code Playgroud)