Scrolling my Recyclerview with linearLayoutManager.scrollToPositionWithOffset(position, offset) but the scroll displaying immediately. How to possible when the scroll start, the current screen should fade out and the new scrolled or positioned screen should fade in effectively without any scrolling?
Edit: My purpose is hiding the delay of my scrollToPositionWithOffset() function. The times of the fade effects and the scroll should be simultaneously. The effect should start with the starting of scrollToPositionWithOffset() and should end with isScrolled().
You can try creating custom LinearLayoutManager like this and override the implementation of smoothScrollToPosition() to start the animation :
public class MyLinearLayoutManager extends LinearLayoutManager {
private Animation fadeIn, fadeOut;
public MyLinearLayoutManager(Context context, final RecyclerView recyclerView) {
super(context);
fadeIn = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
fadeIn.setDuration(1000);
fadeOut = AnimationUtils.loadAnimation(context, android.R.anim.fade_out);
fadeOut.setDuration(1000);
fadeOut.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
recyclerView.startAnimation(fadeIn);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
#Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
recyclerView.startAnimation(fadeOut);
super.smoothScrollToPosition(recyclerView, state, position);
}
}
Set this as the LayoutManager of the RecyclerView and call smoothScrollToPosition(recyclerView, null, position) when you want the fading in/out effect.
Fade out the RecyclerView.
Instant scroll the RecyclerView.
Fade in the RecyclerView.
In Kotlin this might look like
val anim = ObjectAnimator.ofFloat(recyclerView, "alpha", 0.0f)
//Customize your animation then
anim.addListener(onEnd = {
linearLayoutManager.scrollToPositionWithOffset(position, offset)
ObjectAnimator.ofFloat(recyclerView, "alpha", 1.0f).start()
})
anim.start()
Related
I created new custom Button class, I want to achieve, whenever user go to any activity my generic button want to expand from circle to default width. While expanding I want to hide button text for while until button animation complete.
Please check my below code:
private void animateMe(Context context){
final String btnText = this.getText().toString();
final FIButton fiButton = this;
fiButton.setText("");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
fiButton.setText(btnText);
}
},500);
Animation animation = AnimationUtils.loadAnimation(context,
R.anim.expand);
super.startAnimation(animation);
}
Easily by
ViewCompat.animate(fiButton ).setStartDelay(500).alpha(1).setDuration(700).setInterpolator(new DecelerateInterpolator(1.2f)).start();
note that you have to set the fiButton alpha to zero android:alpha="0.0"
in you xml or on create view
this line will animate your view from 0 to 1 in 700 millisecond after 500 millisecond.
You can use an AnimationListener. On finishing the animation you should do setText on the TextView.
private void animateMe(Context context){
final String btnText = this.getText().toString();
final FIButton fiButton = this;
fiButton.setText("");
Animation animation = AnimationUtils.loadAnimation(context, R.anim.expand);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
fiButton.setText("");
}
#Override
public void onAnimationEnd(Animation animation) {
fiButton.setText(btnText);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
super.startAnimation(animation);
}
I want the Layout"authentification" to appear after the Splash screen, in my application it's appear by default please some one help me!!!!!
pleaaase i need help
public class Splash extends Activity {
LinearLayout ln;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashh);
ln = (LinearLayout) findViewById(R.id.LinLaySpalScrenLogin);
final ImageView iv = (ImageView) findViewById(R.id.imageView);
final Animation an = AnimationUtils.loadAnimation(getBaseContext(),R.anim.rotate);
final Animation an2 = AnimationUtils.loadAnimation(getBaseContext(),R.anim.abc_fade_out);
iv.startAnimation(an);
an.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
iv.startAnimation(an2);
finish();
ln.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
You can set android:visibility="gone" in your LinearLayout xml file that contains your Splash Screen and then call yourlayout.setVisibility(View.VISIBLE); after you finish your animation.
Also your activity will end as soon as your animation finishes since you have called finish() before ln.setVisibility(View.VISIBLE); Try to remove finish() and only call it on some event such as Button click or something like that
You have ln.setVisibility(View.VISIBLE); after finish();. Try changing to :-
ln.setVisibility(View.VISIBLE);
finish();
However, this will probably not work as because as soon as the make the layout visible. The activity would finish. You probably want to display the authentication in another activity after the splash has completed.
I want to disable click on button when the animation running.
the code is below:
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(4000);
anim.setRepeatMode(Animation.REVERSE);
btnTag.startAnimation(anim);
So i want to cant click the button until animation done.
I normally accomplish something like this is using an AnimationListener. It allows you to run code at various stages of the animation.
This code is untested, but the way it should look is:
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(4000);
anim.setRepeatMode(Animation.REVERSE);
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
btnTag.setClickable(false);
}
#Override
public void onAnimationEnd(Animation animation) {
btnTag.setClickable(true);
}
#Override
public void onAnimationRepeat(Animation animation) {}
});
btnTag.startAnimation(anim);
Not sure if btnTag is your button or a view holding your button, but call the button's setClickable(boolean clickable) method to enable and disable the button.
I would like to share the solution I found for this problem, for those who use data-binding:
binding.ivImage.setOnClickListener {
binding.ivImage.animate().apply {
duration=1000
scaleXBy(.5f)
scaleYBy(.5f)
rotationYBy(360f)
translationYBy(200f)
}.withEndAction {
binding.ivImage.animate().apply {
duration = 1000
scaleXBy(-.5f)
scaleYBy(-.5f)
rotationXBy(360f)
translationYBy(-200f)
}
}
}
binding.ivImage.animate().setListener(object: Animator.AnimatorListener{
override fun onAnimationStart(p0: Animator?) {
binding.ivImage.isClickable=false
binding.ivImage.isEnabled=false
}
override fun onAnimationEnd(p0: Animator?) {
binding.ivImage.isClickable=true
binding.ivImage.isEnabled=true
}
override fun onAnimationCancel(p0: Animator?) {
binding.ivImage.isClickable=true
binding.ivImage.isEnabled=true
}
override fun onAnimationRepeat(p0: Animator?) {
TODO("Not yet implemented")
}
})
I have ImageView that I want to animate it whenever user clicks on it.
So I ended up with this simple solution:
public void onClick(View v) {
imageView.animate()
.rotationX(360).rotationY(360)
.setDuration(1000)
.setInterpolator(new LinearInterpolator());
}
It works just perfect, but only FIRST TIME (first click plays animation, after that animation does not work at all).
How can I fix that?
You need to reset the rotation before or after each animation. For example:
imageView.animate()
.rotationX(360).rotationY(360)
.setDuration(1000)
.setInterpolator(new LinearInterpolator())
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animator) {
imageView.setRotationX(0);
imageView.setRotationY(0);
}
});
these are just the methods i can override during animationListener.
http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html
Animation animation
animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.device_box);
animation.setAnimationListener(new AnimationListener(){
#Overridepublic void onAnimationEnd(Animation arg0) {}
#Override public void onAnimationRepeat(Animation animation) {}
#Override public void onAnimationStart(Animation animation) {}});
I think adding a thread in onAnimationStart and stop it on onAnimationEnd and getting the coordinates with a loop inside the thread, this may work.
But i think that i could get some problems with buttons because, when animating them in this way, only the background moves, not button area itself
In this case, i have to investigate more with buttons
What do you think about this, is there another way to achieve what i want?
Is this the better way to do it ?
I researched, and what I got is this, not X and Y but can handle the data as get what has been animated as time.
ObjectAnimator anim = ObjectAnimator.ofFloat(box_image, "translationX", 0 , cant);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator arg0) {
double f = Math.round(arg0.getAnimatedFraction()*1000.0)/1000.0;
Log.d("datos", ""+f);
}
});
anim.setDuration(1000);
anim.start();