我是Android编程的新手。目前,我开发了一个应用程序,通过使用ImageGetter和Html.fromHtml()在TextView中显示HTML内容。我可以在TextView中缓存内容和图像,以便我可以离线查看图像吗?
答案 0 :(得分:1)
好吧,前几天我也在做同样的事情。我正在使用ImageGetter显示带有textview的html
。但它没有给出提议的结果。那么我使用了Jsoup libaray并提取元素并在Textvew
中显示它们,当img
标记出现时,我使用UIL通用图像库在Imageview
中显示图像。这个libaray有缓存图像的选项。
您只需要缓存html字符串,然后将其传递给下面的类以及布局rest jsoup和UIL将处理。
public class PostContentHandler {
Context context;
String content;
LinearLayout linearLayout;
public PostContentHandler(Context context, String content , LinearLayout linearLayout){
this.context=context;
this.content=content;
this.linearLayout=linearLayout;
}
public void setContentToView(){
List<String> p = new ArrayList<>();
List<String> src = new ArrayList<>();
Document doc = Jsoup.parse(content);
Elements elements = doc.getAllElements();
for(Element element :elements){
Tag tag = element.tag();
if(tag.getName().matches("h[1-6]{1}")){
String heading = element.select(tag.getName().toString()).text();
TextView textView = new TextView(context);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setTextSize(20);
textView.setText(heading);
textView.setPadding(5, 0, 5, 0);
textView.setTextColor(R.color.black);
linearLayout.addView(textView);
}
if(tag.getName().equalsIgnoreCase("p")){
element.select("img").remove();
String body= element.html();
TextView textView = new TextView(context);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setText(Html.fromHtml(body));
textView.setTextColor(R.color.black);
textView.setPadding(10,0,10,0);
linearLayout.addView(textView);
p.add(body);
}
if (tag.getName().equalsIgnoreCase("img")){
String url = element.select("img").attr("src");
final ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(url, imageView, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
int height=loadedImage.getHeight();
imageView.getLayoutParams().height=getScreenWidth();
imageView.setAdjustViewBounds(true);
imageView.requestLayout();
}
@Override
public void onLoadingStarted(String imageUri, View view) {
super.onLoadingStarted(imageUri, view);
}
});
linearLayout.addView(imageView);
src.add(url);
}
}
}
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
}
答案 1 :(得分:0)
这是我的代码。我尝试调试,它显示代码转到onLoadingFailed()并且不转到onLoadingComplete()。我不明白为什么。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout)findViewById(R.id.linearLayout1);
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisc(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(100)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
.discCacheSize(10 * 1024 * 1024).build();
ImageLoader.getInstance().init(config);
setContentToView();
}
public void setContentToView(){
List<String> p = new ArrayList<>();
List<String> src = new ArrayList<>();
Document doc = Jsoup.parse(content);
Elements elements = doc.getAllElements();
for(Element element :elements){
Tag tag = element.tag();
if(tag.getName().matches("h[1-6]{1}")){
String heading = element.select(tag.getName().toString()).text();
TextView textView = new TextView(this);
textView.setText(Html.fromHtml(heading));
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(textView);
}
if(tag.getName().equalsIgnoreCase("p")){
element.select("img").remove();
String body= element.html();
TextView textView = new TextView(this);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setText(Html.fromHtml(body));
this.linearLayout.addView(textView);
p.add(body);
}
if (tag.getName().equalsIgnoreCase("img")){
String url = element.select("img").attr("src");
final ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(url, imageView, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
super.onLoadingStarted(imageUri, view);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
int height=loadedImage.getHeight();
imageView.getLayoutParams().height=getScreenWidth();
imageView.setAdjustViewBounds(true);
imageView.requestLayout();
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
super.onLoadingCancelled(imageUri, view);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
super.onLoadingFailed(imageUri, view, failReason);
}
});
linearLayout.addView(imageView);
src.add(url);
}
}
}
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}