2012-08-16 35 views
0

THIS IS MY主类:如何使用某些预定义的time_interval自动更改ImageView中的图像?

$ `公共类MainActivity扩展活动实现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"); 

} 

}`

这是我的布局代码:

$`

<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"/> 

`

所以当我运行这个代码时,只有最后一张图像被查看,图像不会根据给定的延迟而改变。我的意思是最后的图像加载为静态图像,但我需要它作为动画视图。有人可以回答我吗?

THANKYOU在提前。 。 。 。 。

+0

为什么你复制粘贴代码块,而不是使通用的方法吗? – neworld 2012-08-16 12:54:36

+0

将您的逻辑写入线程而不是使用线程来提供延迟。你所做的并不是线程应该如何实现。 – 2012-08-16 12:58:07

+0

抱歉不能清楚地告诉你,对于android来说很陌生,请简单介绍一下。 。 。 。 – 2012-08-16 13:00:09

回答

0

您的延迟函数不会延迟onCreate,它只是在背景中启动一个线程而不执行任何操作。 onCreate只需继续在其线程上运行(您的应用程序主要UI线程)。

推迟一些动作的正确方法是调用您的ImageView postDelayed功能:

im1.postDelayed(new Runnable() { 
    public void run() { 
     //Code to change image 
    } 
}, 5000); 
+0

即使我有同样的问题,只有循环中的最后一个图像设置为ImageView,它不会周期性地显示图像的变化。 。 。 。 :( – 2012-08-17 06:26:23

相关问题