2012-08-17 56 views
3
package com.example.imagechange; 

public class MainActivity extends Activity 
{ 

ImageView imageView; 
int []imageArray={R.drawable.a0,R.drawable.a1,R.drawable.a2,R.drawable.a3}; 


@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    imageView = (ImageView)findViewById(R.id.imageView1); 

    final Handler handler = new Handler(); 
    Runnable runnable = new Runnable() 
    { 
       int i=0; 
       public void run() 
       { 
        imageView.setImageResource(imageArray[i]); 
        i++; 
        if(i>imageArray.length-1) 
        { 
        i=0;  
        } 
        handler.postDelayed(this, 3000); //for interval... 
       } 

    }; 
      handler.postDelayed(runnable, 1000); //for initial delay.. 
    } 




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

MY ACTIVITY_MAIN:定期在ImageView中更改圖像。 。 。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 



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

</RelativeLayout> 

所以在上面的代碼,我在繪製複製的圖像,並定期更換,但我需要從url獲得的圖像,並在ImageView顯示它。

+0

定期在ImageView中更改圖像。 。 。 。 (這是什麼意思?)你想知道如何從URL創建位圖嗎? – Shrikant 2012-08-17 06:58:51

+0

其實我並不想從網站上獲取圖片。 。 。假設我有4個URL並下載它,並在ImageView中顯示,並且我必須定期在相同的圖像視圖中更改圖像。 。 。 。 – 2012-08-17 09:55:40

回答

0

您的代碼從一個資源集圖像中的線

imageView.setImageResource(imageArray [I]);

你想要做的是使用UrlImageViewHelper從URL加載圖像。

0

請嘗試以下代碼:首先,您需要從URL獲取圖像路徑,然後將該URL傳遞給此函數。

public static Bitmap getBitmapFromURL(String src) { 
    try { 
     URL url = new URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

沒有什麼太複雜 - 簡單地創建使用「SRC」字符串(即字符串SRC =「http://thinkandroid.wordpress.com」)的URL對象,連接到它,並使用Android的BitmapFactory類解碼輸入流。結果應該是你想要的位圖對象! 而在這之後:

imageView.setImageBitmap(bitmap); 
+0

感謝您的回覆,我通過下載SmartImageView完成了它 - http://loopj.com/android-smart-image-view/ – 2012-08-17 13:05:28

2
ImageView MyImageView; 
int []imageArray={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.one); 
    MyImageView=(ImageView)findViewById(R.id.imageView1); 

    /*here the countdown start for images*/ 

    final Handler handler = new Handler(); 
    Runnable runnable = new Runnable() { 
     int i=0; 
     public void run() { 
      MyImageView.setImageResource(imageArray[i]); 
      i++; 
      if(i>imageArray.length-1) { 
       i=0;  
      } 
      handler.postDelayed(this, 500); //for interval... 
     } 
    }; 
    handler.postDelayed(runnable, 2000); //for initial delay.. 
    /*here the button click counter start */ 
} 

請在re>layouts>drawable第一繪製的文件夾,並放置圖片在它a.png,b.png等。