我正在开发一个Android应用程序,其主要布局中包含scrollview和imagebuttons。我使用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" />
但我想通过在启动画面期间从json webservice下载的图像更新这些图像(图像按钮)。
可以吗?
感谢。
答案 0 :(得分:0)
去吧:
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;
}