how can I control the volume with the android screen?

0

Hello, my friends, programmers, how are you, I hope very well

I have a problem with an app I'm doing for the University which has to control the volume by means of the screen, that is to say that if I press the screen up or down the volume of the device goes up or down the problem is that I could not capture the touch vertically someone can help me in that

    
asked by Jhonny Luis 18.09.2016 в 23:31
source

2 answers

2

You must implement the onTouchEvent() in the Activity.

Then you overwrite the onTouchEvent() .

private String TAG = GestureActivity.class.getSimpleName();
float initialX, initialY;

@Override
public boolean onTouchEvent(MotionEvent event) {
    //mGestureDetector.onTouchEvent(event);

    int action = event.getActionMasked();

    switch (action) {

        case MotionEvent.ACTION_DOWN:
            initialX = event.getX();
            initialY = event.getY();

            Log.d(TAG, "Action was DOWN");
            break;

        case MotionEvent.ACTION_MOVE:
            Log.d(TAG, "Action was MOVE");
            break;

        case MotionEvent.ACTION_UP:
            float finalX = event.getX();
            float finalY = event.getY();

            Log.d(TAG, "Action was UP");

            if (initialX < finalX) {
                Log.d(TAG, "Left to Right swipe performed");
            }

            if (initialX > finalX) {
                Log.d(TAG, "Right to Left swipe performed");
            }

            if (initialY < finalY) {
                Log.d(TAG, "Up to Down swipe performed");
            }

            if (initialY > finalY) {
                Log.d(TAG, "Down to Up swipe performed");
            }

            break;

        case MotionEvent.ACTION_CANCEL:
            Log.d(TAG,"Action was CANCEL");
            break;

        case MotionEvent.ACTION_OUTSIDE:
            Log.d(TAG, "Movement occurred outside bounds of current screen element");
            break;
    }

    return super.onTouchEvent(event);
}

You capture the touch event in your view

class CustomImageView extends ImageView {

    public CustomImageView(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getActionMasked();

        Log.d(TAG, String.valueOf(action));

        return super.onTouchEvent(event);
    }
}

And this for example your view.

ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        // have same code as onTouchEvent() (for the Activity) above

        int action = event.getActionMasked();

        Log.d(TAG, String.valueOf(action));

        return true;
    }
});

For a more detailed explanation you can visit this link.

Using onTouchEvent () and View.OnTouchListener Interface with MotionEvent to Detect Common Gestures Like Tap and Swipes on Android

    
answered by 21.10.2016 в 19:33
0

I have not understood very well, but if you want to raise or lower the volume when you slide up or down on the screen, what you have to do is detect a "Gesture". To do this, register a GestureDetector as explained in link . You can also study some similar examples like link

    
answered by 21.09.2016 в 18:25