2 liguu liguu 于 2014.03.18 11:57 提问

ViewFlipper做图片滑动程序闪退

在网上找了一个代码 测试的时候只是五张图片没问题 当我多加几张的时候问题出来了 部署到手机上的时候程序一打开就自动关闭了

代码:

src\com.example.demo
MainActivity.java

package com.example.demo;

import com.example.demo.R.drawable;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class MainActivity extends Activity implements OnGestureListener {

private int[] imgs = { R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4,
        R.drawable.img5, R.drawable.img6 , R.drawable.img7, 
         };  //这里再加一张图片的话程序就会闪退了 我在模拟器上真机上都试了 求大神帮忙解答  小弟感激不尽
private GestureDetector gestureDetector;  
private ViewFlipper viewFlipper;  
private Activity mActivity;  

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
    mActivity = this;  
    viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);  

    gestureDetector = new GestureDetector(this);  

    for (int i = 0; i < imgs.length; i++) { // 添加图片源  
        ImageView iv = new ImageView(this);  
        iv.setImageResource(imgs[i]);  
        iv.setScaleType(ImageView.ScaleType.FIT_XY);  
        viewFlipper.addView(iv, new LayoutParams(LayoutParams.FILL_PARENT,  
                LayoutParams.FILL_PARENT));  
    }  

    viewFlipper.setAutoStart(false); // 设置自动播放功能(点击事件,前自动播放)  
    viewFlipper.setFlipInterval(3000);  
    if (viewFlipper.isAutoStart() && !viewFlipper.isFlipping()) {  
        viewFlipper.startFlipping();  
    }  
}  

@Override  
public boolean onTouchEvent(MotionEvent event) {  
    // TODO Auto-generated method stub  
    viewFlipper.stopFlipping(); // 点击事件后,停止自动播放  
    viewFlipper.setAutoStart(true);//设置是否自动播放  
    return gestureDetector.onTouchEvent(event); // 注册手势事件  
}  

@Override  
public boolean onDown(MotionEvent arg0) {  
    // TODO Auto-generated method stub  
    return false;  
}  

@Override  
public boolean onFling(MotionEvent e1, MotionEvent e2, float arg2,  
        float arg3) {  
    if (e2.getX() - e1.getX() > 120) { // 从左向右滑动(左进右出)  
        Animation rInAnim = AnimationUtils.loadAnimation(mActivity,  
                R.anim.push_right_in); // 向右滑动左侧进入的渐变效果(alpha 0.1 -> 1.0)  
        Animation rOutAnim = AnimationUtils.loadAnimation(mActivity,  
                R.anim.push_right_out); // 向右滑动右侧滑出的渐变效果(alpha 1.0 -> 0.1)  

        viewFlipper.setInAnimation(rInAnim);  
        viewFlipper.setOutAnimation(rOutAnim);  
        viewFlipper.showPrevious();  
        return true;  
    } else if (e2.getX() - e1.getX() < -120) { // 从右向左滑动(右进左出)  
        Animation lInAnim = AnimationUtils.loadAnimation(mActivity,  
                R.anim.push_left_in); // 向左滑动左侧进入的渐变效果(alpha 0.1 -> 1.0)  
        Animation lOutAnim = AnimationUtils.loadAnimation(mActivity,  
                R.anim.push_left_out); // 向左滑动右侧滑出的渐变效果(alpha 1.0 -> 0.1)  

        viewFlipper.setInAnimation(lInAnim);  
        viewFlipper.setOutAnimation(lOutAnim);  
        viewFlipper.showNext();  
        return true;  
    }  
    return true;  
}  

@Override  
public void onLongPress(MotionEvent arg0) {  
    // TODO Auto-generated method stub  

}  

@Override  
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,  
        float arg3) {  
    // TODO Auto-generated method stub  
    return false;  
}  

@Override  
public void onShowPress(MotionEvent arg0) {  
    // TODO Auto-generated method stub  

}  

@Override  
public boolean onSingleTapUp(MotionEvent arg0) {  
    // TODO Auto-generated method stub  
    return false;  
}  

}

1个回答

qq_32402705
qq_32402705   2015.10.29 13:06

楼主解决了吗?同问啊

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!