Animating a custom list view - android

I have searched the forum for this , but found no similar question, okay so I have a customised list view, Each entry in this list is a text view and an image. Suppose the user adds the text in some activity.and presses the save button. When pressing the back button to come back to this list view. The entry just added should swipe in from the right.How can I do that, any example would help.

Here is one working example from bottom to top, I guess you can turn that into right to left ;-)
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//Do the animation once.
if (position > maxposition) {
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(400);
set.addAnimation(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(500);
set.addAnimation(animation);
row.startAnimation(set);
maxposition = position;
}
return row;
}

Thank you everyone for your contribution, I did some more digging, and found that interpolators provide animations to views. Referred an example, implemented it and got what I wanted.

Related

Touch targets do not move with object when using translate animation?

Whenever I use TranslateAnimation to move an object to a new location, for some reason, the touch target of that object remains in the old position.
How do I change this behaviour?
eg.
public static void hideViewUp (View v, int duration) {
AnimationSet animSet = new AnimationSet(true);
animSet.setFillAfter(true);
animSet.setDuration(duration);
AlphaAnimation alp = new AlphaAnimation(1.0f, 0);
TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
Animation.RELATIVE_TO_SELF,
Animation.RELATIVE_TO_SELF,
-(v.getTop() + v.getHeight()));
animSet.addAnimation(translate);
animSet.addAnimation(alp);
v.startAnimation(animSet);
}
There's no need to post any sample code - the issue is indeed pretty well known and general.
TranslateAnimation and all other old types of animation change only the draw transformation matrix. These animations shouldn't be used to move items permamently.
To truly move a view to a new position, use ObjectAnimator.
Here's an example: http://www.android-app-market.com/animations-in-android-translate-animation-alpha-animation-object-animation.html

How to animate individual elements in ListView

I'm doing a small Android app based around a ListView. When the user selects one or more elements in the list and subsequently selects a menu item from the ActionBar I would like to do a small animation on the selected elements in the list, and this is where things go wrong.
Nothing animates - nor does anything fail. The following code piece is a simplified version of what I'm doing:
private void animateListViewItem()
{
TranslateAnimation anim = 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);
anim.setDuration(2000);
View v = fragment.getListAdapter().getView(fragment.getListView().getFirstVisiblePosition(), null, null);
v.startAnimation(anim);
}
When I messed around with it, trying to figure out what was wrong, I at one point substituted the item with the entire ListView to rule out the animation as the source of the problem - like this.
private void animateListViewItem()
{
TranslateAnimation anim = 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);
anim.setDuration(2000);
fragment.getListView().startAnimation(anim);
}
To my amazement, that worked perfect!
So my question is - why can't I animate the individual elements in a ListView? Or is there something I am doing wrong?
Thanks!
P.S. For the record the ListView is populated with custom views (LinearLayouts), and I have checked that I get the right item before animating.
I found out what the issue was:
View v = fragment.getListAdapter().getView(fragment.getListView().getFirstVisiblePosition(), null, null);
This line was the problem. It returns a new View to display the underlying data at the specified position in the list not the existing View. So the returned View has nothing to do with the list.
Instead doing this:
View v = fragment.getListView().getChildAt(fragment.getListView().getFirstVisiblePosition());
Got me the View that the list was using and the animation worked as expected.

Shading sub Layout in android

I am stuck in a condition that I can't keep the sub layout faded after using the fade animation
FrameLayout mapLayout = (FrameLayout)findViewById(R.id.mapLayout);
Animation fadeAnim = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
mapLayout.startAnimation(fadeAnim);
Please guide how can I achieve this in android. This animation and shaded effect occurs when I click the 'Pause' Button and to reverse the effect I will press the 'Resume' Button(Sorry I made the image with 'Pause' button in shaded view, in actual it is 'Resume')
Try
fadeAnim.fillAfter(true);
fadeAnim.execute();
http://developer.android.com/reference/android/view/animation/Animation.html#setFillAfter(boolean)
hii makki you can set the below code at your onclick event
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
set.addAnimation(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(100);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
l.setLayoutAnimation(controller);
then fade out animation
public static Animation runFadeOutAnimationOn(Activity ctx, View target) {
Animation animation = AnimationUtils.loadAnimation(ctx,android.R.anim.fade_out);
target.startAnimation(animation);
return animation;
}
Both above answers helped me in solving the issue, but both answers weren't the actual solution to my problem. The solution I made is
I defined two animations according to my need
Animation fadeOut = new AlphaAnimation(0f,0.5f);
Animation fadeIn = new AlphaAnimation(0.5f,1f);
fadeOut.setFillAfter(true);
fadeIn.setFillAfter(true);
When I clicked 'Pause' button I executed following line
mapLayout.startAnimation(fadeOut);
Then to resume from paused state, I executed following line
mapLayout.startAnimation(fadeIn);
Thanks Simon and Deepak!

How to do webview animation in android

suppose i have three text as heading.Any how you can take textview or anything.one text on left,one in center and one in right side.so i want an animation when i will slide the screen left then left text will be gone,center text will come to left and right text will come to center.Same thing for right slide.Can anyone suggest how can i achieve this type of animation.Plz help.
You can try to create an animation variable :
private Animation leftAnimation() {
Animation outtoLeft = 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);
outtoLeft.setDuration(500);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
then
set this action to your frame:
public void onLeftArrowClick(View v) {
frame.setAnimation(moveComingSoonToBottomAnim); // your Frame variable
animVariable.startNow();// your anime variable
frame.setVisibility(View.VISIBLE);
rightFrame.setVisibility(View.GONE); // hide your right text view
}

Android Listview Refresh animation

I have simple ListView that displays data from database. Every after 1 min data are getting refresh automatically. Here is the code snippet I am using to do that :
DataAdapter adp = (DataAdapter) DataList.getAdapter();
adp.UpdateDataList(DataAdapter.DATA_LIST);
adp.notifyDataSetChanged();
DataList.invalidateViews();
DataList.scrollBy(0, 0);
I have created ListView i.e. DataView and dataAdapter that simply extends baseAdapter.
UpdateDataList is simply get data from database and creates an arrayList.
And Adapter notifies view to refresh the Data.
Everything is working perfect.
Now one thing I am trying to do here is when data refresh I need to add some kind of animation so that it becomes eyecaching. And people mark that something happened.
Similar to iPhone application. I do not want to add spinner because data update is synchronize process so data change is quickly without making any new changes in view. Simply numbers got flipped.
Any help will be appreciated....
Here is the code I tried and worked well for me....
/* Setting up Animation */
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(400);
set.addAnimation(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(400);
set.addAnimation(animation);
LayoutAnimationController controller =
new LayoutAnimationController(set, 0.25f);
parent.setLayoutAnimation(controller);
/* Animation code ends */
Maybe you can attach an animation to your ListView, since ListView extends ViewGroup you can do it, read the LayoutAnimationController on android reference: http://developer.android.com/reference/android/view/animation/LayoutAnimationController.html
cheers

Categories

Resources