2012-01-07 83 views
0

我想要当我按下特定的按钮图像(如地图)来显示。什么是我的应用程序更轻量级?从URL获取它或将其放在可绘制文件夹上并显示它?图像绘制vs图像来自Url

如果我选择第二个,并且想要实现“后退”按钮,我将不得不将整个事物放在一个额外的类中?

无论如何,我的应用程序需要连接到Internet。

回答

1

我会用一个可绘制的,纯粹出于这个原因,当用户启动应用程序没有WIFI/3G /等时会发生什么。或者连接速度非常慢。您说您的应用程序需要连接,但这并不一定意味着用户启动应用程序时将启用它。

它也更容易被拉伸了不少,只是把它放到绘制文件夹,然后将其指定为源为您ImageView的(如果你使用一个可点击的ImageView)

<ImageView android:layout_height="wrap_content" android:id="@+id/imageView1" 
    android:layout_width="wrap_content" android:src="@drawable/your_image"> 
</ImageView> 

或者,如果你的背景在xml文件中使用Button。

<Button android:text="" android:id="@+id/button1" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:background="@drawable/your_image"> 
    </Button 

尽管你可能想使用一个选择器2个图像之间的改变(按下并未按下状态)

而是指定选择XML文件作为背景/源

ie android:background="@drawable/back_button_selector"

下载图像需要在后台线程(如AsycnTask)中完成,否则在下载图像时UI将不会响应。

但是,如果您决定下载图像的原因是某种原因(例如,您想要更改图像而无需更新并在服务器上进行更改),则可以使用AsyncTask下载图像(您可以使用它作为内部类)

public class GetImage extends AsyncTask<ImageView, Void, ImageView> { 

String url = null; 
Bitmap thumbnail = null; 
public GetImage(String url){ 
    this.url = url; 
} 
@Override 
protected void onPreExecute() { 

} 

@Override 
protected ImageView doInBackground(ImageView... params) { 

    try { 
     thumbnail = BitmapFactory.decodeStream((InputStream) new URL(url).getContent()); 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return params[0]; 

} 

@Override 
public void onPostExecute(ImageView result) { 
    result.setImageBitmap(thumbnail); 

} 
}