Reputation: 496
This question is already been asked,but i didn't find any suitable answers and so I'm posting the question.
I've a ProgressBar
animation which has multiple images and loads these images like a progressbar. I'm able to run it successfully in 4.0 and higher versions but it doesn't work in 2.3.How to make it work in lower versions?
My Animation Drawable XML file
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
android:pivotX="50%"
android:pivotY="50%" >
<item android:drawable="@drawable/p1_wheel" android:duration="200"/>
<item android:drawable="@drawable/p2_wheel" android:duration="200"/>
<item android:drawable="@drawable/p3_wheel" android:duration="200"/>
<item android:drawable="@drawable/p4_wheel" android:duration="200"/>
<item android:drawable="@drawable/p5_wheel" android:duration="200"/>
<item android:drawable="@drawable/p6_wheel" android:duration="200"/>
<item android:drawable="@drawable/p7_wheel" android:duration="200"/>
<item android:drawable="@drawable/p8_wheel" android:duration="200"/>
<item android:drawable="@drawable/p9_wheel" android:duration="200"/>
<item android:drawable="@drawable/p10_wheel" android:duration="200"/>
<item android:drawable="@drawable/p11_wheel" android:duration="200"/>
<item android:drawable="@drawable/p12_wheel" android:duration="200"/>
</animation-list>
My XML File
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
My Java File
package com.example.progressbarsample;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.DrawableContainer;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
ImageView progressbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressbar = (ImageView) findViewById(R.id.imageView1);
progressbar.setBackgroundResource(R.drawable.progressbar);
final AnimationDrawable frame = (AnimationDrawable) progressbar.getBackground();
progressbar.post(new Runnable() {
@Override
public void run() {
frame.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Upvotes: 0
Views: 2388
Reputation: 232
try animating after the view is visible like this:
@Override
public void onWindowFocusChanged (bool hasFocus)
{
super.onWindowFocusChanged (hasFocus);
//get the animation from your view
AnimationDrawable explosionAnimation = (AnimationDrawable) mImageView.getBackground();
//set it visible and start animation
explosionAnimation.setVisible (true, true);
explosionAnimation.start ();
}
The method onWindowFocusChanged from Activity is called after the view is already visible and it gets the focus. So, you will be able to animate your image. I've tried without setVisible but only a static image was shown. The same happens when I started the animation from onResume, onStart, etc. The code above works fine for me, including Android 2.3. I'm currently working with mono (with Xamarin), but I hope it's gonna work for you in Java.
Upvotes: 1
Reputation: 496
At Last I found a solution to this.
progressbar = (ImageView) findViewById(R.id.imageView1);
progressbar.setBackgroundResource(R.drawable.progressbar);
final AnimationDrawable frame = (AnimationDrawable)getResources.getDrawable(R.drawable.progressbar); \\progressbar is the anim.xml file
progressbar.setBackgroundDrawable(frame);
progressbar.post(new Runnable() {
@Override
public void run() {
frame.start();
}
});
}
Upvotes: 1
Reputation: 1363
I have worked with Drawable Animations and it works for me here my Code:
mImageView.setBackgroundResource(R.drawable.anim);
AnimationDrawable explosionAnimation = (AnimationDrawable) mImageView.getBackground();
explosionAnimation.start();
But i think its becausse of the post, try the code without using the progressbar.post
Upvotes: 0