• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Swipe to change brightness

LcsGrz

Lurker
Hi, I'm doing that when I move my finger across the screen vertically, the brightness of the phone changes ...
This is my code:
Java:
 @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        TextView tv = (TextView) findViewById(R.id.textView);
        // TODO Auto-generated method stub
        final int action = MotionEventCompat.getActionMasked(motionEvent);

        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                tv.setText(" down "); // salir de la actividad - volver al menu
                break;
            }
            case MotionEvent.ACTION_UP: {
                tv.setText(" up "); // nada
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                tv.setText(" izq - der ");
                final float x = motionEvent.getX();
                final float y = motionEvent.getY();
                distanceCovered = getDistance(x, y, motionEvent);
                try {
                        changeBrightness(motionEvent.getHistoricalX(0, 0), motionEvent.getHistoricalY(0, 0), x, y, distanceCovered);
                } catch (IllegalArgumentException e) {
                    Log.d("loger","ilegal");
                    Log.d("loger",e.toString());
                    Log.d("loger",e.getMessage());
                } catch (IndexOutOfBoundsException e) {
                    Log.d("loger","index");
                }
                break;
            }
        }
        return true;
    }

Java:
 float getDistance(float startX, float startY, MotionEvent ev) {
        float distanceSum = 0;
        final int historySize = ev.getHistorySize();
        for (int h = 0; h < historySize; h++) {
            float hx = ev.getHistoricalX(0, h);
            float hy = ev.getHistoricalY(0, h);
            float dx = (hx - startX);
            float dy = (hy - startY);
            distanceSum += Math.sqrt(dx * dx + dy * dy);
            startX = hx;
            startY = hy;
        }
        float dx = (ev.getX(0) - startX);
        float dy = (ev.getY(0) - startY);
        distanceSum += Math.sqrt(dx * dx + dy * dy);
        return distanceSum;
    }

    public void changeBrightness(float X, float Y, float x, float y, float distance) {
            distance = distance / 270;
            if (y < Y) {
                commonBrightness(distance);
            } else {
                commonBrightness(-distance);
            }

    }

    public void commonBrightness(float distance) {
        WindowManager.LayoutParams layout = getWindow().getAttributes();
        layout.screenBrightness = getWindow().getAttributes().screenBrightness + distance;
        getWindow().setAttributes(layout);

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

10-15 20:10:42.930 30909-30909/com.gzsoft.linternatestpantalla D/loger: java.lang.IllegalArgumentException: historyPos out of range
10-15 20:10:42.931 30909-30909/com.gzsoft.linternatestpantalla D/loger: historyPos out of range

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

Thank you very much!
 
Back
Top Bottom