Reputation: 554
I want to animate a simple view, say for example a simple textview. I want to use translate animate the view.
Now what my requirement is, I want to make a method, say for example slide(View v, float position)
. Which will take the view to animate and position till where it should be animated. And I will call that method from desired place in my code.
To accomplish this, I have tried something. I have made MyTranslateAnimation
class as follow.
public class MyTranslateAnimation extends Animation {
private View mView;
private final float position;
public MyTranslateAnimation(View view, float position){
mView = view;
this.position = position;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
mView.setY(position);
mView.requestLayout();
}
}
Then I made a textview
in MainActivity.java
and set onTouchListener
and then created this method slide()
to do the task I described above.
Below is the code:
onCreate():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_root = (ViewGroup)findViewById(R.id.root);
_view = new TextView(this);
_view.setText("TextView!!!!!!!!");
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 50);
layoutParams.leftMargin = 50;
layoutParams.topMargin = 50;
layoutParams.bottomMargin = -250;
layoutParams.rightMargin = -250;
_view.setLayoutParams(layoutParams);
_view.setOnTouchListener(this);
_root.addView(_view);
}
slide():
private void slide(View view, float position){
Animation animation = new MyTranslateAnimation(view, position);
animation.setInterpolator(new DecelerateInterpolator());
animation.setDuration(200);
animation.start();
}
And as below I used slide()
method:
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
slide(view, 100);
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
_root.invalidate();
return true;
}
Also I don't want to use nineoldandroid
library for this.
Any help regarding this is highly appreciated.
Upvotes: 2
Views: 361
Reputation: 554
slide():
public void slide(float position){
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(overflow.get(), "translationY", position);
objectAnimator.setInterpolator(new DecelerateInterpolator());
objectAnimator.setDuration(200);
objectAnimator.start();
}
Upvotes: 3