I created a game in AndroidStudio, where you can move right and left through the buttons. Now I would like to add an animation. I have this image:
image
How do I change the animation when I click the left button and then stop it when no touch the button?
Is there a guide? Thanks for all.
First of all you have to create a xml and place the images. You have 6 frames in the sprite, you have to cut it making 6 images. Create a xml (e.g. run_animation.xml) and reference the images in there. For example like this:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/run_0001" android:duration="50"/>
<item android:drawable="#drawable/run_0002" android:duration="50"/>
<item android:drawable="#drawable/run_0003" android:duration="50"/>
<item android:drawable="#drawable/run_0004" android:duration="50"/>
<item android:drawable="#drawable/run_0005" android:duration="50"/>
<item android:drawable="#drawable/run_0006" android:duration="50"/>
</animation-list>
If you don't want to loop the animation, you can use this in the xml: android:oneshot="true"
For example:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
Java:
runAnimation.setImageResource(R.drawable.run_animation);
AnimationDrawable idleAnimation = (AnimationDrawable)runAnimation.getDrawable();
idleAnimation.start();
In order to run the animation using a button, place the Java code above in the button click listener. For example:
btnRight.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// run animation
runAnimation.setImageResource(R.drawable.run_animation);
AnimationDrawable idleAnimation = (AnimationDrawable)runAnimation.getDrawable();
idleAnimation.start();
}
});
Related
I need to design a component for animation rendering. Because you probably are much smarter than me I ask your opinion.How would you do the following design?
More precisely I want to have a horse which can run, stop, jump and dodge.
I can obtain all the frames needed for all these states. For example, I can draw all the frames containing the horse running(f[1] - f[n]). But what if the horse needs to dodge while running on f[k] frame (1
How can I design this type of transitions? What classes would you use? Do I need to draw transitions for every f[k]?
More details if helps: Design will be implemeneted in Android SDK.
Thank you very much!
Please try below code . hope it will work
You need to create animation-list under res/drawable
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/animationFrame"
android:oneshot="false" >
<item
android:drawable="#drawable/es0"
android:duration="100"/>
<item
android:drawable="#drawable/es1"
android:duration="100"/>
<item
android:drawable="#drawable/es2"
android:duration="100"/>
<item
android:drawable="#drawable/es3"
android:duration="100"/>
<item
android:drawable="#drawable/es4"
android:duration="100"/>
</animation-list>
Write below code in your activity
private AnimationDrawable m_frameAnimation = new AnimationDrawable();
imageView.setBackgroundResource(R.drawable.animationFrame);
m_frameAnimation = (AnimationDrawable) imageView.getBackground();
m_frameAnimation.start();
This is my code and resultpage1 and resultpage2 is my .jpg file.This is my background image and i need this to be animated .The image is full of hearts and i need the hearts to fall down from top.Background image is added succesfully but animation is not proper.The hearts while falling are shaking rather falling down slowly.I need the hearts to fall down slowly instead shaking.Iam new to android.Please help
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/resultpage1"
android:duration="50"/>
<item
android:drawable="#drawable/resultpage2"
android:duration="50"/>
<item
android:drawable="#drawable/resultpage3"
android:duration="50"/>
<item
android:drawable="#drawable/resultpage4"
android:duration="50"/>
<item
android:drawable="#drawable/resultpage5"
android:duration="50"/>
</animation-list>
You should be using optimized png images for this. You also are going to need more than two images in most cases. Your animation will almost have a strobe affect if you only use two images. Here is more information regarding different xml animations
http://www.androidhive.info/2013/06/android-working-with-xml-animations/
Today I got, as I suppose, very interesting question. On my job we are creating a dating service and one of the main features will be the screen, where photos of different girls (or guys) are shown and the user press either "Hot" or "Not" button. Both of these buttons are displayed below the photo on the screen.
Our analytics say that we should implement some kind of "gaming" mechanics, so the want such a thing: when user press, for example, "Not" button - it starts freezing and covering with ice, then it breaks and then the next photo is shown. That is not scale or rotate or translate animation... the button itself, its content should be changing over some small period of time (a second or two maybe).
That scares me a lot, because when I'm thinking about scaling these buttons on different devices, and different troubles with ninepatch and hand-made-frames animations I'm likely going to make.
Is it possible to make such thing? Or maybe there are any kind of workarounds or something.
Make images for your animation and keep in a folder in png format.
Animate those images with code below.
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="#drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="#drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="#drawable/rocket_thrust3" android:duration="200" />
</animation-list>
Then
AnimationDrawable rocketAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
rocketAnimation.start();
return true;
}
return super.onTouchEvent(event);
}
Create list of images in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/loading"
android:oneshot="false" >
<item android:drawable="#drawable/preloader_01" android:duration="50" />
<item android:drawable="#drawable/preloader_02" android:duration="50" />
<item android:drawable="#drawable/preloader_03" android:duration="50" />
<item android:drawable="#drawable/preloader_04" android:duration="50" />
Use ImageView img instead of button and set the list as background in xml, then in code use AnimationDrawable to start the animation
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
// Start the animation (looped playback by default).
frameAnimation.start();`
You can animate Alpha, location and just about any property of a button using ObjectAnimators and ValueAnimators.
If you want custom animations such as the ice effect, you'll have to make these yourself.
I am simply trying to create a spinning progress dialog. No text, no borders, no background. Just a spinning notification that lightly is center on the screen on top of content.
I have seen two different Stack Overflow questions in creating this custom Dialog.
They both use this styling:
<style name="NewDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:colorBackground">#ffffff</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:width">600dip</item>
<item name="android:height">100dip</item>
<item name="android:textColor">#FF0000</item>
</style>
But in the Java side, one extends Dialog and contains lots of custom content, the other one is simply this:
public class MyProgressDialog extends ProgressDialog {
public MyProgressDialog(Context context) {
super(context, R.style.NewDialog);
}
}
On the first (extends Dialog), I get a blank. No dialog. Nothing. On the code sample just above, I get a shrunken small dialog spinning inside a black box that is off centered.
Which method is more correct and can I have a sample on how to implement this?
Also, how can I make that dialog transparent/borderless?
The 'right way' to do this now would be to extend DialogFragment http://developer.android.com/reference/android/app/DialogFragment.html
If you are not using the compatibility library, you are not programming in the current Android world.
If you want to extend Dialog, then I have some example code here: https://github.com/tom-dignan/nifty/tree/master/src/com/tomdignan/nifty/dialogs In this code, I implemented a kind of 'progress dialog' by extending Dialog. I extended Dialog because I wanted full control and wanted my Dialog to be full-screen.
I would recommend reading the above DialogFragment doc and using that, though.
So really, there is no right or wrong way here. Extending any of the three classes will work, but the cleanest and most current would be the DialogFragment approach.
One way of doing it is to use a custom dialog, request feature No_Title and set the background drawable resource to transparent.
You can use the following code, I just wrote and tested it out on Android 4.0.3 ICS.
The xml layout in layout folder is, say dialog_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:id="#+id/dialogProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
The java code to display the dialog and make it transparent is as follows.
Dialog dialog = new Dialog (this);
dialog.requestWindowFeature (Window.FEATURE_NO_TITLE);
dialog.setContentView (R.layout.dialog_progress);
dialog.getWindow ().setBackgroundDrawableResource (android.R.color.transparent);
dialog.show ();
Something similar can be achieved very easily by using frame-by-frame animation on an ImageView instead of making custom progress dialog. For this you just need a sequence of images(can be extracted from any gif). And then you can apply frame-by-frame animation on it. It will do exactly what you want.
I have done something similar with the code below -:
// XML for frame by frame animation
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="#drawable/frame00"
android:duration="150"/>
<item
android:drawable="#drawable/frame01"
android:duration="150"/>
<item
android:drawable="#drawable/frame02"
android:duration="150"/>
<item
android:drawable="#drawable/frame03"
android:duration="150"/>
<item
android:drawable="#drawable/frame04"
android:duration="150"/>
<item
android:drawable="#drawable/frame05"
android:duration="150"/>
<item
android:drawable="#drawable/frame06"
android:duration="150"/>
Java code for starting animation :-
ImageView iv = (ImageView)findViewById(R.id.progressDialog);
iv.setBackgroundResource(R.anim.progress_dialog_icon_drawable_animation);
AnimationDrawable aniFrame = (AnimationDrawable) iv.getBackground();
aniFrame = (AnimationDrawable) iv.getBackground();
aniFrame.start();
Hope it helps !
I want an animated gif, since this isn't possible in Android I am using individual frames in a transition.
except it seems like the transition class only will show two frames ever! I saw other animation methods but they didn't seem to apply to what I was doing, or seemed old and convulated like for an older infant android build
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/activateanima"></item>
<item android:drawable="#drawable/activateanimb"></item>
<item android:drawable="#drawable/activateanimc"></item>
<item android:drawable="#drawable/activateanimc"></item>
<item android:drawable="#drawable/activateanimd"></item>
<item android:drawable="#drawable/activateanime"></item>
<item android:drawable="#drawable/activateanimf"></item>
<item android:drawable="#drawable/activateanimg"></item>
</transition>
How do I animate an image to behave like an animated gif, in place. no rotations or translations here. Using android 2.1+
Are you after a Frame animation? See: here. This will play a standing still animation.
Example from above site:
XML file saved at res/anim/rocket.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/rocket_thrust1" android:duration="200" />
<item android:drawable="#drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="#drawable/rocket_thrust3" android:duration="200" />
</animation-list>
To use:
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();
Just use a view flipper to flip between the images. Just define your in and out animations to be 0seconds long and they should be instantianous. (but use alpha to be sure). View flipper has the benefit of also auto animating and auto starting