-1

我想通过滑动图像来进行改变背景的主要活动,并且在左侧我想放置一个包含 4 个按钮的按钮栏以移动到 4 个不同的活动,所以我编写了以下代码:

public class MainActivity extends Activity {
Button[]  buttons = new Button[4]; 
SharedPreferences fsc;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private ViewFlipper mViewFlipper;   
private Context mContext;

private final GestureDetector detector = new SwipeGestureDetector();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.activity_main);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            addListenerOnButton();
    setContentView(R.layout.activity_main);
    mContext = this;
    mViewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);
    mViewFlipper.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(final View view, final MotionEvent event) {
            detector.onTouchEvent(event);
            return true;
        }
    });
}
private void addListenerOnButton() {
    buttons[0] = (Button) findViewById(R.id.slide);

    buttons[0].setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            Intent myIntent = new Intent(MainActivity.this,
                    slideshow.class);
            startActivity(myIntent);
    // TODO Auto-generated method stub
        }
    }

    );
class SwipeGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
                mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));                  
                mViewFlipper.showNext();
                return true;
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
                mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.right_out));
                mViewFlipper.showPrevious();
                return true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }
}

}

4

1 回答 1

0

您没有import使用所需的库类。这个问题可能会有所帮助。

看起来您是在基本的文本编辑器中手动执行此操作。使用像 Eclipse 这样的 IDE(它有很好的 Android 工具)将使您不必手动完成所有这些工作。

于 2013-08-10T13:22:30.263 回答