Using FrameAnimations and AnimationDrawable to display images not working - android

I have a simple .xml layout file that has a simple ImageView, that I want to switch images on. I decided to go ahead and do it with FrameAnimations, using the AnimationDrawable API.
I have managed to create my animation file in "drawable/file.xml", here is the code:
<?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_010_1" android:duration="1000" />
<item android:drawable="#drawable/image_010_2" android:duration="1000" />
</animation-list>
Now, Here is the code that should start my animation:
Image1.setBackgroundResource(R.drawable.image_010);
AnimationDrawable animation = (AnimationDrawable) Image1.getBackground();
animation.start();
For some reason, When i run the app I just see the first frame, and the next image does not come after 1000 milis, or at all! What am I doing wrong? Thanks!

so I managed to solve this problem by adding a Runnable to my ImageView:
image.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
animation.start();
}
});

Related

Animation android start immediately

I'm doing the following simple animation in Android:
<?xml version="1.0" encoding="UTF-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:duration="2000"
android:interpolator="#android:anim/linear_interpolator"/>
I call the animation like this:
#Override
public void onWindowFocusChanged (boolean hasFocus) {
if (hasFocus) {
tvload.startAnimation(anim);
}
}
The animation starts immediately in pre-lollipop devices but in lollipop and Marshmallow it takes around 1 sec for it to start. This animation should start after intent.
Is there a way to make this animation start immediately instead of having the 1 second load time?
I would give the View#post() method a try. It will run the code you post next UI thread cycle which is usually as soon as the View is visible.
tvload.post(new Runnable() {
#Override
public void run() {
tvload.startAnimation(anim);
}
};

How to create Animated Splash Screen such this

I need to make such animated splash screen. Can you help me?
One way is to use Tween Animation like the following. In your case needs more than one anim file
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.myanimation);
Say like if you have to animate white circle image then do the following
ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move);
image.startAnimation(animation1);
Now you need to create anim file in res/anim/move.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toXDelta="75%p"
android:duration="800" />
</set>
This is an example. You need to find ways to modify these basic animations for your requirement . For more refer this link
To use a gif for your splash screen.
In your build.gradle file:
compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.+'
In your activities layout:
<pl.droidsonroids.gif.GifImageView
android:id="#+id/gifView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/gif"
/>
And finally in your class file:
private static int SPLASH_TIME_OUT = 1500;
private boolean isInFront;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_gif);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
if(isInFront)
{
// Start your app main activity
Intent i = new Intent(SplashScreen_Gif.this, MainMenuActivity.class);
startActivity(i);
}
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
You can use a gif image like this and use it on your splash screen instead of a normal jpg or png image

Select images from Galley and add animation

I am creating an app where user selects images from gallery and selects a song of his/her choice and on click of a button 'Run Movie'. Animation is played along with the song.
I am stuck at the animation playing part it plays only music and does not show any pictures.
I was successful playing animation with sound when I had images picked from drawables and specified in the animation.xml. Kindly guide me where I am making a mistake.
Update: I am able to get all image and song paths which user has selected.The only problem is with playing frameanimation.
Here is the code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation_sound);
m_imageView = (ImageView)findViewById(R.id.imageView1);
Bundle bundle = getIntent().getExtras();
imgPathArr = bundle.getStringArray("picsPath");
songPath = bundle.getString("songPath");
m_imageView.setBackgroundResource(R.drawable.animation);
frameAnimation = (AnimationDrawable)m_imageView.getBackground();
if(imgPathArr == null || imgPathArr.length == 0){
Toast.makeText(this, "Please selct images first", Toast.LENGTH_LONG).show();
}else{
for(int i =0 ; i< imgPathArr.length; i++){
Log.d("Activity", imgPathArr[i]);
**frameAnimation.addFrame(
Drawable.createFromPath(imgPathArr[i]), 5000);**
}
}
animation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="4000"
android:fromAlpha="0.0"
android:toAlpha="0.9" >
</animation-list>
My code for Animation with sound when I am picking images from drawables
m_imageView.setBackgroundResource(R.anim.anim);
frameAnimation = (AnimationDrawable)m_imageView.getBackground();
anim.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/pic1"
android:fromAlpha="0.0"
android:toAlpha="0.9"
android:duration="5000"/>
<item
android:drawable="#drawable/pic2"
android:fromAlpha="0.0"
android:toAlpha="0.9"
android:duration="5000"/>
<item
android:drawable="#drawable/pic3"
android:fromAlpha="0.0"
android:toAlpha="0.9"
android:duration="5000"/>
</animation-list>
"I was successful playing animation with sound when I had images picked from drawables."
If that worked for you then you can convert your images into BitmapDrawables by using these methods
BitmapDrawable(Resources res, String filepath)
BitmapDrawable(Resources res, Bitmap bitmap)
from the documentation
"A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a BitmapDrawable from a file path, an input stream, through XML inflation, or from a Bitmap object."
Use it like this
Drawable d =new BitmapDrawable(getResources(),filepath);
Hope it workes for you. Cheers :)

How to set animation when I click on button at that time move image from left to right and right to left in android?

I wanna translate 2 images image When I click on Button, At that time from left image move left to right and right image move right to left(both image come in center at that time stop animation) on emulator using android animation.I'm new to android animation.How could i do that.?
coding is much appreciated.Thanks..
i can't explain everything here but i help you to do that. first you must create two new XML file (Animation in old versions or Tween Animation in new versions) for each image , then use translate. for example:
<translate android:fromXDelta="10"
android:toXDelta="100"
android:duration="2000"
/>
for left image, and for right one use your numbers.
then in java file, set your Button and images. then define animation and use them. i think this example is comprehensible:
final ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
final ImageView iv2 = (ImageView) findViewById(R.id.imageView2);
final Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Animation anim = AnimationUtils.loadAnimation(main.this, R.anim.animation);
iv1.startAnimation(anim);
iv2.startAnimation(anim);
}
});
in this example, i use one animation for both of them.
and remember in this site, you can get help from other, not absolute code.
add two xml files to res/anim folder of your project like below:
lefttoright.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator">
<translate android:fromXDelta="-600%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%" android:duration="300"
android:zAdjustment="bottom">
</translate>
righttoleft.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator">
<translate android:fromXDelta="0%" android:toXDelta="400%"
android:fromYDelta="0%" android:toYDelta="0%" android:duration="300"
android:zAdjustment="bottom">
</translate>
In your layout file keep one ImageView at the left of the screen and other one at the right of the screen
Use the following code snippet in your button's onClick event:
lefttoright = AnimationUtils.loadAnimation(context,
R.anim.lefttoright);
righttoleft = AnimationUtils.loadAnimation(context,
R.anim.righttoleft);
imageView1.startAnimation(lefttoright);
imageView2.startAnimation(righttoleft);
then implement your animation listener:
lefttoright.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
//update imageView's position (e.g. center)
}
});
Do same thing for righttoleft.
Hope it helps.

Android, problem in stopping animation

In my android project I have an image that i want to rotate it and create animation for long time.
I can run program and it works fine. but I don't know how can I stop it. if you know please tell me.
Thanks
This is my code, I have a menu with two items, When I click on Start item, startAnimation function runs and when I click on Stop item, stopAnimation runs.:
public class Hypnagogic extends Activity
{
ImageView imView;
Animation an;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imView = (ImageView)findViewById(R.id.imView);
}
private void startAnimation()
{
an = AnimationUtils.loadAnimation(this, R.anim.spin);
imView.startAnimation(an);
}
private void stopAnimation()
{
;//there is no any stop function!
}
}
also, in res/anim I put spin.xml that is my animation
<?xml version="1.0" encoding="utf-8"?>
<set>
<rotate
android:fromDegrees="0"
android:toDegrees="-18000"
android:pivotX="50%"
android:pivotY="50%"
android:duration="50000" />
</set>
EDIT: I see. Okay, try the clearAnimation() method associated with the View class. You can call it directly on your ImageView, I'm almost certain this should work for your case:
imView.clearAnimation();
What have you tried? AnimationDrawable has a stop() method. You need to post how you're creating/starting the animation.

Categories

Resources