【问题标题】:Changing Screen background color by touch通过触摸更改屏幕背景颜色
【发布时间】:2017-01-22 00:29:30
【问题描述】:

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

    }

【问题讨论】:

  • 首先创建一个默认背景设置为黑色的布局(主布局)。现在在您的 onFling() 方法中将背景颜色更改为您想要的任何颜色
  • 我该怎么做,希望你能帮我创建这个:android:background="#000000"
  • 我将其作为答案发布,请查看

标签: android


【解决方案1】:

这可能会对您有所帮助,这里使用视图翻转器在一组图像视图之间翻转,在您的情况下,图像视图的高度和宽度将设置为 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;
}

   }

【讨论】:

  • 谢谢 :) 我不会用图片来改变背景,没有图片我怎么能做到。
  • 改用布局,会一样
【解决方案2】:

你的布局

<?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);

【讨论】:

    【解决方案3】:

    像这样制作你的布局文件..

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

    希望它有效...

    【讨论】:

      猜你喜欢
      • 2012-08-27
      • 2013-05-28
      • 2021-02-07
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      相关资源
      最近更新 更多