1. Download our Official Android App: Forums for Android!

Tips Swipe to change brightness

Discussion in 'Android Devices' started by LcsGrz, Oct 15, 2017.

  1. LcsGrz

    LcsGrz Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    2
    Joined:
    Oct 15, 2017

    Oct 15, 2017
    2
    0
    15
    Hi, I'm doing that when I move my finger across the screen vertically, the brightness of the phone changes ...
    This is my code:
    Code (Java):
    1.  @Override
    2.     public boolean onTouchEvent(MotionEvent motionEvent) {
    3.         TextView tv = (TextView) findViewById(R.id.textView);
    4.         // TODO Auto-generated method stub
    5.         final int action = MotionEventCompat.getActionMasked(motionEvent);
    6.  
    7.         switch (action) {
    8.             case MotionEvent.ACTION_DOWN: {
    9.                 tv.setText(" down "); // salir de la actividad - volver al menu
    10.                 break;
    11.             }
    12.             case MotionEvent.ACTION_UP: {
    13.                 tv.setText(" up "); // nada
    14.                 break;
    15.             }
    16.             case MotionEvent.ACTION_MOVE: {
    17.                 tv.setText(" izq - der ");
    18.                 final float x = motionEvent.getX();
    19.                 final float y = motionEvent.getY();
    20.                 distanceCovered = getDistance(x, y, motionEvent);
    21.                 try {
    22.                         changeBrightness(motionEvent.getHistoricalX(0, 0), motionEvent.getHistoricalY(0, 0), x, y, distanceCovered);
    23.                 } catch (IllegalArgumentException e) {
    24.                     Log.d("loger","ilegal");
    25.                     Log.d("loger",e.toString());
    26.                     Log.d("loger",e.getMessage());
    27.                 } catch (IndexOutOfBoundsException e) {
    28.                     Log.d("loger","index");
    29.                 }
    30.                 break;
    31.             }
    32.         }
    33.         return true;
    34.     }
    Code (Java):
    1.  float getDistance(float startX, float startY, MotionEvent ev) {
    2.         float distanceSum = 0;
    3.         final int historySize = ev.getHistorySize();
    4.         for (int h = 0; h < historySize; h++) {
    5.             float hx = ev.getHistoricalX(0, h);
    6.             float hy = ev.getHistoricalY(0, h);
    7.             float dx = (hx - startX);
    8.             float dy = (hy - startY);
    9.             distanceSum += Math.sqrt(dx * dx + dy * dy);
    10.             startX = hx;
    11.             startY = hy;
    12.         }
    13.         float dx = (ev.getX(0) - startX);
    14.         float dy = (ev.getY(0) - startY);
    15.         distanceSum += Math.sqrt(dx * dx + dy * dy);
    16.         return distanceSum;
    17.     }
    18.  
    19.     public void changeBrightness(float X, float Y, float x, float y, float distance) {
    20.             distance = distance / 270;
    21.             if (y < Y) {
    22.                 commonBrightness(distance);
    23.             } else {
    24.                 commonBrightness(-distance);
    25.             }
    26.  
    27.     }
    28.  
    29.     public void commonBrightness(float distance) {
    30.         WindowManager.LayoutParams layout = getWindow().getAttributes();
    31.         layout.screenBrightness = getWindow().getAttributes().screenBrightness + distance;
    32.         getWindow().setAttributes(layout);
    33.  
    34.     }
    The problem is that it works fine for a moment, at the time of launching the ecepcion, it stops working forever.
    The error message is as follows:

    Is there a simpler way to create what I want? or a possible arrangement for this?

    Thank you very much!
     

    Advertisement

Share This Page

Loading...