android assign a drawable dynamicaly to a imageview

I have an array of drawables, that I would like to change by my own timer to a imageview. (i tried the other option with xml and setBackgroundResource, but does not work for me as I have hundret of pics and always got memory problem as it looks android assign already the whole memory for all pics at once. (just in this demo i shorted it to 4 images)

Ok, so first i make my array

private static int[] draws = { R.drawable.frankiearmevor_0001, R.drawable.frankiearmevor_0002, R.drawable.frankiearmevor_0003, R.drawable.frankiearmevor_0002 }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imgView = (ImageView) findViewById(; // create timer Timer updateProgressTimer = new Timer(); updateProgressTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { myloop(); } }, 0, 150); } int mycounter; public void myloop() { mycounter++; if (mycounter > 4) mycounter = 1; imgView.setImageResource(draws[mycounter-1]); String hallo; hallo = "now: "+mycounter; Log.d("1",hallo); }

when I assign only a fixed image: imgView.setImageResource(draws[2]); it shows that fine and I see also my thread is logged fine, but when I exchange the fixed resource draws[2] into a dynamic draws[mycounter-1] .. i just get a black screen, no error, nothing.

what to do, so i will show the images :)

thx chris

EDIT: 22. August:

I tried now with the comment I got, it compiles fine, but somehow there is an error i guess.. it crash:

imgView = (ImageView) findViewById(; Timer updateProgressTimer = new Timer(); updateProgressTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { //myloop(); (new Runnable() { public void run() { mycounter++; if (mycounter > 10) mycounter = 1; imgView.setImageResource(draws[1]); //imgView.invalidate(); String hallo; hallo = "now: "+mycounter; Log.d("1",hallo); } }); } }, 0, 150);

-------------Problems Reply------------

The ImageView update should happen on the UI Thread...

Refer this answer. The TimerTask runs on a different thread, so you've to use the to update the UI Component.

Are you sure you need to update the Component every 150ms? That's very expensive.

Good Luck.

Category:android Views:1 Time:2010-08-21
Tags: android

Related post

  • Setting a Drawable Resource of an ImageView from another class - ANDROID 2012-04-26

    Heres the situation: I got 2 classes: "" and "" ...Main , is an activity where I'm building up my app and "Control" is just a java class where I assign some variables I might use on other classes. Okay, on class "Main" I got an I

  • Android add image on top of another imageView as image below in Layout XML 2014-12-08

    How to create a android Layout where you add a smaller imageView on Top of another Image View. but only half way as the image below. Thank. this is the sample I try but it does not work: <RelativeLayout android:layout_width="match_parent" android:

  • Getting at XML files inside Android.jar.res.drawable 2010-10-16

    As I've been researching another problem online, I've seen references to folks modifying the XML files contained inside the Android.jar.res.drawable folder and then copying them to their drawable folder for use in their project. But I can't open thes

  • Android: do system drawables have static IDs? 2011-02-12

    I asked this question about using the system-drawables in an Android-app some time ago: Android: access "system"-drawables I finally found out, that there actually is a way to access those drawables, but especially concerning the battery-icons, there

  • Android - Activity will not close after assigning a bitmap image to an ImageView 2011-05-14

    I am having the stragest problem when assigning a user-selected image from the Android photo gallery to an ImageView in my Activity ... I can no longer close the Activity that hosts that ImageView. If I simply assign an image from my resources folder

  • Android: How to make TableRow height and ImageView height to be equal to ImageView scr drawable 2011-05-24

    I have problem to make TableRow and ImageView height to be equal to ImageView scr drawable height. Could somebody helps me to fix this? <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="

  • Android: Scale a Drawable or background image? 2009-09-09

    On a layout I want to scale the background image (keeping its aspect ratio) to the space allocated when the page gets created. Anyone have any idea how to do this? I am using layout.setBackgroundDrawable() and am using a BitmapDrawable to setGravity

  • Android - How to switch views with 3 ImageViews? 2010-02-25

    I need help coming up with a way of executing the following sequence using some type of view or layout combination in Android: I have 3 image objects... say object A, B, and C... [all objects invisible and layered, one on top of the other... A/B/C, l

  • android - dynamically changing the position of an imageview 2010-11-30

    i'm currently using the matrix method to implement drag and pinch zoom for an image (image A) Now i want a part of image A to be clickable so i put another imageview (image B) over image A and set the layout_margins for it in the xml file. My questio

  • Android: how to programmatically set width of ImageView inside TableRow 2011-01-27

    How do you programmatically set the width of an ImageView inside a TableRow? I do NOT want the cell width to change, only the width of the image inside the cell. Layout: (The ellipsis are there to reduce the code) <TableLayout xmlns:android="http:

  • Android: Is it possible to update a ImageView/ImageButton with a number to show the number of new messages? 2011-04-06

    In iOS we have a feature to update the app icon with the new pending messages for the user by showing a small number on the right-top corner of the icon and similarly I would like to know if we have method to update a ImageView/ImageButton in android

  • Android hide and removing the image in imageview 2011-07-12

    How to remove a image in imageview in android and also how to hide the entire image. Here I have placed an image in imageview by the below code. answerState1.setBackgroundResource(R.drawable.correct); I don't know how to remove or hide the image. Als

  • Android: Remove background drawable programmatically 2011-07-26

    I want to remove background drawable (@drawable/bg) programmatically, is there a way to do that? Currently I have the following XML code in my layout: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/widget29" android:

  • android - passing variable from intent extra to ImageView 2011-09-23

    Android newcomer here. I'm trying to setup an activity to display a selected image. The foundation of this code is from the Hello, Android 3rd edition (Pragmatic Programmer) that I have attempted to modify. Basically, I want to be able to click a but

  • Android java change drawable image 2011-09-30

    Alright so how when i click a button? it will change the a image to a different image my xml of the image i want to change when button press: <ImageView android:id="@+id/bluebtn1" android:src="@drawable/buttonblue" android:layout_width="60dp" andr

  • Android: RelativeLayout background drawable disappears 2011-10-11

    I have an xml layout for a Fragment ( When this Fragment is added for the first time, the background drawable displays fine. But when this Fragment is replaced with another Fragment, then later replaced back in again

  • android: animating a drawable gets cropped, bounds not resized 2011-10-18

    I'm trying to animate a PNG, applying a scale transformation. All works as expected for shrink operations, but when i try and grow the image it gets cropped by the bounds of the original View. How can reset the bounds? Can someone give me an example

  • How to rotate a drawable but not the imageView itself? 2011-11-17

    I'have an image view with a drawable inside. This drawable is a ring similar to the one present in progress dialog : loading_ring : <shape xmlns:android="" android:shape="ring" android:innerRadiusRatio="3"

  • Android, RelativeLayout restarts Marquee-TextView when changing ImageView in same RelativeLayout 2011-11-20

    I have not found a solution for my problem, maybe you can help me here. I am using a RelativeLayout with an ImageView and a TextView as children. The TextView contains a large text and should scroll from right to left. But everytime when I set a new

Copyright (C), All Rights Reserved.

processed in 0.125 (s). 11 q(s)