许你壹世静好岁月 2014-06-30 05:49 采纳率: 100%
浏览 981

求大神解答滑动屏幕切换图片,划多长时间就一直切换,手势移开了就停止滑动

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]);

}

}

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?