I want to set Animation to images of in image view like in newsstand application and change image after particular interval of time.
Please help its very important for me.
for animating a view (image view included) i recommend this library
set the animation time and when it ends change the image, full code for the code snippet
rope = YoYo.with(Techniques.FadeIn).duration(1000).playOn(mTarget);// after start,just click mTarget view, rope is not init
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Techniques technique = (Techniques)view.getTag();
rope = YoYo.with(technique)
.duration(1200)
.interpolate(new AccelerateDecelerateInterpolator())
.withListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
//change image here
}
#Override
public void onAnimationCancel(Animator animation) {
Toast.makeText(MyActivity.this, "canceled", Toast.LENGTH_SHORT).show();
}
#Override
public void onAnimationRepeat(Animator animation) {
}
})
.playOn(mTarget);
}
});
Related
I have a recyclerview which has animation added for each item in onBindViewHolder() which results in multiple calls for single item in recycler view.
My requirement is to zoom in image on selection of an item in recycler-view. I have started an animation in onBindViewHolder() for each item which has resulted in continuous calls of onBindViewHolder() for same item.
#Override
public void onBindViewHolder(#NonNull BaseViewHolder viewHolder, int viewType) {
viewHolder.imageView.animate().scaleX(scaleX).scaleY(scaleY).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
viewHolder.imageView.animate().setListener(null);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
}).setDuration(THUMBNAIL_ZOOMING_DURATION).start();
}
Imageview should zoom in by 1.15f when an item gets selected with animation and should get zoom out if deselected.
onBindViewHolder is not a good place for animations. Try using onViewAttachedToWindow to start and onViewDetachedFromWindow to stop animations
I'm creating an application for Android, and in this moment i want just create a selectable list using a RecyclerView, for this, when a user clicks on an item, I put its id in an array and I change its icon.
The problem is that, when I scroll down and then I scroll up again or when I change fragment, the array still contains all the id of the items, but the icons comes back as before, they don't mantain the new image that I set.
This is the onBindViewHolder metod, this method is the only in the whole project which change the imageview :
List<Integer> selected = new LinkedList<>();
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mBoundString = mValues.get(position);
holder.mTextView.setText(mValues.get(position));
final int pos = position;
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Context context = v.getContext();
Animation to_middle = AnimationUtils.loadAnimation(v.getContext(), R.anim.to_middle);
final Animation from_middle = AnimationUtils.loadAnimation(v.getContext(), R.anim.from_middle);
to_middle.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
holder.mImageView.setImageResource(R.drawable.ic_action_navigation_check);
holder.mImageView.clearAnimation();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
holder.mImageView.setAnimation(to_middle);
holder.mImageView.startAnimation(to_middle);
selected.add(pos);
}
});
if(!selected.contains(position)) {
/* I checked and this method it's not called when
an item is selected, so the following instruction
isn't the cause of my problem, anyway the image
comes back as before */
Glide.with(holder.mImageView.getContext())
.load(R.drawable.ic_default)
.fitCenter()
.into(holder.mImageView);
}
}
I'm sure that R.drawable.ic_default isn't used elsewhere, so which method is changing the image?
Thank you
if(selected.contains(position)) { // Check if already selected
Glide.with(holder.mImageView.getContext())
.load(R.drawable.ic_action_navigation_check) // set selected drawable here.
.fitCenter()
.into(holder.mImageView);
}
You are checking that "if not selected set image to default" but you need to check "if selected set image to selected image".
I am trying to have a view animated in my app and am using NineOldAndroid for animations.
The desired effect is to fade the view out and then set it's visibility to gone so that it doesn't get clicked while invisible. Here is How I do it.
ViewPropertyAnimator.animate(view).alpha(0).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.GONE);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
The problem here is that the listener above sticks with the view and when I try to fade it in again the listener gets called again resulting the view to be GONE upon appearing.
ViewPropertyAnimator.animate(enterGallery).alpha(1);
How can I clear the listener after the view visibility is set to GONE in the first piece of code?
I found the solution and it would be to pass null as listener when making the view VISIBLE.
ViewPropertyAnimator.animate(view).alpha(1).setListener(null);
So when I click a list item in my ListView it gets animated. The problem is that when it's animating I can still click it and this is undesirable. I'm using nineOldAndroids and to disable the ListItem I do:
set.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
v.setClickable(false);
v.setEnabled(false);
}
#Override
public void onAnimationEnd(Animator animator) {
v.setClickable(true);
v.setEnabled(true);
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
setClickable behaves strangely: it does let me click the list item for a brief time after the animation starts and after the animation ends it won't let me click it anymore (even though I set it to true). And setEnabled doesn't work at all.
How can I disable a list Item while an animation is running?
make the adapter isEnabled(int position) method return false while the animation on that item is going on.
add this to your adapter
Set<Integer> disabledItems;
private void setRowEnabled(int position,boolean enabled){
if(enabled){
disabledItems.remove(position);
}else{
disabledItems.add(position);
}
}
#Override
public boolean isEnabled(int position) {
return !disabledItems.contains(position);
}
And call the setRowEnabled from the onAnimationStart onAnimationEnd of your Animation
I am willing to create a news bar in android application and I used the animation to move TextView from left to right and fill it each round with a new text from array of strings. I faced a lot of problems and I used more than 5 ways but each one has a bad behaviour. the main problem is that when the animation ends it refreshes the textView and it appears like flashing which is not a user-friendly behaviour.
Here is a code snippet:
TextView my_text;
Animation slideRight;
Animation slideLeft;
String [] text = {"TwoOneOneOneOneOneOneOneOneOneTwo","OneTwoTwoTwoTwoTwoTwoTwoTwoTwoTwoOne",
"OneThreeThreeThreeThreeThreeThreeThreeOne","OneFourFourFourFourFourFourFourOne",
"OneFiveFiveFiveFiveFiveFiveFiveOne","OneSixSixSixSixSixSixSixOne",
"OneSevenSevenSevenSevenSevenSevenSeveOne","OneEightEightEightEightEightEightEightOne",
"OneNineNineNineNineNineNineNineOne"};
int arr_length;
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arr_length = text.length;
slideRight = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
slideRight.setDuration(2000);
slideRight.setRepeatCount(Animation.INFINITE);
slideRight.setAnimationListener(slideRightListener);
slideLeft = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
slideLeft.setDuration(2000);
slideLeft.setAnimationListener(slideLeftListener);
my_text = (TextView) findViewById(R.id.textView1);
my_text.setVisibility(TextView.VISIBLE);
my_text.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), my_text.getText(), Toast.LENGTH_SHORT).show();
}
});
slideLeft.setAnimationListener(slideLeftListener);
my_text.startAnimation(slideLeft);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
AnimationListener slideLeftListener = new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
my_text.setVisibility(TextView.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
my_text.setVisibility(TextView.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
my_text.setVisibility(TextView.VISIBLE);
my_text.startAnimation(slideRight);
}
};
AnimationListener slideRightListener = new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
my_text.setVisibility(TextView.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
my_text.setVisibility(TextView.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
my_text.setVisibility(TextView.VISIBLE);
if(count<arr_length)
{
my_text.setText(text[count]);
count+=1;
my_text.startAnimation(slideLeft);
}
else
{
count=0;
my_text.setText(text[count]);
my_text.startAnimation(slideLeft);
}
}
};
}
I think ViewFlipper is more suitable for you.
Try adding setFillAfter(true) as that might stop the "refresh" behavior at the end of the Animation.
As I commented on #Bill above I realized that the refresh issue related to Android version.
I have tested the same code above on devices with Android Ice-Cream-Sandwich and it didn't "refresh".
If there is any solution on non-ICS I will accept it.
I've got this link and I found it great to me and can be customized as my requirements.
It uses HorizonalSlideshow.