Wednesday, 15 May 2013

android - How to create ImageView animation sequence from drawable resources? -



android - How to create ImageView animation sequence from drawable resources? -

i trying load 11 images drawable resources , display them in imageview delay. practically doing animationdrawable, since creating such results in outofmemory exception , crash. both reinventing wheel , practice still causes app crash. if init imgview , imgview.setimageresource(r.drawable.pic1); image loaded. when set handler , seek postdelayed() below i'm able see background image of layout before app crashes nil in logcat. here code:

package com.forms.test; import android.app.activity; import android.os.bundle; import android.os.handler; import android.view.animation.animationutils; import android.widget.imageview; public class animacia extends activity { final static int[] imggalleryresources = { r.drawable.pic1, r.drawable.pic2, r.drawable.pic3, r.drawable.pic4, r.drawable.pic5, r.drawable.pic6, r.drawable.pic7, r.drawable.pic8, r.drawable.pic9, r.drawable.pic10, r.drawable.pic11 }; imageview imgview; handler handlertimer = new handler(); public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.animation); initgui(); } private void initgui() { int i; imgview = (imageview)findviewbyid(r.id.imgview); imgview.setanimation(animationutils.loadanimation(this, r.anim.alpha)); for(i = 0; < imggalleryresources.length ;i++) { final int res = imggalleryresources[i]; handlertimer.postdelayed( new runnable() { public void run() { imgview.setimageresource(res); } }, 5000 ); } } }

related code imgview layout/animation.xml:

<?xml version="1.0" encoding="utf-8"?> <framelayout 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/heart" android:id="@+id/framelayout1"> <imageview android:id="@+id/imgview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </framelayout>

and alpha animation of imgview anim/alpha.xml:

<?xml version="1.0" encoding="utf-8"?> <alpha> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromalpha="0.3" android:toalpha="0.9" android:duration="2000" /> </alpha>

all tips , comments welcome.

did seek method?

private int pos = 0; private timer timer; private static final int update_picture = 5; private void initgui() { imgview = (imageview)findviewbyid(r.id.imgview); imgview.setanimation(animationutils.loadanimation(this, r.anim.alpha)); handlertimer = new handler() { public void handlemessage(message msg) { if(msg.what == update_picture) { //start animation here imgview.setimageresource(msg.arg1); imgview.invalidate(); } } }; timer = new timer(); timer.scheduleatfixedrate(new timertask() { public void run() { message msg = new message(); msg.what = update_picture; msg.arg1 = imggalleryresources[pos++]; handlertimer.sendmessage(msg); if(pos>=imggalleryresources.length) timer.cancel(); } }, 0, 2000); }

android animation imageview android-resources

No comments:

Post a Comment