gpt4 book ai didi

android - 通过触摸更改屏幕背景颜色

转载 作者:太空宇宙 更新时间:2023-11-03 11:16:28 24 4
gpt4 key购买 nike

我是Android开发的新手,我的需求是App启动时黑屏。当用户滑动屏幕时,背景颜色应该改变。

我不知道如何实现它。以下是我目前所拥有的。

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

}

最佳答案

这可能对您有帮助, View 翻转器已用于在一组 imageView 之间翻转,在您的情况下, ImageView 的高度和宽度将设置为 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="&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;
}

}

关于android - 通过触摸更改屏幕背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16036148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com