
 AI时代,找源码已成为过去式,纪念我过去的十年0(回)
                        65天前
AI时代,找源码已成为过去式,纪念我过去的十年0(回)
                        65天前 还有人吗2(回)
                        83天前
还有人吗2(回)
                        83天前 会python真的可以为所欲为0(回)
                        563天前
会python真的可以为所欲为0(回)
                        563天前 这里还有人吗2(回)
                        748天前
这里还有人吗2(回)
                        748天前 这里还有人吗0(回)
                        748天前
这里还有人吗0(回)
                        748天前 每天面对着电脑屏幕,敲打键盘。我所面对的并不只是代码,而是一种生活方式。0(回)
                        972天前
每天面对着电脑屏幕,敲打键盘。我所面对的并不只是代码,而是一种生活方式。0(回)
                        972天前package com.demo;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
/**
 * gallery 图片循环播放,自动播放,无惯性(一次滑动一张图)
 * 
 * @author Feng
 * 
 */
public class CycleImageActivity extends Activity {
	public static int[] picture = { R.drawable.gallery1, R.drawable.gallery2,
			R.drawable.gallery3, R.drawable.gallery4 };
	private MyGallery pictureGallery = null;
	private int index = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.pictureGallery = (MyGallery) findViewById(R.id.mygallery);
		ImageAdapter adapter = new ImageAdapter(this);
		this.pictureGallery.setAdapter(adapter);
		Timer timer = new Timer();
		timer.schedule(task, 7000, 7000);
	}
	/**
	 * 定时器,实现自动播放
	 */
	private TimerTask task = new TimerTask() {
		@Override
		public void run() {
			Message message = new Message();
			message.what = 2;
			index = pictureGallery.getSelectedItemPosition();
			index++;
			handler.sendMessage(message);
		}
	};
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 2:
				pictureGallery.setSelection(index);
				break;
			default:
				break;
			}
		}
	};
	/**
	 * 自定义图片显示适配器
	 * 
	 */
	class ImageAdapter extends BaseAdapter {
		private Context context;
		public ImageAdapter(Context context) {
			this.context = context;
		}
		public int getCount() {
			return Integer.MAX_VALUE;
		}
		public Object getItem(int position) {
			return position;
		}
		public long getItemId(int position) {
			return position;
		}
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(context);
			imageView.setImageResource(picture[position % picture.length]);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setLayoutParams(new Gallery.LayoutParams(
					Gallery.LayoutParams.FILL_PARENT,
					Gallery.LayoutParams.FILL_PARENT));
			return imageView;
		}
	}
}
	MyGallery.java
package com.demo;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;
/**
 * 自定义画廊,实现一次只滑动一张图片,无惯性
 * 
 * @author Feng
 * 
 */
class MyGallery extends Gallery {
	public MyGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	public MyGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	public void setImageActivity(CycleImageActivity context) {
	}
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		int kEvent;
		if (isScrollingLeft(e1, e2)) {
			kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
		} else {
			kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
		}
		onKeyDown(kEvent, null);
		if (this.getSelectedItemPosition() == 0) {// 实现后退功能
			this.setSelection(CycleImageActivity.picture.length);
		}
		return false;
	}
	private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
		return e2.getX() > e1.getX();
	}
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return super.onScroll(e1, e2, distanceX, distanceY);
	}
}
		
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <com.demo.MyGallery
        android:id="@+id/mygallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:spacing="30dp"
        android:unselectedAlpha="1" />
</RelativeLayout>





初级程序员
by: 小蘑菇 发表于:2013-05-05 23:53:29 顶(0) | 踩(0) 回复
收藏,备用
回复评论