gpt4 book ai didi

android - 如何在 Html.ImageGetter 类中监听对绘制图像的点击

转载 作者:搜寻专家 更新时间:2023-11-01 08:34:27 26 4
gpt4 key购买 nike

我有一个 Html.ImageGetter 类,我将它与 Universal Image Loader 一起使用,以加载 标签中的图像,该标签包含在我在线获取的 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);
}
}
}
}

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

最佳答案

图像本身不可点击,但您可以将它们包装在自定义 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());

关于android - 如何在 Html.ImageGetter 类中监听对绘制图像的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37897473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com