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);
}
Related
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()
I am using a fragment and there is a button as soon as I click on it the background will become dim and textView will be visible.The appearance of the dimming effect and text will take place at once.For some reason I don't get these result.
Here is my code:-
activate_wifi_button = (Button)wifi_and_hotspot.findViewById(R.id.Activate_wifi);
activate_wifi_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
#Override
public void run() {
WindowManager.LayoutParams layoutParams=getActivity().getWindow()
.getAttributes();
layoutParams.dimAmount = 0.7f;
getActivity().getWindow().setAttributes(layoutParams);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
text.setVisibility(View.VISIBLE);
text_animate_dots.setVisibility(View.VISIBLE);
timer.cancel();
}
};
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(runnable);
}
}, 2000, 2000);
}
});
I have used handler and runnable because I want to animate the textview for 2 sec but the animate part can come latter at first I need to do the above task.
For the desired output, you can take semi-transparent layout and textView in FrameLayout. In onclick method, set its visibility VISIBLE; and for better user experience, set Fade animation to textview.
I have an app that creates questions (sums) for the user that includes a timer. Once the timer reaches zero, the score is displayed to the user as well as a TextView that says Play Again?
The code runs fine until I implement the flashing/blinking (animation sets TextView color and then transparent). Once the Play Again? is clicked, the app stops but doesn't crash. I think it is because the playAgain() method isn't called?
I want to keep the object oriented approach which is why I created a separate method for the animation called flashPlay().
Here is the playAgain() code:
public void playAgain (View v) {
score = 0;
numOfSums = 0;
timeTextView.setText("30");
scoreTextView.setText("0 : 0");
resultTextView.setText("");
playAgainButton.setVisibility(View.INVISIBLE);
button0.setEnabled(true);
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
playAgainButton.setEnabled(true);
createQuestion();
textViewTimesUp.setVisibility(View.INVISIBLE);
new CountDownTimer(3100, 1000){
#Override
public void onTick(long millisUntilFinished) {
timeTextView.setText(String.valueOf(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
button0.setEnabled(false);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
playAgainButton.setVisibility(View.VISIBLE);
timeTextView.setText("0");
textViewTimesUp.setVisibility(View.VISIBLE);
flashPlay(); //calling the flashPlay() method.
if (score >= 1 && numOfSums >= 1) {
int percent =((score * 100) / numOfSums);
resultTextView.setText("Score: " + percent + "%");
textViewTimesUp.setText("Time's Up!");
questionTextView.setText("");
} else {
textViewTimesUp.setText("Time's Up!");
questionTextView.setText("");
}
}
}.start();
}
And here is the flashPlay() code:
public void flashPlay () {
final ObjectAnimator colorAnim = ObjectAnimator.ofInt(playAgainButton, "textColor", Color.CYAN, Color.TRANSPARENT);
colorAnim.setDuration(600); //duration of flash
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
playAgainButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
colorAnim.end();
}
});
}
Can someone show me whats wrong with the code.
Thank you.
Sth like this? You should set the OnClickListener once in onCreate(). In flashPlay() it would have been set again every time the method gets called.
ObjectAnimator colorAnim = null;
onCreate(){
...
playAgainButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
if(colorAnim != null && colorAnim.isStarted()){
colorAnim.end();
//playAgain(v); ?
}
}
});
...
}
...
public void flashPlay () {
colorAnim = ObjectAnimator.ofInt(playAgainButton, "textColor", Color.CYAN, Color.TRANSPARENT);
colorAnim.setDuration(600); //duration of flash
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
}
in my app, i just blink my textView with this animation:
public static Animation blinkAnim() {
// Configure your animation properties here
Animation animation = new AlphaAnimation(1, 0);
animation.setDuration(550);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(10);
animation.setRepeatMode(Animation.REVERSE);
return animation;
}
You can anim it by doing:
// get the animation
Animation anim = blinkAnim();
// start the animation
myTextView.startAnimation(anim);
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 use this code to when i click on a imagebox, run an animation on another object and dissaper itself via visibility.GONE. but it doesnt work!! here is my code:
againbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//answer button on animation
Animation anim2 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.askbtnonanim);
anim2.setFillAfter(true);
askbtn.startAnimation(anim2);
//gone myselft (againbtn)
againbtn.setVisibility(View.GONE);
}
});
if a delete 3 animation line from this code, everything is OK and works, but now it doesn't. but why? it's related to anim2.setFillAfter(true); ??? i put this because my animation run one time and dont reset! please help me
Try this
You have to clear the view animation then you can setVisibility
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
view.clearAnimation();
view.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {}
});
You should implement Animation Listener and in onAnimationEnd() you should perform your task... hope below code will help you...
anim2.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
againbtn.setVisibility(View.GONE); //set your button visibility here
}
});
I think put visibility button before animation coding the this might work
againbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//gone myselft (againbtn)
againbtn.setVisibility(View.GONE);
//answer button on animation
Animation anim2 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.askbtnonanim);
anim2.setFillAfter(true);
askbtn.startAnimation(anim2);
}
});
Calling clearAnimation() on the view that is doing the animation before calling View.INVISIBLE, or GONE does the trick.
use AnimationListener for setting Button Visibility GONE when Animation end.
.....
anim2.setAnimationListener(animButnListener);
askbtn.startAnimation(anim2);
AnimationListener animButnListener = new AnimationListener(){
#Override
public void onAnimationEnd(Animation animation) {
// make Button Visibility GONE here
againbtn.setVisibility(View.GONE);
}
//.......other AnimationListener methods
};