0

我有一个应用程序,它有一组选项卡,其中的图像设置为 TabIndecator。我想从 URL 中获取图像并将 TabIndecator 图像设置为为该特定选项卡指定的 URL。有没有办法这样做?我可以使用 ImageView 并将其设置为 TabIndecator 吗?

4

2 回答 2

0

我想从 URL 中获取图像并将 TabIndecator 图像设置为为该特定选项卡指定的 URL。有没有办法这样做?

不是直接,您必须将图像作为 a 下载到设备Bitmap,将其包装在 aBitmapDrawable中,并将其设置为TabSpect.setIndicator()

我可以使用 ImageView 并将其设置为 TabIndecator 吗?

当然,如果您愿意,TabSpec.setIndicator()可以将 aView作为参数。

于 2012-08-27T18:39:29.183 回答
0

此方法将允许您从图像的 URL 获取本地位图。我的评论是西班牙语,但我希望这个例子无论如何都会有用。确保在 AsyncTask 或类似的(不是在 UI 线程上)运行它:

private static final int IO_BUFFER_SIZE = 8 * 1024;
private static final int MINIMO_TAM = 10;
public static final int MAXIMO_TAM = 640;

public static Bitmap loadRemoteImage(CharSequence urlImagen) {
    if (null == urlImagen) {
        return null;
    }

    Bitmap bm = null;
    InputStream is = null;
    BufferedInputStream bis = null;

    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.addRequestInterceptor(new GzipHttpRequestInterceptor());
    httpclient.addResponseInterceptor(new GzipHttpResponseInterceptor());

    try {
        String urlSinEspacios = urlImagen.toString().replace(" ", "+");

        // Hacer la llamada
        HttpGet httpget = new HttpGet(urlSinEspacios);
        HttpEntity entity = httpclient.execute(httpget).getEntity();

        is = entity.getContent();
        bis = new BufferedInputStream(is, IO_BUFFER_SIZE);
        //Obtener solo el tamaño
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(bis, null, o);
        try {
            bis.close();
            is.close();
        } catch (Exception e) {
        }

        //Calcular mejor escala
        int scale = 1;
        if (o.outHeight > MAXIMO_TAM || o.outWidth > MAXIMO_TAM) {
            scale = (int) Math.pow(2, (int) Math.round(Math.log(MAXIMO_TAM / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        //Descargar el real
        entity = httpclient.execute(httpget).getEntity();
        is = entity.getContent();
        bis = new BufferedInputStream(is, IO_BUFFER_SIZE);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inTempStorage = new byte[16 * 1024];
        options.inSampleSize = scale;
        bm = BitmapFactory.decodeStream(bis, null, options);
        // Finalizado
        httpclient.getConnectionManager().shutdown();
    } catch (Exception e) {

        bm = null;
    } finally {
        try {
            bis.close();
            is.close();
            // Finalizado
            httpclient.getConnectionManager().shutdown();
        } catch (Exception e) {
        }
    }

    return bm;
}

然后,您可以使用 BitmapDrawable 包装此 Bitmap 并使用:

tabHost.newTabSpec("TODO").setIndicator("TODO", TODO).setContent(TODO);
于 2012-08-27T18:40:09.243 回答