I want to apply Animation in my Image View inside Navigation View.
I saw its working fine in some other apps. I have tried with following code but its not working for me.
drawer_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_logout"
android:title="#string/nav_menu_logout" />
</group>
<group
android:id="#+id/menu_sync"
android:checkableBehavior="single">
<item
android:id="#+id/nav_sync_now"
android:title="#string/string_menu_sync_now"
app:actionLayout="#layout/layout_menu_sync_now"
app:showAsAction="always" />
</group>
</menu>
NavigationActivity.java
runOnUiThread(new Runnable() {
#Override
public void run() {
MenuItem menuItemSync = navigationBinding.navView.getMenu().findItem(R.id.nav_sync_now);
menuItemSync.setEnabled(false);
menuItemSync.setTitle(R.string.string_menu_syncing);
mImageViewSyncing = (AppCompatImageView) menuItemSync.getActionView().findViewById(R.id.nav_sync_now_image_view);
mImageViewSyncing.setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent));
/* Create Animation */
Animation mRotateAnimation = AnimationUtils.loadAnimation(mContext, R.anim.button_rotate);
/* start Animation */
mImageViewSyncing.startAnimation(mRotateAnimation);
}
});
button_rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<rotate
android:duration="2500"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toDegrees="0"
android:fromDegrees="360"/>
</set>
layout_menu_sync_now.xml
<android.support.v7.widget.AppCompatImageView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nav_sync_now_image_view"
android:layout_width="24dp"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:layout_gravity="end|center_vertical"
app:srcCompat="#drawable/ic_sync_black" />
Is there any suggestion what to do?
Thanks in advance.
I was following the Google provided example of how to use AnimationDrawable with an ImageView. You can find it here: http://developer.android.com/guide/topics/graphics/drawable-animation.html
imageView.setBackgroundResource(R.drawable.animation);
AnimationDrawable animation = (AnimationDrawable)imageView.getBackground();
animation.start();
When I run it I get the error:
java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
Google seems to think this should work, but if you cannot cast a BitmapDrawable to AnimationDrawable I am not sure how this is supposed to work?
I figured out the solution to this problem.
imageView.setImageDrawable(getResources().getDrawable(R.drawable.animation));
AnimationDrawable animation = (AnimationDrawable) imageView.getDrawable();
animation.start();
I have no idea why Google's documentation says to use the background, but using setImageDrawable and getDrawable works. Honestly it makes more sense it would work this way than the other way anyways.
I had the same problem. I know this thread is some month old, but maybe somebody what to read about my experience.
I dont know why, but google doesnt accept Spacemarks like "_" in his Picturenames while using it for animation. I uses names like "loading_frame1", and it doesnt work. I changed the names to something like "loadingframe1" and it works....
Before:
<?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/loading_frame1" android:duration="100" />
<item android:drawable="#drawable/loading_frame2" android:duration="100" />
<item android:drawable="#drawable/loading_frame3" android:duration="100" />
<item android:drawable="#drawable/loading_frame4" android:duration="100" />
<item android:drawable="#drawable/loading_frame5" android:duration="100" />
<item android:drawable="#drawable/loading_frame6" android:duration="100" />
<item android:drawable="#drawable/loading_frame7" android:duration="100" />
<item android:drawable="#drawable/loading_frame8" android:duration="100" />
<item android:drawable="#drawable/loading_frame9" android:duration="100" />
<item android:drawable="#drawable/loading_frame10" android:duration="100" />
<item android:drawable="#drawable/loading_frame11" android:duration="100" />
<item android:drawable="#drawable/loading_frame12" android:duration="100" />
<item android:drawable="#drawable/loading_frame13" android:duration="100" />
<item android:drawable="#drawable/loading_frame14" android:duration="100" />
<item android:drawable="#drawable/loading_frame15" android:duration="100" />
<item android:drawable="#drawable/loading_frame16" android:duration="100" />
<item android:drawable="#drawable/loading_frame17" android:duration="100" />
<item android:drawable="#drawable/loading_frame18" android:duration="100" />
<item android:drawable="#drawable/loading_frame19" android:duration="100" />
<item android:drawable="#drawable/loading_frame20" android:duration="100" />
</animation-list>
After:
<?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/loadingframe1" android:duration="100" />
<item android:drawable="#drawable/loadingframe2" android:duration="100" />
<item android:drawable="#drawable/loadingframe3" android:duration="100" />
<item android:drawable="#drawable/loadingframe4" android:duration="100" />
<item android:drawable="#drawable/loadingframe5" android:duration="100" />
<item android:drawable="#drawable/loadingframe6" android:duration="100" />
<item android:drawable="#drawable/loadingframe7" android:duration="100" />
<item android:drawable="#drawable/loadingframe8" android:duration="100" />
<item android:drawable="#drawable/loadingframe9" android:duration="100" />
<item android:drawable="#drawable/loadingframe10" android:duration="100" />
<item android:drawable="#drawable/loadingframe11" android:duration="100" />
<item android:drawable="#drawable/loadingframe12" android:duration="100" />
<item android:drawable="#drawable/loadingframe13" android:duration="100" />
<item android:drawable="#drawable/loadingframe14" android:duration="100" />
<item android:drawable="#drawable/loadingframe15" android:duration="100" />
<item android:drawable="#drawable/loadingframe16" android:duration="100" />
<item android:drawable="#drawable/loadingframe17" android:duration="100" />
<item android:drawable="#drawable/loadingframe18" android:duration="100" />
<item android:drawable="#drawable/loadingframe19" android:duration="100" />
<item android:drawable="#drawable/loadingframe20" android:duration="100" />
</animation-list>
And here the LoadingAnimation.class Listing
package com.justkidding.animation;
import android.support.v7.app.ActionBarActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class LoadingAnimation extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading_animation);
}
#Override
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
ImageView animation = (ImageView)findViewById(R.id.aniimage);
animation.setBackgroundResource(R.drawable.loading_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) animation.getBackground();
if(hasFocus) {
frameAnimation.start();
} else {
frameAnimation.stop();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.loading_animation, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Google's code works. The "can not be cast issue" which lead me here, was because I was not paying attention and put my animation.xml in res.anim instead of res.drawable.
However I agree using setImageDrawable and getDrawable works better.
About this problem, I had a little oversight on the detail on google's example code in the documentation and this might be the case for a couple of persons using the guide.
There is an separate xml file that holds the drawables, indicating the transitions and has the tag:
<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>
The above file is named rocket_thrust, and it is this same file that is set as backgroundDrawable in the following lines:
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
Give a try at this and be sure that documentation has no errors.
Best of luck.
Complete process to do animation is :
1. Create XML layout with imageView
and
2. Create XML file for animation suppose drawable/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/twit" android:duration="120"></item>
<item android:duration="120" android:drawable="#drawable/a111"></item>
<item android:duration="120" android:drawable="#drawable/a2"></item>
<item android:duration="120" android:drawable="#drawable/a3"></item>
<item android:duration="120" android:drawable="#drawable/a4"></item>
<item android:duration="120" android:drawable="#drawable/a5"></item>
<item android:duration="120" android:drawable="#drawable/a6"></item>
</animation-list>
now
3. Create Main Activity
Then Type this code
public class AnimationMe extends AppCompatActivity {
private ImageView imgView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logoo);
imgView = (ImageView) findViewById(R.id.imgView);
// the frame-by-frame animation defined as a xml file within the drawable folder
/*imgView.setBackgroundResource(R.drawable.animation);*/
imgView.setImageDrawable(getResources().getDrawable(R.drawable.animation));
// It's not possible to start the animation during the onCreate.
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable animationDrawable = (AnimationDrawable)imgView.getDrawable();
if(hasFocus)
{
animationDrawable.start();
}
else
{
animationDrawable.stop();
}
}
}
*
Note : ImageView have background as drawable and give a name of
animation.xml not for a particular image and then call with
imageview.getDrawable in AnimationDrawable.
---- You can't Run Animation in onCreate Method. Set drawable property in Imageview in onCreate() but call AnimationDrawable method out of
block of onCreate().
*
Sure it will work !
Just to add more answer to this page based on my experience because Stackoverflow seems has a very limited answer for this issue
I my case I tried to animate my background layout which using drawable for rounded radius. I got logcat error
java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
Turn out I must set the background attribute on my layout file to this drawable file
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
<item android:drawable="#drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
<item android:drawable="#drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
<item android:drawable="#drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
And then call this code on my Main Activity
val backgroundAnim = info_layout?.background as AnimationDrawable
backgroundAnim.start()
My mistake is previously I put #drawable/rounded_corner on layout file as background attribute.
Hope this can help somebody, as I spend 3 hours just to solve this issue.
When I launch the emulator, the animation only plays the first frame of the animation, sits for the duration of the timer and then starts the activity.
This is my Drawable xml
<?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/title1" android:duration="200" />
<item android:drawable="#drawable/title2" android:duration="200" />
<item android:drawable="#drawable/title3" android:duration="200" />
<item android:drawable="#drawable/title4" android:duration="200" />
<item android:drawable="#drawable/title5" android:duration="200" />
<item android:drawable="#drawable/title6" android:duration="200" />
<item android:drawable="#drawable/title7" android:duration="200" />
<item android:drawable="#drawable/title8" android:duration="200" />
<item android:drawable="#drawable/title9" android:duration="200" />
<item android:drawable="#drawable/title10" android:duration="200" />
<item android:drawable="#drawable/title11" android:duration="200" />
</animation-list>
This is my layout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/gyro"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
/>
This is my java file
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// Start the animation (looped playback by default).
ImageView splash = (ImageView) findViewById(R.id.gyro);
splash.setBackgroundResource(R.drawable.title);
AnimationDrawable splashAnimation = (AnimationDrawable) splash.getBackground();
splashAnimation.start();
Thread logoTimer = new Thread(){
public void run(){
try{
int logoTimer = 0;
while(logoTimer <10000){
sleep(100);
logoTimer = logoTimer + 100;
}//end of while loop
//mpsplash.stop();
startActivity(new Intent("tv.bScienceFiction.scrip.or.scrap.CLEARSCREEN"));
} catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}//end of finally
}//end of run
};//end of new Thread
logoTimer.start();
}
Start it from the UI thread after you exit onCreate
splash.postDelayed(new Runnable() {
public void run() {
splashAnimation.start();
}
}, 200);
I'm trying to get the AnimationDrawable on my Android app to repeat.
I configured the android:oneshot on false. (tried both with java and with XML)
Still, whenever the animation is played, once it's done, it goes back to the first frame, and stops
This is my XML file
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/network_scanning" android:oneshot="false">
<item android:drawable="#drawable/network_wireless_0" android:duration="300" />
<item android:drawable="#drawable/network_wireless_1" android:duration="300" />
<item android:drawable="#drawable/network_wireless_2" android:duration="300" />
<item android:drawable="#drawable/network_wireless_3" android:duration="300" />
<item android:drawable="#drawable/network_wireless_4" android:duration="300" />
</animation-list>
and this is the code to start the animation:
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
LinearLayout network = (LinearLayout) findViewById(R.id.wifi_anim);
AnimationDrawable anim = (AnimationDrawable) network.getBackground();
if (hasFocus)
{
anim.setOneShot(false);
anim.setCallback(network);
anim.setVisible(true, true);
anim.start();
}
}
hi you can get solution from here
http://docs.mono-android.net/Android.Graphics.Drawables.AnimationDrawable
I have to repeat the image sequence I am using with Thread and AnimationDrawable but it is not working continuously. I don't want to stop this animation until next activity is started through button click event.
Here is my java code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);{
final ImageView splashImage=(ImageView)findViewById(R.id.heartFunction);
splashImage.setBackgroundResource(R.drawable.slide_right);
splashAnimation = (AnimationDrawable) splashImage.getBackground();
}
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if ( isFocused ) {
//isFocused = false;
splashAnimation.start();
var=false;
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(SPLASH_DISPLAY_LENGTH);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
slide_right.xml:-
<?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/heartcolored0" android:duration="200" />
<item android:drawable="#drawable/heartcolored2" android:duration="200" />
<item android:drawable="#drawable/heartcolored4" android:duration="200" />
<item android:drawable="#drawable/heartcolored5" android:duration="200" />
<item android:drawable="#drawable/heartcolored6" android:duration="200" />
<item android:drawable="#drawable/heartcolored7" android:duration="200" />
<item android:drawable="#drawable/heartcolored8" android:duration="200" />
<item android:drawable="#drawable/heartcolored9" android:duration="200" />
<item android:drawable="#drawable/heartcolored10" android:duration="200" />
<item android:drawable="#drawable/heartcolored11" android:duration="200" />
<item android:drawable="#drawable/heartcolored12" android:duration="200" />
<item android:drawable="#drawable/heartcolored13" android:duration="200" />
</animation-list>
If you want your animation to contiuously run then you need to set android:oneshot="false"
You were saying before to only run through once.
If you want an animation to run until you click the screen to go to the next activity. Start the animation when the onWindowFocusChanged function
#Override
public void onWindowFocusChanged(boolean hasFocus){
splashanimation.start();
}
Then use an onTouchEvent to catch the touch, start a new activity and finish the old activity.
#Override
public boolean onTouchEvent(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Intent i = new Intent(Anim.this, Main.class);
startActivity(i);
finish();
}
return true;
}
Hope this helps, your question is very hard to read/understand.