how to access a particular item in the animation list - android

I have a animation list contains the following items
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res
/android"android:oneshot="true">
<item
android:drawable="#drawable/b1"
android:duration="500"/>
<item
android:drawable="#drawable/delay"
android:duration="1000"/>
<item
android:drawable="#drawable/b1"
android:duration="500"/>
<item
android:drawable="#drawable/delay"
android:duration="1000"/>
<item
android:drawable="#drawable/b2"
android:duration="500"/>
Now, when the animation is running or has started by the following
code,
img1 = (ImageView)findViewById(R.id.stimulichangeid);
img1.setBackgroundResource(R.drawable.erp_simple_presentationlist1);
AnimationDrawable splashanimation = (AnimationDrawable)img1.getBackground();
splashanimation.start();
I want to click some button when in animation list "b2" drawable
appears on the imageview.But in a running animation, how to access a
particular item(in my case b2). When b2 comes i want to press a button
and do something. i was thinking to assign id's to every item. if yes,
then how to access that particular item with its id when the
animation is running. please suggest some way to solve this problem.
thanks in advance

AnimationDrawable isn't kind of layout so you can't catch any child view with order. It just replaces a drawable with a new one, so you need a kind of listener returns the order of new drawable. And native implementation doesn't have these kind of listener.
There is some custom AnimationDrawables and you can modify one of these for your need.

Related

gif with Listview in Android

I am new to Android. I Have Listview in My Activity and it's working Fine. But suddenly in mind one thought is going on. Is there any way to put gif animation in background of my Listview and display data to on gif Image. Let's say that In Our Listview we Have set background color as follow.
android:background="#color/Mycolor
so it can change the whole Listview color But I want to
display gif Animation Like small Image of Birds are flying in
Background
. If it is possible then give suggestion.
Any Help will be Appreciated.
I think this is the way you can set,first you need to divide gif animation in indvidual frame,then you need to create one xml for background and use animation-list and add all the frames of gif image and set that xml as background
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="#drawable/frame1"
android:duration="1000"/>
<item
android:drawable="#drawable/frame2"
android:duration="1000"/>
<item
android:drawable="#drawable/frame3"
android:duration="1000"/>
....
</animation-list>
use an Imageview in row background and play gif on it using this component.

Can I make the background of the android application as group of images?

I need help in make the background of my app as gallery of photos... is that possible? and how to do that?
You can use a GridView to show images in column and row fashion. For more information you can take a look at this article for implementing gridview.
You can do this by assigning an id to your parent layout and then in java you can change the background image after a certain action is performed.
You can use Asyymmetric Gridview to achive attractive view of gallery
you can use this library
Yes You can I did it before by creating an animation that contain the images then I set this animation as backgroud
// Type casting the Image View
ImageView view = (ImageView) findViewById(R.id.imageView1);
// Setting animation_list.xml as the background of the image view
view.setBackgroundResource(R.anim.animation);
// Type casting the Animation drawable
AnimationDrawable frameAnimation = (AnimationDrawable) view.getBackground();
frameAnimation.start();
example animation xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="#drawable/image_1" android:duration="3000" />
<item android:drawable="#drawable/image_2" android:duration="3000" />
<item android:drawable="#drawable/image_3" android:duration="3000" />
</animation-list>

Android: Detail animation inside Floating Action Button

I am trying to get familiar with the new design guidelines of Android Lollipop. So especially for animations, I try to achieve something like these detailed animations for icons or buttons: http://www.google.com/design/spec/animation/delightful-details.html
I was wondering how they did that?
In my special case, I have a floating action button, which is used to drop an item in a shopping cart. After the user presses the button, I want to animate the icon drawable inside that button. The animation shall have a sliding effect where the cart is moved out to the bottom of the button and the check mark is moved in from the top of the button.
I found a ViewFlipper (http://www.inter-fuser.com/2009/07/android-transistions-slide-in-and-slide.html), however I want to keep the button in place and only animate the icon inside the button.
On the other hand I found AnimationDrawables (http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html) but there, I have to create each frame by hand, which is also odd.
What's the best way to achieve that?
I wonder if fab support that now, however, you can always create your own.
Create a custom linear layout that contains image view. Make it round like fab. You can then look at this animation examples
Hope it helps. Happy codings.
You could use a frame animation:
you create 15-20 frames for the animation you want to make.
You create an animation list drawable, for example animation_list.xml:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item
android:duration="50"
android:drawable="#drawable/frame_01"/>
<item
android:duration="50"
android:drawable="#drawable/frame_02"/>
<item
android:duration="50"
android:drawable="#drawable/frame_03"/>
<item
android:duration="50"
android:drawable="#drawable/frame_04"/>
<item
android:duration="50"
android:drawable="#drawable/frame_05"/>
<item
android:duration="50"
android:drawable="#drawable/frame_06"/></animation-list>
Then you put this drawable as a src inside your custom FAB.
Now you can start the frame by frame animation (when the FAB animation finishes):
ImageView mImageViewFilling = (ImageView) findViewById(R.id.animated_imageview);
((AnimationDrawable) mImageViewFilling.getBackground()).start();

Android Animation with onDraw or ImageView?

I want to create an android game. Everytime someone touches the display the player should go up, and if he releases the player should fall.
With the help of many tutorials I made it work but now I want to animate it and I'm stuck. That means the image of the player should be changed every half a second. Additionally an animated rotation should be created when the player goes up.
However (after hours of googling) I couldnt find any helpful answer to my problem. The Android Developers site talks about creating an ImageView and a XML file. But thats where I'm stuck: I dont have an ImageView, my player (for which I used a PNG file) is simply created by the onDraw() method:
public void onDraw(Canvas canvas) {
for (Sprite s : sprites) {
canvas.drawBitmap(s.getGraphic(), s.getLocation().x,
s.getLocation().y, null);
}
}
Now I wanted to ask how I should do the animation and the animated rotation. Should I start off with an ImageView or can I somehow "convert" the onDraw method to an ImageView? Or is there another way to do the animation and animated rotation without an ImageView?
Secondly, if I had to create the ImageView I don't understand how I can make the player "dynamic", i.e.: changing the position when someone touches the display.
Thanks in advance :)
EDIT:
Ok, I created my animation.xml file in the drawable folder:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" android:id="#+id/splashAnimation">
<item android:drawable="#drawable/ship" android:duration="200" />
<item android:drawable="#drawable/ship_2" android:duration="200" />
</animation-list>
and in my main file I added:
ImageView img = (ImageView) findViewById(R.id.splashAnimation);
img.setBackgroundResource(R.drawable.animation);
ship_anim= (AnimationDrawable) img.getBackground();
ship_anim.start();
However, now I get the error message: NullPointerException
Where is the Problem?
if you have drawables that describe animation, you can always create xml in your drawable folder like this than add this as imageView to the layout
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/img1" android:duration="100" />
<item android:drawable="#drawable/img2" android:duration="100" />
<item android:drawable="#drawable/img3" android:duration="100" />
<item android:drawable="#drawable/mimg4" android:duration="100" />
<item android:drawable="#drawable/img5" android:duration="100" />
</animation-list>
The code for it would look like this:
myAnimation = (AnimationDrawable) findViewById(R.id.animation);
Add just event handler than
if (!isPlaying)
{
playDrawableAnimation();
isPlaying = true;
} else
{
faceAnimation.stop();
isPlaying = false;
}

animation-list not animating initially

I have a checkbox that should look like a green light when checked and a red blinking light when unchecked. To do this I created a selector called connected_selector.xml.
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/green_button" />
<item android:state_checked="false" android:drawable="#drawable/red_button_blinking" />
<item android:drawable="#drawable/red_button_blinking" />
</selector>
The green_button is simply a png and the red_button_blinking is an animation-list of pngs.
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="#drawable/red_button" android:duration="500" />
<item android:drawable="#drawable/red_button_lit" android:duration="500" />
</animation-list>
The checkbox's background is set to #drawable/connected_selector. If the checkbox is initially unchecked, it doesn't blink, it just shows the #drawable/red_button. However if I check the box and then uncheck it, the checkbox will animate correctly.
How can I make the animation start initially since the checkbox will be initially unchecked? I guess I could try to start the animation manually in code, but I don't think that should be necessary.
Okay, I found an ugly, dirty hack to get around this. Note that I don't have the slightest clue why this happens, just a way around this. It is probably not even a generic way since different devices will have different loading times.
I execute the following workaround in onPostResume() to minimize the necessary delay time.
if(onOffStatus) {
// SLEEP 0.5 SECONDS HERE ...
new Handler().postDelayed(new Runnable() {
public void run() {
switcher.setBackgroundResource(R.drawable.button_state_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) switcher.getBackground();
// Start the animation (looped playback by default).
frameAnimation.start();
}
}, 500); // Actual required time will probably be dependent on device performance
}
It ain't pretty but I'm going with this until I find something better. Let me know if I overlooked something here.

Categories

Resources