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.
Related
In my application, I want expand and collapse recyclerView!
I can expand and collapse recyclerView and for this I use this library : https://github.com/thoughtbot/expandable-recycler-view
I want when click on recyclerView (expand or collapse) rotate arrow image!
I write below code in viewHolder class but not work and not rotate arrow image!
ParentViewHolder codes:
public class seasonParentViewHolder extends GroupViewHolder {
private TextView row_epGuideParent_Title, row_epGuideParent_Count, row_epGuideParent_Date;
private ImageView row_epGuideParent_arrowImg;
public seasonParentViewHolder(View itemView) {
super(itemView);
row_epGuideParent_Title = (TextView) itemView.findViewById(R.id.row_epGuideParent_Title);
row_epGuideParent_Count = (TextView) itemView.findViewById(R.id.row_epGuideParent_Count);
row_epGuideParent_Date = (TextView) itemView.findViewById(R.id.row_epGuideParent_Date);
row_epGuideParent_arrowImg = (ImageView) itemView.findViewById(R.id.row_epGuideParent_arrowImg);
}
public void setGenreTitle(ExpandableGroup title) {
if (title instanceof ExpandableModel) {
row_epGuideParent_Title.setText(title.getTitle());
row_epGuideParent_Count.setText(((ExpandableModel) title).getCount());
row_epGuideParent_Date.setText(((ExpandableModel) title).getDate());
}
}
#Override
public void expand() {
animateExpand();
}
#Override
public void collapse() {
animateCollapse();
}
private void animateExpand() {
RotateAnimation rotate =
new RotateAnimation(360, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(300);
rotate.setFillAfter(true);
row_epGuideParent_arrowImg.setAnimation(rotate);
}
private void animateCollapse() {
RotateAnimation rotate =
new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(300);
rotate.setFillAfter(true);
row_epGuideParent_arrowImg.setAnimation(rotate);
}
}
Called expand and collapse in library :
public abstract class GroupViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
private OnGroupClickListener listener;
public GroupViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (listener != null) {
if (listener.onGroupClick(getAdapterPosition())) {
collapse();
} else {
expand();
}
}
}
public void setOnGroupClickListener(OnGroupClickListener listener) {
this.listener = listener;
}
public void expand() {}
public void collapse() {}
}
How can I fix this problem and rotate arrow image when click on recyclerView (expand or collapse) ???
TL;DR You have to use startAnimation() for your ImageView instead of setAnimation().
void setAnimation (Animation animation)
Sets the next animation to play for this view. If you want the
animation to play immediately, use
startAnimation(android.view.animation.Animation) instead [...]
(quoted from developer.android.com/reference/android/view/View.html)
#Override
public void expand() {
arrow_icn.setRotation(180);
}
#Override
public void collapse() {
arrow_icn.setRotation(0);
}
i have a button that i animating when touched. the animation is working but after the animation is compelete, i am calling setAnimation(null) to reset the button, but the button is not getting back to its default position.
here is the code:
ok_btn_id.setOnTouchListener(new OnSwipeTouchListener(BarCodeReaderActivity.this) {
public void onSwipeTop() {
}
public void onSwipeRight() {
slidefromLeftToRight(ok_btn_id);
}
public void onSwipeLeft() {
slidefromRightToLeft(ok_btn_id);
}
public void onSwipeBottom() {
}
});
public void slidefromRightToLeft(final View view)
{
TranslateAnimation animate;
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
if (view.getHeight() == 0) {
animate = new TranslateAnimation(metrics.widthPixels/2,
0, 0, 0);
} else {
animate = new TranslateAnimation(0,-viewGroup.getWidth(), 0, 0); // View for animation
}
animate.setDuration(500);
animate.setFillAfter(false);
view.startAnimation(animate);
view.postDelayed(new Runnable() {
#Override
public void run() {
view.setAnimation(null);
view.setVisibility(View.VISIBLE);
}
}, 500);
}
Please try this:
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
aView.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
button.clearAnimation();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Hope this will help you.
If you want to reset the position try Animation.reset();
animate.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation)
{
animation.reset();
}
}
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) {
}
});
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();
}
});
I can't get the circular reveal animation to work.
I think I checked the most obvious things:
It starts, width and height are > 0 and it is visible, no Exception..
I load some data from the internet and display it in the view(fab)
The animation should only play after the download finishes.
TmdbHelper helper = new TmdbHelper();
helper.getMovieById(id, "en", new TmdbHelper.ResultListener() {
#Override
public void onResultReceived(JSONObject result) {
// called when finished downloading
try {
String rating = result.getString("vote_average");
AnimationHelper.circularReveal(fab, 500, 0);
fab.setText(rating);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
AnimationHelper:
public static void circularReveal(final View view, final long duration, long startDelay) {
// get the center for the clipping circle
int cx = (view.getLeft() + view.getRight()) / 2;
int cy = (view.getTop() + view.getBottom()) / 2;
// get the final radius for the clipping circle
int finalRadius = Math.max(view.getWidth(), view.getHeight());
// create the animator for this view (the start radius is zero)
Animator anim =
ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
anim.setDuration(duration);
anim.setStartDelay(startDelay);
anim.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
view.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {}
});
// make the view visible and start the animation
anim.start();
}
I use the circular reveal animation in other parts like this to make sure the view is attached, and it works:
headerContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
headerContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
AnimationHelper.circularReveal(headerContainer, 500, 200);
}
});
Perhaps you should erase this line inside your onResultRecieved():
fab.setVisibility(View.VISIBLE);
My assumption is that your circular reveal method is working just fine. It is because of you have made the FAB visible before the animation even begin, you can't see it in action.
As an addition, those lines you've shown which is working doesn't have fab.setVisibility(View.VISIBLE) called anywhere in it.
1st Approach:
Try Transition Listener.
getWindow().getSharedElementExitTransition().addListener(new Transition.TransitionListener() {
#Override
public void onTransitionStart(Transition transition) {
}
#Override
public void onTransitionEnd(Transition transition) {
}
#Override
public void onTransitionCancel(Transition transition) {
}
#Override
public void onTransitionPause(Transition transition) {
}
#Override
public void onTransitionResume(Transition transition) {
}
});
2nd Approach: Try setting start delay and listener to the reveal animation and when animation starts then set the view visible
if (Build.VERSION.SDK_INT >= 21) {
Animator anim = ViewAnimationUtils.createCircularReveal(viewRoot, cx, cy, 0, finalRadius);
anim.setStartDelay(300);
anim.setDuration(1000);
anim.setInterpolator(new DecelerateInterpolator());
anim.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
viewRoot.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
anim.start();
}
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
anim.start();