Skip to content

Commit

Permalink
重新调整解析策略,修复无法浏览图片的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiboboo committed Feb 18, 2019
1 parent 4e78d75 commit 424c83a
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,32 +166,31 @@ private UserEntity onParseProplePictures(String url) throws IOException
final String baseUrl = "https://www.pexels.com" + url;
Document doc = Jsoup.connect(baseUrl).get();
UserEntity entity = new UserEntity();
Element element = doc.selectFirst("img[class*=profile-header__img]");
entity.author = element.attr("alt");
entity.avatar = element.attr("src");
Elements statsElements = doc.selectFirst("div[class*=profile-header__stats]")
.select("a[class*=profile-header__link fact-box fact-box--inline]");
Element element = doc.selectFirst("div[class*=profile-header__user-info__avatar__container]");
entity.author = element.selectFirst("img").attr("alt");
entity.avatar = element.selectFirst("img").attr("src");
Elements statsElements = doc.select("span[class=profile-header__fact]");
for (Element linkElement : statsElements)
{
String fact = linkElement.selectFirst("span[class*=fact-box__fact]").text();
String title = linkElement.selectFirst("span[class*=fact-box__title]").text();
if (title.contains("Total"))
entity.totalViews = fact;
if (title.contains("All"))
entity.historyRank = fact;
if (title.contains("30"))
entity.day30Rank = fact;
String html = linkElement.html();
String value = linkElement.selectFirst("strong").text();
if (html.contains("Total"))
entity.totalViews = value;
if (html.contains("All-time"))
entity.historyRank = value;
if (html.contains("30"))
entity.day30Rank = value;
}
Elements pageElement = doc.selectFirst("div[class*=tabs tabs--in-container clear]").getElementsByTag("a");
Elements pageElement = doc.selectFirst("div[class*=rd__tabs]").select("a[class*=rd__tabs__tab]");
for (Element pElement : pageElement)
{
String text = pElement.text();
String linkUrl = pElement.attr("href");
if (text.contains("hoto"))
if (text.contains("Photo"))
data.putString("page.photo", linkUrl);
if (text.contains("ollection"))
if (text.contains("Collections"))
data.putString("page.collect", linkUrl);
if (text.contains("tats"))
if (text.contains("Stats"))
data.putString("page.stats", linkUrl);
}
return entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.bobby.pictures.util.AsynchronousManager;
import com.bobby.pictures.util.ExecuteApi;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
Expand Down Expand Up @@ -50,6 +51,7 @@ protected void setupViews(View contentView)
manager.setJustifyContent(JustifyContent.SPACE_BETWEEN);
manager.setFlexWrap(FlexWrap.WRAP);
manager.setFlexDirection(FlexDirection.ROW);
manager.setAlignItems(AlignItems.FLEX_START);
mRecyclerView.setLayoutManager(manager);
mRecyclerView.setHasFixedSize(true);
adapter = new PictureAdapter(new ArrayList<PhotoEntity>());
Expand Down
40 changes: 11 additions & 29 deletions app/src/main/java/com/bobby/pictures/adapter/PictureAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup;

import com.bobby.pictures.R;
Expand All @@ -25,6 +25,8 @@
*/
public class PictureAdapter extends BaseQuickAdapter<PhotoEntity, BaseViewHolder>
{
private final String TAG = PictureAdapter.class.getSimpleName();

public PictureAdapter(@Nullable List<PhotoEntity> data)
{
super(R.layout.fragment_picture_world_item, data);
Expand All @@ -36,48 +38,28 @@ protected void convert(final BaseViewHolder helper, PhotoEntity item)
NiceImageView mImageView = helper.getView(R.id.image_thumbnail);
int imageWidth = item.width;
int imageHeight = item.height;
Resources r = mContext.getResources();
DisplayMetrics outMetrics = r.getDisplayMetrics();
int targetWidth = ((outMetrics.widthPixels - r.getDimensionPixelSize(R.dimen.dp_3) * 4) / 3);
int targetHeight = (int) (imageHeight / (float) (imageWidth / targetWidth));
ViewGroup.LayoutParams params = mImageView.getLayoutParams();
int targetWidth = this.calculateImageByScreenWidth(imageWidth);
params.width = targetWidth;
params.height = imageHeight;
params.height = targetHeight;
Log.d(TAG, targetWidth + "|" + targetHeight + "&" + imageWidth + "|" + imageHeight);
ColorDrawable mLoadingColor;
int[] rgb = item.rgb;
if (rgb.length == 3)
mLoadingColor = new ColorDrawable(Color.rgb(rgb[0], rgb[1], rgb[2]));
else
mLoadingColor = new ColorDrawable(Color.WHITE);
Log.d(TAG, item.smallSrc);
Glide.with(mContext)
.load(item.smallSrc)
.apply(new RequestOptions()
.placeholder(mLoadingColor)
.error(mLoadingColor)
.centerCrop()
.override(targetWidth, imageHeight))
.override(targetWidth, targetHeight))
.into(mImageView);
}

/**
* 计算一张图片在屏幕中所占的位置宽度
* <p>
* 这首先取决于图片本身的宽度,根据图片宽度占屏幕宽度比例(difference),算出三种结果:
* <ul>
* <li>difference < 0.4 ? screenWidth * 0.3</li>
* <li>difference < 0.75 ? screenWidth * 0.485</li>
* <li>screenWidth * 0.98</li>
* </ul>
*
* @param imageWidth 图片的原始宽度
* @return 返回经过计算后得到的图片占位宽
*/
private int calculateImageByScreenWidth(int imageWidth)
{
Resources r = mContext.getResources();
DisplayMetrics outMetrics = r.getDisplayMetrics();
float difference = (imageWidth / (float) outMetrics.widthPixels);
if (difference < 0.4)
return (int) (outMetrics.widthPixels * 0.29);
if (difference < 0.75)
return (int) (outMetrics.widthPixels * 0.485);
return (int) (outMetrics.widthPixels * 0.98);
}
}
78 changes: 41 additions & 37 deletions app/src/main/java/com/bobby/pictures/util/ExecuteApi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bobby.pictures.util;

import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;

import com.bobby.pictures.entity.PhotoEntity;
Expand All @@ -23,6 +24,8 @@
*/
public final class ExecuteApi
{
private final String TAG = ExecuteApi.class.getSimpleName();

public enum Apis
{
HOME_LIST(0xff15),
Expand Down Expand Up @@ -144,13 +147,13 @@ private ArrayList<PhotoEntity> getHomeImages(int page) throws IOException
photo.largeSrc = imageEl.attr("data-large-src");
photo.smallSrc = imageEl.attr("src");
photo.pinSrc = imageEl.attr("data-pin-media");
String widthText = imageEl.attr("width").trim();
String widthText = imageEl.attr("data-image-width").trim();
if (!widthText.matches("\\d+"))
widthText = "0";
widthText = "1";
photo.width = Integer.parseInt(widthText);
String heightText = imageEl.attr("height").trim();
String heightText = imageEl.attr("data-image-height").trim();
if (!heightText.matches("\\d+"))
heightText = "0";
heightText = "1";
photo.height = Integer.parseInt(heightText);
String style = imageEl.attr("style");
String[] rgbs = style.replaceAll("[a-zA-Z():]", "").split(",");
Expand All @@ -163,12 +166,16 @@ private ArrayList<PhotoEntity> getHomeImages(int page) throws IOException
rgb[i] = Integer.parseInt(text);
}
photo.rgb = rgb;
photo.deatilPage = element.selectFirst("a[class*=js-photo-link]").attr("href");
UserEntity user = new UserEntity();
user.avatar = element.selectFirst("img[class*=photo-item__avatar]").attr("src");
user.author = element.selectFirst("span[class*=photo-item__name]").text();
photo.user = user;
images.add(photo);
Element linkEl = element.selectFirst("a[class*=js-photo-link]");
if (linkEl != null)
{
photo.deatilPage = linkEl.attr("href");
UserEntity user = new UserEntity();
user.avatar = element.selectFirst("img[class*=photo-item__avatar]").attr("src");
user.author = element.selectFirst("span[class*=photo-item__name]").text();
photo.user = user;
images.add(photo);
}
}
return images;
}
Expand Down Expand Up @@ -206,24 +213,25 @@ private ArrayList<PopularEntity> getPopluarList(int page) throws IOException

private PhotoEntity getImage(String url) throws IOException
{
Log.d(TAG, baseUrl + url);
Document doc = Jsoup.connect(baseUrl + url).get();

PhotoEntity image = new PhotoEntity();
Element picElementByImg = doc.selectFirst("picture[class*=image-section__picture]").selectFirst("img");
this.parseImageTags(image, picElementByImg);
image.rgb = this.parseRgbs(picElementByImg.attr("style"));

image.bigSrc = picElementByImg.attr("data-zoom-src");
image.downloadUrl = doc.selectFirst("a[download]").attr("href");
Element divActionElement = doc.selectFirst("div[class*=box image-section__actions]");
image.id = divActionElement.selectFirst("button").attr("data-photo-id");

Element profileboxEl = doc.selectFirst("div[class*=mini-profile box]");
Element imgElement = profileboxEl.selectFirst("img[class*=mini-profile__img]");
Element divElement = doc.selectFirst("div[class*=mini-profile]");
UserEntity user = new UserEntity();
user.author = imgElement.attr("alt");
user.avatar = imgElement.attr("src");
user.userid = profileboxEl.selectFirst("button").attr("data-user-id");
Element alinkEl = profileboxEl.selectFirst("a[class*=mini-profile__link]");
user.author = divElement.selectFirst("[class*=mini-profile__name]").text();
user.avatar = divElement.selectFirst("img[class*=mini-profile__img]").attr("src");
user.userid = divElement.selectFirst("button").attr("data-user-id");
Element alinkEl = divElement.selectFirst("a[class*=mini-profile__link]");
if (alinkEl != null)
{
user.userPage = alinkEl.attr("href");
Expand All @@ -240,6 +248,8 @@ private PhotoEntity getImage(String url) throws IOException
childImage.deatilPage = pageEl.attr("href");
this.parseImageTags(childImage, photoEl.selectFirst("img[class*=photo-item__img]"));
childImage.downloadUrl = photoEl.selectFirst("a[download]").attr("href");
String style = photoEl.selectFirst("a[class*=js-photo-link photo-item__link]").attr("style");
childImage.rgb = this.parseRgbs(style);
similarPhotos.add(childImage);
}
image.mSimilarPhotos = similarPhotos;
Expand All @@ -260,15 +270,18 @@ private void parseImageTags(PhotoEntity entity, Element imgElement)
entity.largeSrc = imgElement.attr("data-large-src");
entity.smallSrc = imgElement.attr("src");
entity.pinSrc = imgElement.attr("data-pin-media");
String widthText = imgElement.attr("width");
String widthText = imgElement.attr("data-image-width");
if (!widthText.matches("\\d+"))
widthText = "0";
widthText = "1";
entity.width = Integer.parseInt(widthText);
String heightText = imgElement.attr("height");
String heightText = imgElement.attr("data-image-height");
if (!heightText.matches("\\d+"))
heightText = "0";
heightText = "1";
entity.height = Integer.parseInt(heightText);
String style = imgElement.attr("style");
}

private int[] parseRgbs(String style)
{
String[] rgbs = style.replaceAll("[a-zA-Z():]", "").split(",");
int[] rgb = new int[rgbs.length];
for (int i = 0; i < rgbs.length; i++)
Expand All @@ -278,7 +291,7 @@ private void parseImageTags(PhotoEntity entity, Element imgElement)
text = "0";
rgb[i] = Integer.parseInt(text);
}
entity.rgb = rgb;
return rgb;
}

public ArrayList<PhotoEntity> getPeopleImages(int page, String photoUrl) throws IOException
Expand All @@ -305,25 +318,16 @@ public ArrayList<PhotoEntity> getPeopleImages(int page, String photoUrl) throws
entity.largeSrc = imgElement.attr("data-large-src");
entity.smallSrc = imgElement.attr("src");
entity.pinSrc = imgElement.attr("data-pin-media");
String widthText = imgElement.attr("width").trim();
String widthText = imgElement.attr("data-image-width").trim();
if (!widthText.matches("\\d+"))
widthText = "0";
widthText = "1";
entity.width = Integer.parseInt(widthText);
String heightText = imgElement.attr("height").trim();
String heightText = imgElement.attr("data-image-height").trim();
if (!heightText.matches("\\d+"))
heightText = "0";
heightText = "1";
entity.height = Integer.parseInt(heightText);
String style = imgElement.attr("style");
String[] rgbs = style.replaceAll("[a-zA-Z():]", "").split(",");
int[] rgb = new int[rgbs.length];
for (int i = 0; i < rgbs.length; i++)
{
String text = rgbs[i].trim();
if (!text.matches("\\d{1,3}"))
text = "0";
rgb[i] = Integer.parseInt(text);
}
entity.rgb = rgb;
String style = element.selectFirst("a[class*=js-photo-link photo-item__link]").attr("style");
entity.rgb = this.parseRgbs(style);
images.add(entity);
}
return images;
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.android.tools.build:gradle:3.2.1'


// NOTE: Do not place your application dependencies here; they belong
Expand Down
Loading

0 comments on commit 424c83a

Please sign in to comment.