android左右滑动翻页查看图片

java部分代码

package com.xpc.onflingdemo;

import java.io.File;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MainActivity extends Activity implements // 手势事件监控接口
		android.view.GestureDetector.OnGestureListener {
	private GestureDetector gestureDetector = null;
	private ImageView img;
	private String filepath[];// 路径数组
	private int k = 0;

	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		File sd = Environment.getExternalStorageDirectory();
		String path = sd.getPath();// 获取当前手机储存路径storage
		filepath = new String[9];// 路径数组初始化
		for (int i = 0; i < filepath.length; i++) {
			filepath[i] = path + "/picturereceive/beautiful" + i + ".jpg"; // path后为手机图片路径运行,需要更改为自己手机中图片路径
		}
		img = (ImageView) findViewById(R.id.imageView1);
		gestureDetector = new GestureDetector(this); // 声明检测手势事件
		File file = new File(filepath[k]);// 设置初始背景图片
		if (file.exists()) {
			Bitmap bm = BitmapFactory.decodeFile(filepath[k]);
			img.setImageBitmap(bm);
		} else {
			System.out.println("<<<<<<<<<<<<<<<<<<<<" + filepath[k]);
			System.out.println("<<<<<<<<<<<<<<<<<< 120) { // 从左向右滑动(左进右出)
			if (k > 0) {
				k--;
				File file = new File(filepath[k]);
				if (file.exists()) {
					Bitmap bm = BitmapFactory.decodeFile(filepath[k]);
					img.setImageBitmap(bm);
				} else {
					System.out.println("<<<<<<<<<<<<<<<<<<
xml布局文件代码



    



注意manifest文件中添加手机读取权限

demo下载地址

你可能感兴趣的:(android)