final ArrayList<Link> links = new ArrayList<Link>(); HtmlCleaner mHtmlCleaner = new HtmlCleaner(); CleanerTransformations transformations = new CleanerTransformations(); TagTransformation tt = new TagTransformation("img", "imgs", true); transformations.addTransformation(tt); mHtmlCleaner.setTransformations(transformations); //clean html = mHtmlCleaner.getInnerHtml(mHtmlCleaner.clean(parsed_content)); TagNode root = mHtmlCleaner.clean(html); root.traverse(new TagNodeVisitor() { @Override public boolean visit(TagNode tagNode, HtmlNode htmlNode) { if (htmlNode instanceof TagNode) { TagNode tag = (TagNode) htmlNode; String tagName = tag.getName(); if ("iframe".equals(tagName)) { if (tag.getAttributeByName("src") != null) { Link link = parseTag(tag, "iframe"); if (link != null) { links.add(link); } } } if ("imgs".equals(tagName)) { String src = tag.getAttributeByName("src"); //ico if (src != null && !src.endsWith("/") && !src.toLowerCase().endsWith("ico")) { Link link = parseTag(tag, "img"); if (link != null) { links.add(link); } } } } return true; } });
public Link parseTag(TagNode tag,String type) { final String src = tag.getAttributeByName("src"); final String width = tag.getAttributeByName("width"); final String height = tag.getAttributeByName("height"); int iWidth=0, iHeight=0; try { iWidth = Integer.parseInt(width.split("\\.")[0]); iHeight = Integer.parseInt(height.split("\\.")[0]); } catch (Exception e) {} // 1/3 - if (iWidth>((displayWidth*1)/3) && iHeight>0) { iHeight = (displayWidth * iHeight)/iWidth; iWidth = displayWidth; } // if (iWidth>45 && iHeight>45) { int scaleFactor = 1; if (iWidth<displayWidth/3) { // 2 scaleFactor = 2; } if (iHeight>=4096 || iWidth>=4096 || src.endsWith("gif")) { type = "iframe"; } return new Link(type, src, iWidth*scaleFactor, iHeight*scaleFactor,""); } return null; }
private ArrayList<Link> data = new ArrayList<Link>();; for(int i=0;i<links.size();i++) { final Link link = links.get(i); if (link.type.equals("txt")) continue; int pos = html.indexOf(link.src); String abzats = ""; if (pos>0) { abzats = html.substring(0, pos); int closeTag = html.indexOf(">",pos)+1; if (closeTag>0) { html = html.substring(closeTag); } if (!TextUtils.equals("", abzats)) { data.add(new Link("txt","",0,0,abzats)); } } //add text if (link.type.equals("img")) { //add image data.add(link); } //add iframe if (link.type.equals("iframe")) { data.add(link); } } data.add(new Link("txt","",0,0,html));
if (link.type.equals("txt")) { // return getTextView(activity, link.txt); } if (link.type.equals("img")) { // } ... //, textview public TextView getTextView(Context context,String txt){ TextView textView = new TextView(activity); textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setText(Html.fromHtml(txt)); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,fontSize); textView.setPadding(UtilsScreen.dpToPx(8),0,UtilsScreen.dpToPx(8),0); textView.setAutoLinkMask(Linkify.ALL); textView.setLineSpacing(0, 1.4f); ColorStateList cl = null; try { XmlResourceParser xpp = context.getResources().getXml(R.xml.textview_link_color_selector); cl = ColorStateList.createFromXml(context.getResources(), xpp); textView.setLinkTextColor(cl); } catch (Exception e) { textView.setLinkTextColor(Color.parseColor("#6fb304")); } return textView; }
String youtubeVideo = ""; if (link.src.contains("lj-toys") && link.src.contains("youtube") && link.src.contains("vid=")) { try { youtubeVideo = link.src.substring(link.src.indexOf("vid=") + 4, link.src.indexOf("&", link.src.indexOf("vid=") + 4)); } catch (Exception e) { e.printStackTrace(); } } //http://www.youtube.com/embed/ZSPyC6Uv9xw if (link.src.contains("youtube") && link.src.contains("embed/")) { try { youtubeVideo = link.src.substring(link.src.indexOf("embed/") + 6); } catch (Exception e) { e.printStackTrace(); } } if (!youtubeVideo.equals("")) { //new RelativeLayout RelativeLayout relativeLayout = new RelativeLayout(activity); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); ImageView imageView = new ImageView(activity); imageView.setLayoutParams(layoutParams); relativeLayout.addView(imageView); imageView.setBackgroundColor(Color.parseColor("#f8f8f8")); if (link.width>0 && link.height>0) { aq.id(imageView).width(link.width, false).height(link.height, false); } String youtubeVideoImage = youtubeVideo; if (youtubeVideoImage.contains("?")) { //params youtubeVideoImage = youtubeVideoImage.substring(0, youtubeVideoImage.indexOf("?")); } if (link.width>0) { aq.id(imageView).image("http://img.youtube.com/vi/" + youtubeVideoImage + "/0.jpg", true, false, link.width, 0, null, AQuery.FADE_IN_NETWORK); } else { aq.id(imageView).image("http://img.youtube.com/vi/" + youtubeVideoImage + "/0.jpg"); } ImageView imageViewPlayBtn = new ImageView(activity); relativeLayout.addView(imageViewPlayBtn); RelativeLayout.LayoutParams playBtnParams = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); playBtnParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); imageViewPlayBtn.setLayoutParams(playBtnParams); aq.id(imageViewPlayBtn).image(R.drawable.play_youtube); final String videoId = youtubeVideo; aq.id(relativeLayout).clickable(true).clicked(new View.OnClickListener() { @Override public void onClick(View v) { try { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + videoId)); intent.putExtra("VIDEO_ID", videoId); activity.startActivity(intent); } catch (Exception e) { activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + videoId))); } } }); return relativeLayout;
Source: https://habr.com/ru/post/242911/
All Articles