我是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;
}
}
答案 0 :(得分:0)
这可能会帮助你在这里使用视图鳍状肢在一组imageViews中翻转,在你的情况下,图像视图的高度和宽度将被设置为fill_parent,它们的背景颜色将由你给出
`<?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="< 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;
}
}
答案 1 :(得分: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);
答案 2 :(得分: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);
}
希望它有效......