0

我有TextView来自远程服务器的内容图像并将它们绘制到TextView,例如,我想让这个下载的或绘制的图像在单击以大模式打开图像时可点击

如何使Drawable可点击或有onclicklistener

这是我用于 Html 表单的课程

例如,如果TextView<img src="http://www.bla.com/1.jpg" />

课后下载并绘制此图像,我如何使其具有单击侦听器以以大模式打开它!

`公共类 URLImageParser 实现 ImageGetter {

//Activity Context
Context c;
//TextView Content
TextView container;
//File Cache
FileCache fileCache;
//Imgae Resulotion
int IMGAE_REZ = 100;

/*************************************************
 * Setup Class Values
 */
public URLImageParser( Context c,int position,View t) {

    this.c         = c;
    this.container = (TextView) t;
    fileCache      = new FileCache(c);

}
/*************************************************
 * Start Draw
 */
@SuppressWarnings("deprecation")
public Drawable getDrawable(String source) {

     //Fix Url Spaces 
     source =  ( source != null ) ? source.replace(" ", "%20") :  null; 

     //Not cached yet Fetch and Draw
     if ( source != null )
     {
        //Check if Cached by Bitmap
        Bitmap cached  = Api.bitmap_cache.get(source);    

        //if not in Bitmap Cache get from file cache or Download it
        if ( cached != null )
        {
            Drawable drw;

            drw = new BitmapDrawable(cached);
            drw.setBounds(0,0, (int)(drw.getIntrinsicWidth()*5.5),(int)(drw.getIntrinsicHeight()*5.5));
            return drw;

        }else
        {
          URLDrawable urlDrawable = new URLDrawable();

          // get the actual source
          ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask( urlDrawable , source);
          asyncTask.execute(source);   
          return urlDrawable; 
        }

     }else
     {
         return null;
     }

}
/*******************************************************
 * Decode file as Bitmap
 * Return Bitmap Decoded
 */
private Bitmap decodeFile(File f){

    try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){

            if(width_tmp/2<IMGAE_REZ || height_tmp/2<IMGAE_REZ)
            break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }
        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}

    return null;
}   
/*******************************************************
 * AsyncTask Download or get from cache Drawable
 * Setting Image if exists or broken image
 * Below this class nothing related to Above
 */   
public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable>  {

    //Image Draw
    URLDrawable urlDrawable;
    //If called from Cache BITMAP
    boolean is_cached = false;
    //File Cache id in BitmapCache
    String cacheID;
    //Bitmap to Cache
    Bitmap cacheMe;

    public ImageGetterAsyncTask(URLDrawable d, String cacheID) {
        this.urlDrawable = d;
        this.cacheID     = cacheID;

        if ( d == null )
        {
         cancel(true);
        }
    }
    /*****************************************************
     * Get Draw
     */
    @Override
    protected Drawable doInBackground(String... params) {

        return fetchDrawable(params[0]);
    }
    /*****************************************************
     * Draw Image and Set Bounds if not Set
     */
    @Override 
    protected void onPostExecute(Drawable result) { 

        //if Drawable not null procced
        if ( result != null )
        {
                urlDrawable.setBounds(0,0, (int)(result.getIntrinsicWidth()*5.5),(int)(result.getIntrinsicHeight()*5.5));  
                urlDrawable.drawable = result; 

                int newhight = (URLImageParser.this.container.getHeight() + (int)(result.getIntrinsicHeight()*5.5));
                URLImageParser.this.container.setHeight(newhight);  

                URLImageParser.this.container.setEllipsize(null);
                URLImageParser.this.container.requestLayout();
                URLImageParser.this.container.invalidate();

                Api.bitmap_cache.put(cacheID, cacheMe);
        }
    } 
    /*****************************************************
     * Fetch Draw convert from Bitmap to Draw
     * Return Draw
     */
    @SuppressWarnings("deprecation")
    public Drawable fetchDrawable(String urlString) {

        Drawable drw   = null;

        cacheMe = downloadFile(urlString);


        if ( cacheMe != null )
        {
            drw = new BitmapDrawable(cacheMe);
            drw.setBounds(0,0, (int)(drw.getIntrinsicWidth()*5.5),(int)(drw.getIntrinsicHeight()*5.5));             
        }


        return drw;
    }
    /*******************************************************
     * Return Bitmap of Image String if downloaded or Cached
     * Return Bitmap
     */
    private Bitmap downloadFile (String url )
    {

        Bitmap bitmap = null;

         File f = fileCache.getFile(url);

         bitmap = decodeFile(f);

         //return from cached file
        if ( bitmap != null )
        {
            return bitmap;
        }else
        {
           try {
                //Download and Cache file
                 URL imageUrl  = new URL(url);
                 HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
                 conn.setConnectTimeout(30000);
                 conn.setReadTimeout(30000);
                 conn.setInstanceFollowRedirects(true);
                 InputStream is=conn.getInputStream();
                 OutputStream os = new FileOutputStream(f);
                 Utils.CopyStream(is, os);
                 os.close();

                bitmap = decodeFile(f);

         } catch (Throwable ex){
               ex.printStackTrace();

               if(ex instanceof OutOfMemoryError)
               {
                   Api.bitmap_cache.clear();   
               }
         }
        }
        return bitmap;
    }
}

}`

4

1 回答 1

1
how to make Drawable clickable or has onclicklistener ?

您不需要Drawable图像的侦听器,但您的TextView.

在你的XMLandroid:clickable="true"onClick="your_method"

在你的Javatextview.setClickable(true)textView.setOnClickListner(your_listner)

于 2013-08-31T00:39:04.503 回答