Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is what I normally do in these cases.</p> <p>First of all, your onScroll method should look something like this:</p> <pre><code>@Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // Make sure that mTextView is the text view you want to move around if (!(mTextView.getLayoutParams() instanceof MarginLayoutParams)) { return false; } MarginLayoutParams marginLayoutParams = (MarginLayoutParams) mTextView.getLayoutParams(); marginLayoutParams.leftMargin = (int) marginLayoutParams.leftMargin - distanceX; marginLayoutParams.topMargin = (int) marginLayoutParams.topMargin - distanceY; mTextView.requestLayout(); return true; } </code></pre> <p>We are modifying the <code>leftMargin</code> and <code>topMargin</code> an amount equivalent to the distance that has been scrolled.</p> <p>Next, to make the text view animate back to its original position you need to do so when the the event is <code>ACTION_UP</code> or <code>ACTION_CANCEL</code>:</p> <pre><code>@Override public boolean onTouch(View arg0, MotionEvent event) { if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) { snapBack(); } return mScrollDetector.onTouchEvent(event); } </code></pre> <p>Then in the snapBack method we animate back the text view:</p> <pre><code>private void snapBack () { if (mTextView.getLayoutParams() instanceof MarginLayoutParams) { final MarginLayoutParams marginLayoutParams = (MarginLayoutParams) mTextView.getLayoutParams(); final int startValueX = marginLayoutParams.leftMargin; final int startValueY = marginLayoutParams.topMargin; final int endValueX = 0; final int endValueY = 0; mTextView.clearAnimation(); Animation animation = new Animation() { @Override protected void applyTransformation(float interpolatedTime, Transformation t) { int leftMarginInterpolatedValue = (int) (startValueX + (endValueX - startValueX) * interpolatedTime); marginLayoutParams.leftMargin = leftMarginInterpolatedValue; int topMarginInterpolatedValue = (int) (startValueY + (endValueY - startValueY) * interpolatedTime); marginLayoutParams.topMargin = topMarginInterpolatedValue; mTextView.requestLayout(); } }; animation.setDuration(200); animation.setInterpolator(new DecelerateInterpolator()); mTextView.startAnimation(animation); } } </code></pre> <p>And that should do! You can modify the <code>endValueX</code> and <code>endValueY</code> variables to control where the text view goes back when you lift your finger.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload