4

我是 Android 开发的新手,我的要求是当应用程序启动时,会显示一个空白屏幕。当用户滑动屏幕时,背景颜色应该会改变。

我不知道如何实现这一点。以下是我到目前为止所拥有的。

import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Color;
    import android.view.GestureDetector;
    import android.view.GestureDetector.OnGestureListener;
    import android.view.Menu;
    import android.view.MotionEvent;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    public class MainActivity extends Activity implements OnGestureListener {    
        private LinearLayout main;    
        private TextView viewA;

        private GestureDetector gestureScanner;

        @Override
        public boolean onTouchEvent(MotionEvent me) {
            return gestureScanner.onTouchEvent(me);

        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public boolean onDown(MotionEvent e) {
            viewA.setText("-" + "DOWN" + "-");
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            viewA.setText("-" + "FLING" + "-");
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            viewA.setText("-" + "LONG PRESS" + "-");
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            viewA.setText("-" + "SCROLL" + "-");
            return true;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            viewA.setText("-" + "SHOW PRESS" + "-");
        }    

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            viewA.setText("-" + "SINGLE TAP UP" + "-");
            return true;
        }

        public LinearLayout getMain() {
            return main;
        }

        public void setMain(LinearLayout main) {
            this.main = main;
        }

    }
4

3 回答 3

0

使您的布局文件像这样..

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

现在在您的 MAinActivity 中使用以下代码..

public class MainActivityextends Activity implements android.gesture.GestureOverlayView.OnGestureListener {
GestureDetector gd;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);


setContentView(R.layout.image);
gd= new GestureDetector((OnGestureListener) ImageEditing.this);
    LinearLayout layout = (LinearLayout)findViewById(R.id.ll_main);
    layout.setOnTouchListener(new OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub

                    return true;
                }
            });
}
}

然后覆盖onfling方法...

@Override
public boolean onFling(MotionEvent start, MotionEvent finish,
            float xVelocity, float yVelocity) {
 ll.setBackground(yourDrawable);


    }

希望它有效...

于 2013-04-16T13:00:33.497 回答
0

这可能对您有所帮助

      `<?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
        android:layout_height="match_parent"
       android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.50" >

    <Button
        android:id="@+id/btnPrev"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="&lt; Prev" />

    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Next >" />

    <ViewFlipper
        android:id="@+id/viewFlipper1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/btnNext" >

        <ImageView
            android:layout_width="170dp"
            android:layout_height="459dp"
            android:layout_gravity="center_horizontal"
            android:contentDescription="five"
            android:src="@drawable/hair_five" />

        <ImageView
            android:layout_width="170dp"
            android:layout_height="459dp"
            android:layout_gravity="center_horizontal"
            android:contentDescription="seven"
            android:src="@drawable/hair_seven" />

        <ImageView
            android:layout_width="170dp"
            android:layout_height="459dp"
            android:contentDescription="one"
            android:src="@drawable/hair_one" />

        <ImageView
            android:layout_width="170dp"
            android:layout_height="459dp"
            android:contentDescription="two"
            android:src="@drawable/hair_two" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="170dp"
            android:layout_height="459dp"
            android:contentDescription="three"
            android:src="@drawable/hair_three" />

        <ImageView
            android:layout_width="170dp"
            android:layout_height="459dp"
            android:contentDescription="four"
            android:src="@drawable/hair_four" />

        <ImageView
            android:layout_width="170dp"
            android:layout_height="459dp"
            android:contentDescription="six"
            android:src="@drawable/hair_six" />
    </ViewFlipper>



</RelativeLayout>

`

你的代码将是:

  public class FlipSlide extends Activity implements OnGestureListener{
GestureDetector gDetector;
protected void yourFlipperForward() {
    // Set animation
    tflipper.setAnimation(AnimationUtils.loadAnimation(
            FlipSlide.this, android.R.anim.slide_in_left));

    // Show next step
    tflipper.showNext();

}

protected void yourFlipperBack() {
    // Set animation
    tflipper.setAnimation(AnimationUtils.loadAnimation(
            FlipSlide.this, android.R.anim.slide_in_left));

    // Show next step
    tflipper.showPrevious();

}

private ViewFlipper tflipper;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.flip_slide);

    tflipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
    tflipper.startFlipping();
    gDetector = new GestureDetector((OnGestureListener) this);
    Button bp=(Button)findViewById(R.id.btnPrev);
    bp.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            flipPrevious( arg0);

        }
    });
    Button bn=(Button)findViewById(R.id.btnNext);
    bn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            flipNext( arg0);

        }
    });
}
public boolean onFling(MotionEvent start, MotionEvent finish, float xVelocity,              float yVelocity) {

        if (start.getRawY() < finish.getRawY()) {
            yourFlipperForward();


        } else {

            yourFlipperBack();    
        }

        return true;

}
@Override

public boolean onTouchEvent(MotionEvent me) {

return gDetector.onTouchEvent(me);

}
public void close(View v) {
    finish();
}

public void flipNext(View v) {
    yourFlipperForward();
}

public void flipPrevious(View v) {
    yourFlipperBack();
}

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

@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;
}

   }
于 2013-04-16T12:06:11.363 回答
0

你的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Shake to get a toast and to switch color" />

</LinearLayout> 

现在您可以在您的 onCreate() 中设置颜色如下

View view = findViewById(R.id.textView);
view.setBackgroundColor(Color.BLACK);

现在在你的 onFling()

view.setBackgroundColor(Color.GREEN);
于 2013-04-16T12:48:07.643 回答