问题描述
- 求大神解答滑动屏幕切换图片,划多长时间就一直切换,手势移开了就停止滑动
-
package com.exsample.clonn;import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;public class MainActivity extends Activity implements OnTouchListener,
OnGestureListener {
private GestureDetector detector = new GestureDetector(this);
int[] girls = new int[] { R.drawable.shapan000, R.drawable.shapan001,
R.drawable.shapan002, R.drawable.shapan003, R.drawable.shapan004,
R.drawable.shapan005, R.drawable.shapan006, R.drawable.shapan007,
R.drawable.shapan008, R.drawable.shapan009, R.drawable.shapan010,
R.drawable.shapan011, R.drawable.shapan012, R.drawable.shapan013,
R.drawable.shapan014, R.drawable.shapan015, R.drawable.shapan016,
R.drawable.shapan017, R.drawable.shapan018, R.drawable.shapan019,
R.drawable.shapan020, R.drawable.shapan021, R.drawable.shapan022,
R.drawable.shapan023, R.drawable.shapan024, R.drawable.shapan025,
R.drawable.shapan026, R.drawable.shapan027, R.drawable.shapan028,
R.drawable.shapan029, R.drawable.shapan030, R.drawable.shapan031,
R.drawable.shapan032, R.drawable.shapan033, R.drawable.shapan034,
R.drawable.shapan035, R.drawable.shapan036, R.drawable.shapan037,
R.drawable.shapan038, R.drawable.shapan039, R.drawable.shapan040,
R.drawable.shapan041, R.drawable.shapan042, R.drawable.shapan043,
R.drawable.shapan044, R.drawable.shapan045, R.drawable.shapan046,
R.drawable.shapan047, R.drawable.shapan048, R.drawable.shapan049,
R.drawable.shapan050, R.drawable.shapan051, R.drawable.shapan052,
R.drawable.shapan053, R.drawable.shapan054, R.drawable.shapan055,
R.drawable.shapan056, R.drawable.shapan057, R.drawable.shapan058,
R.drawable.shapan059, R.drawable.shapan060, };
private int index;
private ImageView image;@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.myImg); image.setImageResource(girls[index]); image.setOnTouchListener(this); image.setLongClickable(true); detector.setIsLongpressEnabled(true); } public void goNext() { index++; index = Math.abs(index % girls.length); image.setImageResource(girls[index]); } @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_MOVE) { } return true; } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } public void onLongPress(MotionEvent e) { this.goNext(); } public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (velocityX < 0) { goNext(); } else if (velocityX > 0) { goPrevious(); } return false; } private void goPrevious() { index--; index = Math.abs(index % girls.length); image.setImageResource(girls[index]); }
}