animation canceled when scrolling - android

I've implemented a small animation on a button placed over a list view.
Basically it's like a quick action button to add/refresh the list.
What I want to do is to animate the button once the user has clicked on it and display a popup. Once the popup has been dismissed, the button should go back to its initial state.
I've made the followning code:
final PopupWindow popup = new PopupWindow(getContext());
// Attach layout
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.core_popup_quickactions, new LinearLayout(getContext()));
// Creating the PopupWindow
popup.setContentView(layout);
popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new BitmapDrawable());
popup.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss()
{
// Rotate back
RotateAnimation animation = new RotateAnimation(-45.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(300);
animation.setRepeatCount(0);
animation.setFillAfter(true);
animation.setFillBefore(true);
animation.setFillEnabled(true);
startAnimation(animation);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation arg0)
{
QuickActionButton.this.setClickable(false);
}
#Override
public void onAnimationEnd(Animation arg0)
{
QuickActionButton.this.setClickable(true);
}
#Override
public void onAnimationRepeat(Animation arg0)
{
}
});
}
});
popup.setTouchable(false);
popup.showAsDropDown(this, 0, -dpToPx(350));
// Rotate the icon
RotateAnimation animation = new RotateAnimation(0.0f, -45.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(300);
animation.setRepeatCount(0);
animation.setFillAfter(true);
animation.setFillBefore(true);
animation.setFillEnabled(true);
startAnimation(animation);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation arg0)
{
popup.setTouchable(false);
}
#Override
public void onAnimationEnd(Animation arg0)
{
popup.setTouchable(true);
}
#Override
public void onAnimationRepeat(Animation arg0)
{
}
});
The problem now is that when I scroll the view and press the quick action button, the animation starts and is reseted after a few miliseconds.
Is that a normal behaviour because of the Popup ?

Finally, I've found a dirty way to solve this issue:
handler.postDelayed(new Runnable() {
#Override
public void run()
{
if (!popup.isShowing())
{
handler.postDelayed(this, 100);
return;
}
// Rotate the icon
RotateAnimation animation = new RotateAnimation(0.0f, -45.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(300);
animation.setRepeatCount(0);
animation.setFillAfter(true);
animation.setFillBefore(true);
animation.setFillEnabled(true);
startAnimation(animation);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation arg0)
{
popup.setTouchable(false);
}
#Override
public void onAnimationEnd(Animation arg0)
{
popup.setTouchable(true);
}
#Override
public void onAnimationRepeat(Animation arg0)
{
}
});
}
}, 100);
It's just a delayed until the popup has been completely shown.

Related

is there a TranslateAnimation onComplete?

I am animating a view to move off the screen, on complete i would like to call another function but I cant seem to find a onComplete method.
int originalPos[] = new int[2];
icons.getLocationOnScreen( originalPos );
layoutMoved = originalPos[0]+icons.getWidth();
TranslateAnimation anim = new TranslateAnimation( 0, -layoutMoved , 0, 0);
anim.setDuration(500);
anim.setFillAfter( true );
icons.startAnimation(anim);
icons.setVisibility(View.GONE);
Set an AnimationListener, you'll have access to OnAnimationEnd.
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
// do your stuff
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

RecyclerView onClick ViewHolder not triggering Animation

I am trying to achieve AlphaAnimation on an ImageView whenever user clicks on a RecyclerView item. I have the following code:
class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView vh_idnumber, vh_task;
Information a;
View vh_colorTag;
FrameLayout checkboxLayout;
ImageView checkboxBackground;
Drawable checkBoxBackgroundDrawable;
private ScaleAnimation ZoomOut;
private ScaleAnimation ZoomIn;
#SuppressWarnings("deprecation")
public CustomViewHolder(View itemView)
{
super(itemView);
itemView.getId();
vh_idnumber = (TextView) itemView.findViewById(R.id.idnumber);
vh_task = (TextView) itemView.findViewById(R.id.task);
vh_colorTag = (View) itemView.findViewById(R.id.colortag);
checkboxLayout = (FrameLayout) itemView.findViewById(R.id.checkBoxLayout);
checkboxBackground = (ImageView) itemView.findViewById(R.id.checkBoxBackground);
checkBoxBackgroundDrawable= context.getResources().getDrawable(R.drawable.circle_shape);
checkboxBackground.setBackgroundDrawable(checkBoxBackgroundDrawable);
ZoomOut = new ScaleAnimation(1f, 0.3f, 1f, 0.3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
checkboxBackground.setAnimation(ZoomOut);
ZoomOut.setDuration(100);
ZoomOut.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation)
{
checkBoxBackgroundDrawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
}
});
ZoomIn = new ScaleAnimation(0.3f, 1f, 0.3f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
checkboxBackground.setAnimation(ZoomIn);
ZoomIn.setDuration(100);
ZoomIn.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation)
{
checkBoxBackgroundDrawable.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);
}
});
random = new Random();
int index = random.nextInt(colorCode.length);
if(!colorRibbon)
{
vh_colorTag.setVisibility(View.GONE);
}
else
{
vh_colorTag.setBackgroundColor(Color.parseColor(colorCode[index]));
}
itemView.setOnClickListener(this);
vh_task.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
a = MainActivity.data.get(getAdapterPosition());
if(a.availability.matches("available"))
{
vh_task.setPaintFlags(vh_task.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
vh_task.setTextColor(Color.RED);
a.availability="unavailable";
}
else if(a.availability.matches("unavailable"))
{
vh_task.setPaintFlags( vh_task.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
vh_task.setTextColor(Color.parseColor("#212121"));
a.availability="available";
}
}
});
checkboxLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
changeStatus();
}
});
}
#Override
public void onClick(View v)
{
changeStatus();
}
protected void changeStatus()
{
a = MainActivity.data.get(getAdapterPosition());
if(a.status.matches("checked"))
{
/* This dont' work */
ZoomOut.start();
a.status="unchecked";
}
else if(a.status.matches("unchecked"))
{
/* Neither this */
ZoomIn.start();
a.status="checked";
}
}
}
The AlphaAnimation does not get triggered. But onClick event gets fired. Tested the onclick event using Log. If I replace Animation with anything else it works but the Animation wont work.
What you want is achievable using startAnimation instead of setAnimation
protected void changeStatus() {
a = MainActivity.data.get(getAdapterPosition());
if(a.status.matches("checked"))
{
checkboxBackground.startAnimation(ZoomOut);
a.status="unchecked";
}
else if(a.status.matches("unchecked")) {
checkboxBackground.startAnimation(ZoomIn);
a.status="checked";
}
}
setAnimation needs a start time for the animation to be set, and you will have to invalidate checkboxBackground's parent.

Why OnAnimationEnd moves the initial position of the button but also moves the button itself? #.#

I did some animations with regards to my created button. However, after the animation, the button is at its new position, but the onclick function is at its old initial position. I did the onAnimationEnd and set the button to the new position but the Button and the invisible-like button with the onclick function moves together. Any help will be awesome
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//move
TranslateAnimation animation = new TranslateAnimation(0.0f, ((widthPixels / 2) - 100),
0.0f, 0.0f);
animation.setDuration(1000);
animation.setRepeatCount(0);
animation.setRepeatMode(0);
animation.setFillEnabled(true);
animation.setFillAfter(true);
//fade
// Play.setVisibility(View.VISIBLE);
Play.startAnimation(animationfadein);
Play.startAnimation(animation);
Play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DreamTales.this, Stories.class);
startActivity(i);
}
});
}
}, DELAY);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Button Playghost = (Button) findViewById(R.id.PlayBtn);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)Play.getLayoutParams();
lp.leftMargin = (widthPixels/100); // use topmargin for the y-property, left margin for the x-property of your view
Playghost.setLayoutParams(lp);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

view slide down animation not working

I am animating my relative layout with some images in my app. When i write the code to animate the layout on button click, for the first time it is just displaying not animating. Then on the same button click i am sliding up the layout. After that if i click the button sliding down animation works fine.Actually i have to write this sliding down animation inside onCreate itself instead of button click. Given below is my code. Can anyone tell me why it s not showing the animation?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fadeview);
iconLayout=(RelativeLayout)findViewById(R.id.icon_ayout);
iconLayout.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
iconLayout.setVisibility(View.VISIBLE);
SlideToDown();
}
}, 500);
}
public void SlideToAbove() {
Animation slide = null;
slide=new TranslateAnimation(0.0f, 0.0f, 0.0f,-iconLayout.getHeight());
slide.setDuration(1000);
slide.setFillAfter(true);
slide.setFillEnabled(true);
iconLayout.startAnimation(slide);
slide.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
iconLayout.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
iconLayout.getWidth(), iconLayout.getHeight());
// lp.setMargins(0, 0, 0, 0);
//lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
iconLayout.setLayoutParams(lp);
}
});
}
public void SlideToDown() {
Animation slide = null;
slide=new TranslateAnimation(0.0f, 0.0f, -iconLayout.getHeight(), 0.0f);
slide.setDuration(1000);
slide.setFillAfter(true);
slide.setFillEnabled(true);
iconLayout.startAnimation(slide);
slide.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
iconLayout.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
iconLayout.getWidth(), iconLayout.getHeight());
lp.setMargins(0,0, 0, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
iconLayout.setLayoutParams(lp);
}
});
}
This is because you are starting animation in onCreate() and in this time the view is available but it is not actually drawn(doesnt have width and height). Here the value of iconLayout.getHeight() will return value 0.
Solution is to setup a Layoutlistener to the view and the listener will notity you when it is drawn. After that you can start the animation.
ViewTreeObserver loadingObserver = iconLayout.getViewTreeObserver();
loadingObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
iconLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
SlideToDown();
}
});

Slide down view in android

I would like to have a button in my android application trigger a slide down view of a form. I want to have a view at the top of the screen, a list at the bottom of the screen, and I want to have the slide down form view appear between the two when a button is clicked.
I have no problem showing the view, but can't seem to animate it from hidden to shown on the screen.
Any ideas on how this could work?
public void doSlideDown(View view){
RelativeLayout myView = (RelativeLayout)findViewById(R.id.my_view);
addListingView.setVisibility(myView.VISIBLE);
Animation slideDown = setLayoutAnim_slidedown();
myView.startAnimation(slideDown);
}
public void doSlideUp(View view){
RelativeLayout myView = (RelativeLayout)findViewById(R.id.my_view);
Animation slideUp = setLayoutAnim_slideup();
myView.startAnimation(slideUp);
}
public Animation setLayoutAnim_slidedown() {
AnimationSet set = new AnimationSet(true);
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, -1.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(800);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
// MapContacts.this.mapviewgroup.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Log.d("LA","sliding down ended");
}
});
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(
set, 0.25f);
return animation;
}
public Animation setLayoutAnim_slideup() {
AnimationSet set = new AnimationSet(true);
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f);
animation.setDuration(800);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
RelativeLayout bodyView = (RelativeLayout)findViewById(R.id.bodyView);
RelativeLayout myView = (RelativeLayout)findViewById(R.id.my_view);
addListingView.clearAnimation();
bodyView.removeView(myView);
}
});
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(
set, 0.25f);
return animation;
}
This may help you:
ANDROID ANIMATION SLIDEDOWN SLIDEUP
Android Animation Framework

Categories

Resources