关闭

Android 之 Galley, 手指滑动翻看图片

3043人阅读 评论(3) 收藏 举报
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ethan.activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
       	>
        <activity android:name=".MyActivity"
            android:label="@string/app_name">
        
          <intent-filter>
			<action android:name="android.intent.action.MAIN"/>
			<category android:name="android.intent.category.LAUNCHER"/>              
          </intent-filter>
        </activity>
        
    </application>

</manifest>


布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true">
    </ImageSwitcher>
	 <Gallery android:id="@+id/gallery" 
	     android:layout_width="fill_parent"  
        android:layout_height="60dp" 
        android:background="#55000000"  
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:gravity="center_vertical"  
        android:spacing="16dp" />  
</RelativeLayout>

package com.ethan.activity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
/**
 * 供Gallery调用
 * @author ETHAN
 *
 */
public class ImageAdapter extends BaseAdapter {
	
	private Context context;
	/*public static Integer[] imageIDs = {
		
	};*/
	
	public static List<Integer> imgs1 = new ArrayList<Integer>();
	public static List<Integer> imgs2= new ArrayList<Integer>();
	//利用反射 读取 res/drawable 下边的图片ID
	static {
		/*
		 * public final class R {
			    public static final class attr {
			    }
			    public static final class drawable {
				        public static final int ic_launcher=0x7f020000;
				        public static final int image1=0x7f020001;
				        public static final int image2=0x7f020002;
				        public static final int image3=0x7f020003;
				        public static final int image4=0x7f020004;
				        public static final int image5=0x7f020005;
    			}
		 */
		
		Pattern p1= Pattern.compile("sample_thumb_\\d");
		Pattern p2 = Pattern.compile("sample_\\d");
	
		Field[] fields = R.drawable.class.getDeclaredFields();
		
		for(Field field:fields) {
			if(p1.matcher(field.getName()).matches()) {
				try {
					//保存图片ID
					imgs1.add( field.getInt(R.drawable.class));
				} catch (Exception e) {
					e.printStackTrace();
				}
				//保存图片ID
			} else if(p2.matcher(field.getName()).matches()) {
				try {
					imgs2.add( field.getInt(R.drawable.class));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public ImageAdapter(Context context) {
		super();
		this.context = context;
	}
	
	@Override
	public int getCount() {
		//小图片
		return imgs1.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView image = new ImageView(context);
		//小图片
		image.setImageResource(imgs1.get(position));
		image.setAdjustViewBounds(true);
		image.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		image.setScaleType(ImageView.ScaleType.FIT_CENTER);
		return image;
	}

}

package com.ethan.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

/**
 * viewFactory ---->makeView()
 * OnItemSelectedListener------->onItemSelected()
 * @author ETHAN
 *
 */
public class MyActivity extends Activity implements ViewFactory, OnItemSelectedListener {
	private ImageSwitcher is;
	private Gallery g;
	
	//选中的索引
	private int selected = 0;
	//手势向上向下 落点 x 坐标
    private int upX,downX;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//全屏设置
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.main);
		
		//得到ImageSwitcher对象
		is = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
		//实现并设置工厂内部接口的makeView方法,用来显示视图。
		is.setFactory(MyActivity.this);
		
		is.setOnTouchListener(touchListener);
		//设置图片来源
		g = (Gallery) findViewById(R.id.gallery);
		g.setAdapter(new ImageAdapter(MyActivity.this));
		g.setOnItemSelectedListener(this);
		
		//设置切入动画
		is.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in));
		//设置切出动画
		is.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_out));
		
		
		
	}

	OnTouchListener touchListener = new OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			
			if(event.getAction()==MotionEvent.ACTION_DOWN) {
				//取得 按下时的 x坐标
				downX = (int) event.getX();
				//True if the listener has consumed the event, false otherwise.
				//我的理解是:如果event销毁了,结束了一个时间,就返回true;下一个event is new
				return true;
			} else if(event.getAction()==MotionEvent.ACTION_UP) {
				//取得 松开时的 x坐标
				upX = (int)event.getX();
				//手指 向右划, 看前一张
				if(upX-downX>100) {
					//如果是第一张,则给出 最后一张照片(前一张)
					if(g.getSelectedItemPosition()==0) {
						selected = g.getCount()-1;
					} else {
						selected = g.getSelectedItemPosition() - 1; // 上一张
					}
				} else if(downX - upX>100) {
					//手指 向左划, 看下一张
					
					if(g.getSelectedItemPosition()==g.getCount()-1) {
						selected = 0;
					} else {
						selected = g.getSelectedItemPosition() + 1;// 下一张
					}
				}
				
				//int position, boolean animate
				g.setSelection(selected,true);
				return true;
			}
			return false;
		}
	};
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		is.setImageResource(ImageAdapter.imgs2.get(position));
		selected = position;
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		
	}

	@Override
	public View makeView() {
		ImageView i = new ImageView(this);
		i.setBackgroundColor(0xFF000000);
		i.setScaleType(ImageView.ScaleType.FIT_CENTER);
		i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		return i;
	}
	
	
}


0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:613763次
    • 积分:7123
    • 等级:
    • 排名:第3139名
    • 原创:183篇
    • 转载:80篇
    • 译文:0篇
    • 评论:123条
    文章分类
    最新评论