extends ImageView : ImageView « UI « Android






extends ImageView

  
package app.test;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.AttributeSet;
import android.widget.ImageView;

class WebImageView extends ImageView {

  private Drawable mPlaceholder, mImage;
  public WebImageView(Context context, AttributeSet attrs, int defaultStyle) {
    super(context, attrs, defaultStyle);
  }

  public void setPlaceholderImage(Drawable drawable) {
    mPlaceholder = drawable;
    if (mImage == null) {
      setImageDrawable(mPlaceholder);
    }
  }

  public void setPlaceholderImage(int resid) {
    mPlaceholder = getResources().getDrawable(resid);
    if (mImage == null) {
      setImageDrawable(mPlaceholder);
    }
  }

  public void setImageUrl(String url) {
    DownloadTask task = new DownloadTask();
    task.execute(url);
  }

  private class DownloadTask extends AsyncTask<String, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... params) {
      String url = params[0];
      try {
        BufferedInputStream bis = new BufferedInputStream((new URL(url)).openConnection().getInputStream());
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
          baf.append((byte) current);
        }
        byte[] imageData = baf.toByteArray();
        return BitmapFactory.decodeByteArray(imageData, 0,imageData.length);
      } catch (Exception exc) {
        return null;
      }
    }

    @Override
    protected void onPostExecute(Bitmap result) {
      mImage = new BitmapDrawable(result);
      if (mImage != null) {
        setImageDrawable(mImage);
      }
    }
  };
}

public class Test extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    WebImageView imageView = (WebImageView) findViewById(R.id.webImage);
    imageView.setPlaceholderImage(R.drawable.icon);
    imageView.setImageUrl("http://yourdomain.com/a.gif");
  }
}
//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
  <app.test.WebImageView
    android:id="@+id/webImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
  />
</LinearLayout>

   
    
  








Related examples in the same category

1.Set ImageView width, height
2.Load image with ImageView
3.Using ImageView within ListActivity
4.Using ImageView to display image and reference Image resource in layout xml file
5.ImageView background
6.Set Layout Parameters, Scale Type, background for ImageView
7.Using ImageView to display Image
8.ImageView click listener
9.Set Image resource for ImageView
10.Set Image Bitmap for ImageView
11.Set image Uri for ImageView
12.Adding Touch Listener to ImageView
13.Demonstrates setting size constraints on android.widget.ImageView
14.Create ImageView
15.Create an image view, given a drawable. you can set the max size of this imageview as well.
16.download images from the Internet and binds those with the provided ImageView.
17.Choose a Picture