0

THIS IS MY MAIN CLASS :

$ `public class MainActivity extends Activity implements Runnable {

ImageView im1,im2,im3;
int b,p=0,j,k,l;
String b1 ;
Bitmap img;
Bitmap bm;
ArrayList<String> arl = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    im1 = (ImageView)findViewById(R.id.imageView1);
    im2 = (ImageView)findViewById(R.id.imageView2);
    im3 = (ImageView)findViewById(R.id.imageView3);

    arl.add("http://dev.purpletab.com/files/jewels/original/Jewel_1344148107.png");
    arl.add("http://dev.purpletab.com/files/jewels/original/Jewel_1332428944.png");
    arl.add("http://dev.purpletab.com/files/jewels/original/Jewel_1341902610.png");

    while(p<1)
    {
      for(j=0;j<3;j++)
      {

        Bitmap bitmap1 = DownloadImage(arl.get(j));
        Log.e("image of j : ", ""+bitmap1);
        im1.setImageBitmap(bitmap1);
        delay();

        k=j+1;
        if(k==3)
            k=0;
        Bitmap bitmap2 = DownloadImage(arl.get(k));
        Log.e("image of k : ", ""+bitmap2);
        im2.setImageBitmap(bitmap2);
        delay();

        l=k+1;
        if(l==3)
            l=0;
        Bitmap bitmap3 = DownloadImage(arl.get(l));
        Log.e("image of l : ", ""+bitmap3);
        im3.setImageBitmap(bitmap3);
        delay();

      }         
    p++;
    }
}
private void delay()
{
     Thread t = new Thread()
     {
        public void run() 
        {
             Log.e("entered thread", " entered run method");

             try
                    {
                        Log.e("entered for loop ", "loop : ");
                        Thread.sleep(10000);
                    }
                catch(Exception e)
                    {
                    Log.e("thread error", ""+e);
                    }

        }

     };
     t.start();
     Log.e("started thread", ""+t);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

private Bitmap DownloadImage(String URL)
{        
    Log.e("bitmap d/l",""+URL);
    Bitmap bitmap = null;
    InputStream in = null;        
    try 
    {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in);
         Log.e("downloaded images ", " images ");
        in.close();
    } 
    catch (IOException e1) 
    {
        e1.printStackTrace();
         Log.e("started thread", ""+e1);
    }
    return bitmap;                
}

private InputStream OpenHttpConnection(String urlString)throws IOException
{
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))throw new IOException("Not an HTTP connection");

    try
    {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();
        response = httpConn.getResponseCode();                
        if (response == HttpURLConnection.HTTP_OK) 
        {
            in = httpConn.getInputStream();  
             Log.e("download in progress", " . . . ");
        }
        else if(response != HttpURLConnection.HTTP_OK)
        {
            //ProfileImgPreview.setBackgroundResource(R.drawable.photo_bg);
        }
    }
    catch (Exception ex)
    {
        throw new IOException("Error connecting");            
    }
    return in;    
}
@Override
public void run() {
     Log.e("started the other thread", "not defined");

}

}`

AND THIS IS MY LAYOUT CODE :

$ `

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:contentDescription="@string/app_name"
    android:src="@drawable/a0"/>

 <ImageView
    android:id="@+id/imageView2"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/imageView1"
    android:contentDescription="@string/app_name"
    android:src="@drawable/a1"/>

  <ImageView
    android:id="@+id/imageView3"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/imageView2"
    android:contentDescription="@string/app_name"
    android:src="@drawable/a2"/>

`

so when i run this code , only the last image is viewed and the images are not changing according to the delay given . i mean the last images are loaded as static images , but i need it as animated view . could anybody plz answer me ?

THANKYOU IN ADVANCE . . . . .

  • Why you copy-paste code blocks instead of making universal method? – neworld Aug 16 '12 at 12:54
  • Write your logic inside the thread instead of using the thread just to provide a delay. What you are doing is not how threads are supposed to be implemented. – Vikram Gupta Aug 16 '12 at 12:58
  • Sorry could not get you clearly , am new to android and could u please brief me . . . . – VIGNESH.SRINIVASAGAN Aug 16 '12 at 13:00
  • i tried , but don't know why , i could not get the images from the thread . i need to implement some delay between the image change and so i created the thread if there is any other method to implement time_delay plz let me know .. . . – VIGNESH.SRINIVASAGAN Aug 16 '12 at 13:03
  • and the while loop is just for checking i specified the value as 1 , i gave 10 and i got OUTOFMEMORY error in emulator . . . . thatso checked with 1 . – VIGNESH.SRINIVASAGAN Aug 16 '12 at 13:07

1 Answers1

0

Your delay function does nothing to delay onCreate, it just starts a Thread in background which does nothing. onCreate simply proceeds to run on its thread (your apps main ui thread).

The right way to delay some action would be to call postDelayed function on your ImageView:

im1.postDelayed(new Runnable() {
    public void run() {
        //Code to change image
    }
}, 5000);
devmiles.com
  • 9,895
  • 5
  • 31
  • 47
  • even after i have the same problem , only the last image in the loop is set to the ImageView and it doesn't show the change of images periodically . . . . :( – VIGNESH.SRINIVASAGAN Aug 17 '12 at 06:26