Android: OutOfMemory exception when loading animation - android

I'm doing things pretty simply but I have a lot of frames. It seems to be the amount of frames that causes the exception. I've tried to reduce the size of each frame, but that doesn't seem to effect much. This is even before the animation starts. Short of showing less frames, what can be done? Is there another approach?
Adapted from sample code:
public class XMLAnimation extends Activity
{
class MyAnimationRoutine extends TimerTask
{
MyAnimationRoutine()
{
}
#Override
public void run()
{
ImageView img = (ImageView) findViewById(R.id.simple_anim);
AnimationDrawable frameAnimation = (AnimationDrawable) img
.getBackground();
frameAnimation.start();
}
}
class MyAnimationRoutine2 extends TimerTask
{
MyAnimationRoutine2()
{
}
#Override
public void run()
{
ImageView img = (ImageView) findViewById(R.id.simple_anim);
AnimationDrawable frameAnimation = (AnimationDrawable) img
.getBackground();
frameAnimation.stop();
}
}
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
}
#Override
protected void onResume()
{
super.onResume();
ImageView img = (ImageView) findViewById(R.id.simple_anim);
img.setBackgroundResource(R.anim.simple_animation);
MyAnimationRoutine mar = new MyAnimationRoutine();
MyAnimationRoutine2 mar2 = new MyAnimationRoutine2();
Timer t = new Timer(false);
t.schedule(mar, 100);
Timer t2 = new Timer(false);
t2.schedule(mar2, 5000);
}
}
the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView android:id="#+id/simple_anim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, XMLAnimation"
/>
</LinearLayout>
the animation list:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false">
<item android:drawable="#drawable/frame1" android:duration="50" />
...
<item android:drawable="#drawable/frame40" android:duration="50" />
</animation-list>

After some minor refactoring and code clean-up, wiping data in the emulator, and starting fresh, these problems went away. Testing on the actual device, this part of code works quite smoothly.
So, sometimes, just starting fresh works out.

Related

How can you fade out and in between two images? [duplicate]

This question already has answers here:
How to fade out and in between two images?
(2 answers)
Closed 9 years ago.
I have two images loading in my splash screen. The first image opens (starting the splash screen) then the second image opens. Once the second image fades out the MainActivity starts. Now my question is how do I make my first image fade out, then fade in with my second image?
I'm not trying to cross fade between the two either. I'm trying to do a complete fade out then fade in transition.
The splash.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="#+id/lin_lay"
android:gravity="center" >
<ImageView
android:contentDescription="#string/desc"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinning_wheel_image"
android:background="#drawable/splashscreen1" />
</LinearLayout>
The mainanim.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="#drawable/splashscreen1" android:duration="2500" />
<item android:drawable="#drawable/splashscreen2" android:duration="4000" />
</animation-list>
The Splash.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(10500);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openStartingPoint = new Intent("com.theapplication.app.STARTINGPOINT");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
#Override
public void setRequestedOrientation(int requestedOrientation) {
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
ImageView mainimage = (ImageView)findViewById(R.id.spinning_wheel_image);
mainimage.setBackgroundResource(R.anim.mainamin);
mainanimation = (AnimationDrawable) mainimage.getBackground();
mainanimation.start();
}
You can try this demo.
may be helpful for you.

Animation using xml not working in android

I created one simple demo project, which does the animation using AnimationDrawable class of android.
Here is my java code:
public class TestAnimationActivity extends Activity {
/** Called when the activity is first created. */
ImageView imgCircleWhite,imgCircleYellow;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgCircleYellow = (ImageView)findViewById(R.id.imgCircleYellow);
animateState(true, imgCircleYellow);
}
public void animateState(boolean flag,ImageView imageView)
{
imgCircleYellow.setBackgroundResource(R.drawable.animate_circle_sensor_1);
AnimationDrawable yourAnimation = (AnimationDrawable) imageView.getBackground();
if(!flag)
{
//imageView.getAnimation().reset();
imageView.setBackgroundResource(R.drawable.circle_label_white_1);
yourAnimation.stop();
}
else
{
imageView.setBackgroundResource(R.drawable.animate_circle_sensor_1);
yourAnimation = (AnimationDrawable) imageView.getBackground();
yourAnimation.start();
}
}
}
Here is my main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ImageView android:id="#+id/imgCircleYellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/circle_label_yellow_1"/>
</LinearLayout>
Below is my animate_sensor_circle_1.xml, which I've put in drawable folder to use with:
<?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/circle_label_white_2"
android:duration="50"/>
<item
android:drawable="#drawable/circle_label_yellow_1"
android:duration="50"/>
</animation-list>
When I run the app, only the first item in the above xml file gets displayed, but animation does not occur repeatedly as it should done.
Can anyone direct me where I am going wrong?
It is not the right time to call the AnimationDrawable.start(). You should do this after the view initialization has been completed. Try this:
final ImageView imgCircleYellow = (ImageView)findViewById(R.id.imgCircleYellow);
imgCircleYellow.setBackgroundResource(R.drawable.animate_circle_sensor_1);
imgCircleYellow.post(new Runnable() {
#Override
public void run() {
animateState(true, imgCircleYellow);
}
Check this for more information: https://stackoverflow.com/a/5490922/813135

get resource from server for animation

I have developed an app which has Frame by Frame animation. I am getting resources from drawable folder. So the size of my apk is huge. Now I would like to get resources from server, but I am unable to come up with an idea to do this.
i have put my code over here
main.xml ::
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView android:id="#+id/simple_anim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, XMLAnimation"
/>
<Button android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
frame_animation_girl.xml ::
<animation-list xmlns:android=
"http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/girl0001" android:duration="20" />
<item android:drawable="#drawable/girl0002" android:duration="20" />
<item android:drawable="#drawable/girl0003" android:duration="20" />
<item android:drawable="#drawable/girl0004" android:duration="20" />
<item android:drawable="#drawable/girl0005" android:duration="20" />
<item android:drawable="#drawable/girl0006" android:duration="20" />
</animation-list>
Java file ::
ImageView img = (ImageView) findViewById(R.id.simple_anim);
img.setBackgroundResource(R.drawable.frame_animation_girl);
MyAnimationRoutine mar = new MyAnimationRoutine();
MyAnimationRoutine2 mar2 = new MyAnimationRoutine2();
Timer t = new Timer(false);
t.schedule(mar, 100);
Timer t2 = new Timer(false);
t2.schedule(mar2, 5000);
Buttona = (Button) findViewById(R.id.button1);
final Intent animationIntent = new Intent(this, TranningIntent.class);
Buttona.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(animationIntent);
finish();
}
});
}
class MyAnimationRoutine extends TimerTask {
MyAnimationRoutine() {
}
public void run() {
ImageView img = (ImageView) findViewById(R.id.simple_anim);
// Get the background, which has been compiled to an
// AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img
.getBackground();
// Start the animation (looped playback by default).
frameAnimation.start();
}
}
class MyAnimationRoutine2 extends TimerTask {
MyAnimationRoutine2() {
}
public void run() {
ImageView img = (ImageView) findViewById(R.id.simple_anim);
// Get the background, which has been compiled to an
// AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img
.getBackground();
// stop the animation (looped playback by default).
/* frameAnimation.stop(); */
}
}
}
I think this really depends on what you're trying to do, and what your animations are. Some simpler animations (e.g. rotations) you could get away with animating a single drawable, potentially allowing you to store fewer assets. If you are dealing with really complex animations and potentially many assets, you could always return data from your server that can be interpreted by your client logic into animations programmatically (not via xml).
There is also some discussion of using XML files at runtime in this thread:
Download and replace Android resource files
i have solve this by ::
ImageView img = (ImageView) findViewById(R.id.simple_anim);
animation = new AnimationDrawable();
try {
for(int i=0;i<54;i++)
{
xyz("girl000",i);
}
animation.setOneShot(false);
} catch (Exception e) {
}
img.setBackgroundDrawable(animation);
img.post(new Starter());
}
public void xyz(String str,int x)
{
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
"http://sdfsdffff/MRESC/images/test/girl/"+"girl000"+x+".png")
.getContent());
Drawable frame =new BitmapDrawable(bitmap);
animation.addFrame(frame, 50);
} catch (Exception e) {
}
}
class Starter implements Runnable {
public void run() {
animation.start();
}
}

Creating a Slide Show that Fade images into each other and transforms

I am trying to create an image slide show that is similar to this
I found this tutorial which shows me how to make images fade into each other, which works really well, but I can't find any examples how to make it do the movement/scaling at the same time so I am trying to adapt it.
I have added a scale animation and put that along with the alpha animation in an animationset but I can't get it to work correctly, it is only doing the animation on every other image and then when the zoom starts it zooms one way and switches and then zooms the other way.
I am reasonably new to Android have not done any animations before and am having difficultly understanding how the example is working. Therefore I am having difficulty amending it.
Can anyone help me work out what I am doing wrong? I'm starting to pull my hair out!
My java code is:
public class TopListActivity extends Activity {
ImageView slide_0;
ImageView slide_1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
slide_0 = (ImageView) findViewById(R.id.slide_1);
slide_1 = (ImageView) findViewById(R.id.slide_2);
}
private static class AnimationTimer extends TimerTask implements
AnimationListener {
TopListActivity topList;
Vector<BitmapDrawable> images;
int count = 0;
public AnimationTimer(TopListActivity _topList) {
this.topList = _topList;
this.images = new Vector<BitmapDrawable>();
Resources resources = topList.getResources();
images.add((BitmapDrawable) resources.getDrawable(R.drawable.one));
images.add((BitmapDrawable) resources.getDrawable(R.drawable.two));
images.add((BitmapDrawable) resources.getDrawable(R.drawable.three));
images.add((BitmapDrawable) resources.getDrawable(R.drawable.four));
images.add((BitmapDrawable) resources.getDrawable(R.drawable.five));
images.add((BitmapDrawable) resources.getDrawable(R.drawable.six));
if (this.images.size() > 0) {
this.topList.slide_0.setBackgroundDrawable(this.images.get(0));
if (this.images.size() > 1) {
this.topList.slide_1.setBackgroundDrawable(this.images
.get(1));
}
}
this.count = 1;
}
public void launch() {
if (this.images.size() >= 2) {
(new Timer(false)).schedule(this, 100);
}
}
#Override
public void run() {
this.doit();
this.cancel();
}
private void doit() {
if ((this.count % 2) == 0) {
AnimationSet set = new AnimationSet(false);
AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setStartOffset(5000);
animation.setDuration(3000);
animation.setFillAfter(true);
ScaleAnimation zoom = new ScaleAnimation(1, 1.20f, 1, 1.20f);
zoom.setStartOffset(0);
zoom.setDuration(8000);
zoom.setFillAfter(true);
set.addAnimation(animation);
set.addAnimation(zoom);
set.setAnimationListener(this);
this.topList.slide_1.startAnimation(set);
} else {
AnimationSet set = new AnimationSet(false);
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setStartOffset(5000);
animation.setDuration(3000);
animation.setFillAfter(true);
ScaleAnimation zoom = new ScaleAnimation(1.20f, 1, 1.20f, 1);
zoom.setStartOffset(0);
zoom.setDuration(8000);
zoom.setFillAfter(true);
set.addAnimation(animation);
set.addAnimation(zoom);
set.setAnimationListener(this);
this.topList.slide_1.startAnimation(set);
}
}
public void onAnimationEnd(Animation animation) {
if ((this.count % 2) == 0) {
this.topList.slide_1.setBackgroundDrawable(this.images
.get((this.count + 1) % (this.images.size())));
} else {
this.topList.slide_0.setBackgroundDrawable(this.images
.get((this.count + 1) % (this.images.size())));
}
this.count++;
this.doit();
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
}
#Override
public void onResume() {
super.onResume();
(new AnimationTimer(this)).launch();
}
}
and my layout is:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/slide_1"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/slide_2"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
Finally sorted it... it's very long winded as I couldn't get to grips with the animation end event because of the multiple animations.. so probably not the best way but it works and I'm happy!
For anyone who wants to know how.. or wants to adapt it to make it better here it is:
Main Activity (MyTransition.java):
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.Vector;
public class MyTransition extends AppCompatActivity {
ImageView slide_0;
ImageView slide_1;
ImageView lastSlide;
Vector<Integer> imageIds;
LinearLayout linLayout;
int count = 0;
private Handler transparencyHandler = new Handler();
private Handler timerHandler = new Handler();
private Runnable transparencyTimer = new Runnable() {
public void run() {
if (lastSlide == slide_0) {
slide_1.setBackgroundColor(getResources().getColor(android.R.color.transparent));
slide_1.setImageResource(0);
} else {
slide_0.setBackgroundColor(getResources().getColor(android.R.color.transparent));
slide_0.setImageResource(0);
}
}
};
private Runnable timer = new Runnable() {
public void run() {
if (lastSlide == slide_0) {
slide_1.setImageResource(0);
slide_1.setImageResource(imageIds.get((count + 1)
% (imageIds.size())));
slide_1.startAnimation(AnimationUtils
.loadAnimation(MyTransition.this,
R.anim.transition_down));
lastSlide = slide_1;
} else {
slide_0.setImageResource(0);
slide_0.setImageResource(imageIds.get((count + 1)
% (imageIds.size())));
slide_0.startAnimation(AnimationUtils
.loadAnimation(MyTransition.this,
R.anim.transition_up));
lastSlide = slide_0;
}
count++;
transparencyHandler.postDelayed(transparencyTimer, 1000);
timerHandler.postDelayed(timer, 8000);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
slide_0 = (ImageView) findViewById(R.id.slide_1);
slide_1 = (ImageView) findViewById(R.id.slide_2);
imageIds = new Vector<Integer>();
imageIds.add(R.drawable.first_image);
imageIds.add(R.drawable.second_image);
// Load Image 1
slide_0.setImageResource(imageIds.get(0));
slide_0.startAnimation(AnimationUtils.loadAnimation(this,
R.anim.transition_down));
lastSlide = slide_0;
timerHandler.postDelayed(timer, 8000);
}
}
Layout (test2.xml):
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/slide_1"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
<ImageView
android:id="#+id/slide_2"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
</FrameLayout>
Animation (transition_down.xml):
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:fromAlpha="0.0"
android:toAlpha="2.0"
android:duration="2000"
android:repeatCount="0"
android:fillAfter="true" />
<scale
android:fromXScale="1"
android:toXScale="1.2"
android:fromYScale="1"
android:toYScale="1.2"
android:duration="10000"
android:repeatCount="0"
android:fillAfter="true" />
<alpha
android:fromAlpha="2.0"
android:toAlpha="0.0"
android:duration="2000"
android:repeatCount="0"
android:startOffset="8000"
android:fillAfter="true" />
</set>
You are adding two separate animation. That is why they are intervaling.
As a beginner, I recommend getting into the habit of making animations (and really all resources possible) using xml. It is much more verbose and it is easier to change resources later on (as well as allowing you to automatically change resources at run time based on screen size, language, etc). Also get into a habit of really exhausting all android widgets before implementing something custom.
As far as a animation resource, look at the hyperspace_jump.xml example at animation resources.
For view transitions i would normally recommend a ViewFlipper. However, since you want custom transitions, you may need to go down a level and use the ViewnAnimator. I understand you want to cycle through animations, but first get this working for one animation. I think this could still work with ViewFlipper, which would look something like this:
<ViewFlipper
android:id="#+id/VF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inAnimation="#anim/yourFirstAnimation
android:flipInterval="#integer/flip_interval_time">
<ImageView
android:id="#+id/slide_1"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/slide_2"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ViewFlipper>
onCreate(Bundle b) {
setContentView(R.layout.main);
mVF = (ViewFlipper) findViewById(R.id.VF);
mVF.startFlipping();
}
Then when you want to cycle through animations:
Set your flipInterval to 0 (i think) and do like you did with the OnAnimationEnd event:
public void onAnimationEnd(Animation animation) {
Animation an = myAnimationContainer.getRandomAnimation();
mVF.setInAnimation(an);
mVF.showNext();
}
You shouldn't need to synchronize the flipInterval time, since this time really just depends how long the animation is.

Android: make animation from still images

I've a series of still images and total of more than 500 images presented in drawable directory. I need to make an animation (load about 20 images per second). I want it to run smoothly and with no Out Of Memory Exception.
I've idea to do this that images for 2 to 3 seconds (40 to 60 images) should load in memory and displayed and then they should disposed off (release the memory) and then images for next 2 to 3 seconds should load. This technique can prevent Out Of Memory Exception. Its just an idea, I dont know whether its a good idea or not. Please guide me some better idea with some code to go with... If my idea is much better and can work then please tell me some helping code to do that.
After reading replies and doing as you suggest, I've written some code like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/llMain">
<ViewFlipper android:id="#+id/imageflipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerInside"
android:layout_gravity="center" />
</ViewFlipper>
</LinearLayout>
and here is my code for doing animation:
public class Animation extends Activity {
ViewFlipper flipper;
int myIndex = 216;
private final Handler handler = new Handler();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper=(ViewFlipper)findViewById(R.id.imageflipper);
doTheAutoRefresh();
//displayData();
}
private void doTheAutoRefresh() {
handler.postDelayed(new Runnable() {
public void run(){
displayData(); // this is where you put your refresh code
doTheAutoRefresh();
}
}, 30);
}
private void displayData()
{
Resources r = getResources();
if(myIndex > 230){
myIndex = 216;
ImageView myImg = (ImageView)findViewById(R.id.ImageView01);
myImg.setImageResource(r.getIdentifier("drum0" + myIndex, "drawable", "com.vt.animation"));
myIndex += 1;
flipper.showNext();
}
else{
ImageView myImg = (ImageView)findViewById(R.id.ImageView01);
myImg.setImageResource(r.getIdentifier("drum0" + myIndex, "drawable", "com.vt.animation"));
myIndex += 1;
flipper.showNext();
}
}
}
but its very slow. I've set up the refresh time to 30 milliseconds but actually its not refreshing too fast rather its refresh time is about 1 second. Any suggestion to make it fast to feel like real animation?
Thanks,
Use a FrameAnimation, eg in res/drawable/movie.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/frame1" android:duration="50" />
<item android:drawable="#drawable/frame2" android:duration="50" />
<item android:drawable="#drawable/frame3" android:duration="50" />
etc...
</animation-list>
And then in Java:
imageView.setBackgroundResource(R.drawable.movie);
AnimationDrawable anim = (AnimationDrawable) imageView.getBackground();
anim.start();
OK. The biggest problem and the easiest solution I got to go with after so many days. I would never expect that it would be so easy to do... :D
I've used both handler and timer to achieve with just an image view, no flipper, no animator nothing else at all... Here is my solution:
----- main.xml file -----
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView1">
</ImageView>
And here is the way I have done it:
public class MainActivity extends Activity {
private ImageView _imagView;
private Timer _timer;
private int _index;
private MyHandler handler;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handler= new MyHandler();
_imagView=(ImageView) findViewById(R.id.imageView1);
_index=0;
_timer= new Timer();
_timer.schedule(new TickClass(), 500, 200);
}
private class TickClass extends TimerTask
{
#Override
public void run() {
// TODO Auto-generated method stub
handler.sendEmptyMessage(_index);
_index++;
}
}
private class MyHandler extends Handler
{
#Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
try {
Bitmap bmp= BitmapFactory.decodeStream(MainActivity.this.getAssets().open("drum_"+_index+".png"));
_imagView.setImageBitmap(bmp);
Log.v("Loaing Image: ",_index+"");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v("Exception in Handler ",e.getMessage());
}
}
}
}
Note: I've placed all my images to asset directory.
Its so simple as it can, nothing big to do...
I hope it'll be helpful for someone looking to go like this :)
Loading several images is very expensive.
I think it's better to load a single image containing all the movements of that certain animation. An animation strip.
private Bitmap animation = BitmapFactory.decodeResource(getResources(), R.drawable.myPng);
The idea is to traverse the bitmap.
Well, I'm using viewFlipper, switching between views. Good thing about this one is that u can see previous picture sliding out while the new one slides in.
Inside image displaying method:
if (direction == NEXT) {
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
if (currImg < max)
currImg++;
if (currImg == max)
currImg = 0;
if (currentView == 0) {
currentView = 1;
ImageView iv = (ImageView) findViewById(R.id.ImageView02);
iv.setImageResource(images[currImg]);
} else if (currentView == 1) {
currentView = 2;
ImageView iv = (ImageView) findViewById(R.id.ImageView03);
iv.setImageResource(images[currImg]);
} else {
currentView = 0;
ImageView iv = (ImageView) findViewById(R.id.ImageView01);
iv.setImageResource(images[currImg]);
}
viewFlipper.showNext();
}
else if (direction == PREV) {
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
if (currImg > 0)
currImg--;
else if (currImg <= 0)
currImg = (max-1);
if (currentView == 0) {
currentView = 2;
ImageView iv = (ImageView) findViewById(R.id.ImageView03);
iv.setImageResource(images[currImg]);
} else if (currentView == 2) {
currentView = 1;
ImageView iv = (ImageView) findViewById(R.id.ImageView02);
iv.setImageResource(images[currImg]);
} else {
currentView = 0;
ImageView iv = (ImageView) findViewById(R.id.ImageView01);
iv.setImageResource(images[currImg]);
}
viewFlipper.showPrevious();
And inside XML file:
<ViewFlipper android:id="#+id/imageflipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:layout_gravity="center" />
<ImageView android:id="#+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:layout_gravity="center" />
<ImageView android:id="#+id/ImageView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:layout_gravity="center" />
</ViewFlipper>

Categories

Resources