繁体   English   中英

使用Android中的Gestures提高Swipe视图的可见性

[英]Increasing the visibility of a view on Swipe using Gestures in android

我正在使用以下代码

private GestureDetector.OnGestureListener mGestureListener = new GestureDetector.OnGestureListener()
{
    private float lastDeltaValue;
    private static final int MIN_DISTANCE = 15;
    private static final int MIN_DISTANCE_Y = 25;
    @Override
    public boolean onDown(MotionEvent e)
    {
        lastDeltaValue = 0;
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        float startingX = (int)e1.getRawX();
        float startingY = (int)e1.getRawY();

        float endingX = (int)e2.getRawX();
        float endingY = (int)e2.getRawY();

        float deltaX = startingX - endingX;
        float deltaY = startingY - endingY;

        // swipe horizontal?
        if(Math.abs(deltaX) > MIN_DISTANCE && Math.abs(deltaY) < MIN_DISTANCE_Y)
        {
            // left or right
            if(deltaX > 0)
            {
                shouldCallBtn = SHOULD_CALL_WHERE;
                if (gestureLayout.getVisibility() == View.GONE)
                {
                    gestureLayout.setVisibility(View.VISIBLE);
                    gestureLayout.setAlpha(0.3f);
                }
                gestureText.setText(getString(R.string.option_where));
                if (deltaX > lastDeltaValue)
                {
                    if (gestureLayout.getAlpha() > 0.29 && gestureLayout.getAlpha() < 0.80)
                        gestureLayout.setAlpha(gestureLayout.getAlpha() + 0.1f);
                }
                else
                {
                    if (gestureLayout.getAlpha() < 0.81 && gestureLayout.getAlpha() > 0.29)
                        gestureLayout.setAlpha(gestureLayout.getAlpha() - 0.1f);
                }
                Log.d("DELTA VALUES", String.valueOf(deltaX) + "  ==  " + String.valueOf(lastDeltaValue) + "   " +String.valueOf(gestureLayout.getAlpha()));

                lastDeltaValue = deltaX;

            }
            else if(deltaX < 0)
            {
                shouldCallBtn = SHOULD_CALL_ONMYWAY;
                if (gestureLayout.getVisibility() == View.GONE)
                {
                    gestureLayout.setVisibility(View.VISIBLE);
                    gestureLayout.setAlpha(0.3f);
                }
                gestureText.setText(getString(R.string.option_onway));
                if (deltaX > lastDeltaValue)
                {
                    if (gestureLayout.getAlpha() > 0.29 && gestureLayout.getAlpha() < 0.80)
                        gestureLayout.setAlpha(gestureLayout.getAlpha() - 0.1f);
                }
                else
                {
                    if (gestureLayout.getAlpha() < 0.81 && gestureLayout.getAlpha() > 0.29)
                        gestureLayout.setAlpha(gestureLayout.getAlpha() + 0.1f);
                }
                Log.d("DELTA VALUES", String.valueOf(deltaX) + "  ==  " + String.valueOf(lastDeltaValue) + "   " +String.valueOf(gestureLayout.getAlpha()));

                lastDeltaValue = deltaX;
            }
        }
        else
        {
        }
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e)
    {
        gestureLayout.setVisibility(View.VISIBLE);
        gestureLayout.setAlpha(0.8f);
        gestureText.setText(getString(R.string.option_my_location));
        sendLocBtn.performClick();
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        return false;
    }
};

最初GestureLayout可见性已经GONE 滑动后,其可见度将从0.3增加到0.8。 如果我尝试左键滑动它会在屏幕上的某个地方增加一个View的alpha值,例如(向左滑动),右键滑动也是如此。

这段代码似乎有效,但alpha从低到高的动画不符合标准。

任何帮助都会有所帮助

注意:我不需要动画。 我希望它基于手指的滑动

让我们调用0.1f deltaAlpha0.1f deltaAlpha必须基于deltaX值。 您必须为应用程序定义一个系数,即deltaXdeltaAlpha之间的比率。 例如,假设10px等于0.01 alpha变化。 现在您知道,如果您的手指在屏幕上移动40px ,则Alpha值将以0.04无效的速度改变。 这样,动画将是平滑的,并且将基于手指在屏幕上移动的距离。 尝试以下java代码:

// 1 deltaX = 0.01 alpha
// This is just an example coefficient.
// Replace it with a value that fits your needs
private static final float COEFFICIENT = 0.01;

private float calculateDeltaAlpha(float deltaX) {
  return deltaX * COEFICIENT;
}

private void incrementViewAlpha(View view, float distanceX) {
  float oldAlpha = gestureLayout.getAlpha();
  if (oldAlpha > 0.29 && oldAlpha < 0.80) {
    gestureLayout.setAlpha(oldAlpha + calculateAlphaDelta(distanceX));
  }
}

private void decrementViewAlpha(View view, float distanceX) {
  float oldAlpha = gestureLayout.getAlpha();
  if (oldAlpha > 0.29 && oldAlpha < 0.80) {
    gestureLayout.setAlpha(oldAlpha - calculateAlphaDelta(distanceX));
  }
}

当您想要增加或减少视图的alpha值时,调用incrementViewAlphadecrementViewAlpha方法。

我改进了Kiril的代码并避免了不必要的代码。 您需要修改COEFFICIENT值以根据布局大小,屏幕尺寸,相关参数等控制变化/平滑度。让我知道它是否适合您。 还进行了更改以将alpha限制在UPPER_BOUND_ALPHA和LOWER_BOUND_ALPHA范围内。

private static final float COEFFICIENT = 0.0001f;
private static final float UPPER_BOUND_ALPHA = 0.8f;
private static final float LOWER_BOUND_ALPHA = 0.3f;

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
    float startingX = (int)e1.getRawX();
    float startingY = (int)e1.getRawY();

    float endingX = (int)e2.getRawX();
    float endingY = (int)e2.getRawY();

    float deltaX = startingX - endingX;
    float deltaY = startingY - endingY;

    // swipe horizontal?
    if(Math.abs(deltaX) > MIN_DISTANCE && Math.abs(deltaY) < MIN_DISTANCE_Y) 
    {
        if (gestureLayout.getVisibility() == View.GONE) 
        {
            gestureLayout.setVisibility(View.VISIBLE);
            gestureLayout.setAlpha(LOWER_BOUND_ALPHA);
        }

        // left or right
        if(deltaX > 0)
        {
            shouldCallBtn = SHOULD_CALL_WHERE;
            gestureText.setText(getString(R.string.option_where));
        }
        else if(deltaX < 0)
        {
            shouldCallBtn = SHOULD_CALL_ONMYWAY;
            gestureText.setText(getString(R.string.option_onway));
        }

        float alphaValue = gestureLayout.getAlpha() + COEFFICIENT * deltaX;
        if (alphaValue > UPPER_BOUND_ALPHA) 
        {
            alphaValue = UPPER_BOUND_ALPHA;
        } 
        else if (alphaValue < LOWER_BOUND_ALPHA) 
        {
            alphaValue = LOWER_BOUND_ALPHA;
        }

        gestureLayout.setAlpha(alphaValue);

        Log.d("DELTA VALUES", deltaX + "  ==  " + lastDeltaValue + "   " + gestureLayout.getAlpha());
        lastDeltaValue = deltaX;
    }

    return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM