如何在Html.ImageGetter类中侦听绘制图像的点击次数

时间:2016-06-18 13:24:44

标签: android universal-image-loader

我有一个Html.ImageGetter类,我使用Universal Image Loader在< img>中加载图像标签包含在我在线获取的html json文件中。

图像加载得相当不错,但在较小的设备上,图像会缩小以匹配设备的宽度,因此会使某些图像太小而无法看到内容。

我的目的是在图像上设置单击侦听器并显示缩放对话框,用户可以在其中查看图像的完整大小。目前,我发现很难听取点击次数。

这是我的ImageGetter类

public class UILImageGetter implements Html.ImageGetter{
    Context c;
    TextView container;
    UrlImageDownloader urlDrawable;


    public UILImageGetter(View textView, Context context) {
        this.c = context;
        this.container = (TextView) textView;
    }

    @Override
    public Drawable getDrawable(String source) {

        urlDrawable = new UrlImageDownloader(c.getResources(), source);

        if (Build.VERSION.SDK_INT >= 21) {
        urlDrawable.mDrawable = c.getResources().getDrawable(R.drawable.default_thumb,null);
        } else {
            urlDrawable.mDrawable = c.getResources().getDrawable(R.drawable.default_thumb);
        }
        ImageLoader.getInstance().loadImage(source, new SimpleListener(urlDrawable));
        return urlDrawable;
    }

    private class SimpleListener extends SimpleImageLoadingListener {
        UrlImageDownloader mUrlImageDownloader;


        public SimpleListener(UrlImageDownloader downloader) {
            super();
            mUrlImageDownloader= downloader;
        }

        @Override
        public void onLoadingStarted(String imageUri, View view) {
            Log.d("DEBUG", "onLoadingStarted called");
            //spinner.setVisibility(View.VISIBLE);

        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            //spinner.setVisibility(View.GONE);

            int width = loadedImage.getWidth();
            int height = loadedImage.getHeight();

            int newWidth = width;
            int newHeight = height;

            if (width > container.getWidth()) {
                newWidth = container.getWidth();
                newHeight = (newWidth * height) / width;
            }

            if (view != null) {
                container.getLayoutParams().width = newWidth;
                container.getLayoutParams().height = newHeight;
            }

            Drawable result = new BitmapDrawable(c.getResources(), loadedImage);
            result.setBounds(0, 0, newWidth, newHeight);

            mUrlImageDownloader.setBounds(1, 1, newWidth, newHeight);
            mUrlImageDownloader.mDrawable = result;
            container.invalidate();

            container.setText(container.getText());

            if (view != null) {
                //Don't know if this is the right place to listener for clicks but it ain't working
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d("TheImage", "Image Clicked");
                    }
                });
            }

        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            String message = null;
            switch (failReason.getType()) {
                case IO_ERROR:
                    message = "Input/Output error";
                    break;
                case DECODING_ERROR:
                    message = "Image can't be decoded";
                    break;
                case NETWORK_DENIED:
                    message = "Downloads are denied";
                    break;
                case OUT_OF_MEMORY:
                    message = "Out Of Memory error";
                    break;
                case UNKNOWN:
                    message = "Unknown error";
                    break;
            }


//            Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();
            Log.w("UILImageGetter", message);

            //spinner.setVisibility(View.GONE);
        }



    }

    private class UrlImageDownloader extends BitmapDrawable {
        public  Drawable mDrawable;

        public UrlImageDownloader(Resources resources, String filepath) {
            super(resources, filepath);
            mDrawable = new BitmapDrawable(resources, filepath);
        }

        @Override
        public void draw(Canvas canvas) {
            if (mDrawable != null) {
                mDrawable.draw(canvas);
            }
        }
    }
}

拜托,你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

图片本身不可点击,但您可以将它们包装在自定义URLSpan中,并收听来自URLSpan的点击次数。使用Html.fromHtml(...)解析HTML并执行

Spannable html = (Spannable) Html.fromHtml(...);
for (ImageSpan span : html.getSpans(0, html.length(), ImageSpan.class) {
    int flags = html.getSpanFlags(span);
    int start = html.getSpanStart(span);
    int end = html.getSpanEnd(span);

    html.setSpan(new URLSpan(span.getSource()) {
        @Override public void onClick(View v) {
            /* handle click here */
        }
    }, start, end, flags);
}
textView.setText(html);
textView.setMovementMethod(LinkMovementMethod.getInstance());