app创建后更新UI



我正在开发一个Android应用程序,在其主布局中具有滚动视图和图像按钮。我使用来自drawable文件夹的图像,并通过它们的id来调用它们。

//AboutScreen.java class
    private ImageButton mStartButton3;
    mStartButton3 = (ImageButton) findViewById(R.id.woops3); 
    mStartButton3.setOnClickListener(this);

//MainLayout.xml
    <ImageButton
        android:id="@+id/woops3"
        android:background="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:gravity="center_horizontal|center_vertical"
        android:src="@drawable/woops6"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
             android:layout_marginTop="10dp"
        android:text="@string/button_start"
        android:textSize="@dimen/menu_title" />

但是我想更新这些图像(imagebuttons)的图像,我从一个json webservice在闪屏期间下载。

可以做到吗?

谢谢。

Go for this:

    public void onCreate(Bundle savedInstanceState) 
{ 
Bitmap bitmap = DownloadImage("Your URL");
ImageView img = (ImageView) findViewById(R.id.imagefromserver);
img.setImageBitmap(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();                                 
            }                     
        }
        catch (Exception ex)
        {
            throw new IOException("Error connecting");            
        }
        return in;     
    }
    private Bitmap DownloadImage(String URL)
    {        
        Bitmap bitmap = null;
        InputStream in = null;        
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return bitmap;                
    }

相关内容

  • 没有找到相关文章

最新更新