From 424c83af778a649ee3a4e54c67a0144d03a1b7f4 Mon Sep 17 00:00:00 2001 From: Hiboboo Date: Mon, 18 Feb 2019 12:55:50 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E8=B0=83=E6=95=B4=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E7=AD=96=E7=95=A5=EF=BC=8C=E4=BF=AE=E5=A4=8D=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E6=B5=8F=E8=A7=88=E5=9B=BE=E7=89=87=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../actions/home/PeoplePictureActivity.java | 33 ++++---- .../actions/home/PictureHomeFragment.java | 2 + .../pictures/adapter/PictureAdapter.java | 40 +++------- .../com/bobby/pictures/util/ExecuteApi.java | 78 ++++++++++--------- build.gradle | 2 +- gradlew | 72 ++++++++++------- gradlew.bat | 14 +--- 7 files changed, 117 insertions(+), 124 deletions(-) diff --git a/app/src/main/java/com/bobby/pictures/actions/home/PeoplePictureActivity.java b/app/src/main/java/com/bobby/pictures/actions/home/PeoplePictureActivity.java index 2a83168..097e291 100644 --- a/app/src/main/java/com/bobby/pictures/actions/home/PeoplePictureActivity.java +++ b/app/src/main/java/com/bobby/pictures/actions/home/PeoplePictureActivity.java @@ -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; diff --git a/app/src/main/java/com/bobby/pictures/actions/home/PictureHomeFragment.java b/app/src/main/java/com/bobby/pictures/actions/home/PictureHomeFragment.java index 1a60512..d4e3ff6 100644 --- a/app/src/main/java/com/bobby/pictures/actions/home/PictureHomeFragment.java +++ b/app/src/main/java/com/bobby/pictures/actions/home/PictureHomeFragment.java @@ -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; @@ -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()); diff --git a/app/src/main/java/com/bobby/pictures/adapter/PictureAdapter.java b/app/src/main/java/com/bobby/pictures/adapter/PictureAdapter.java index 51d49ea..599aa2e 100644 --- a/app/src/main/java/com/bobby/pictures/adapter/PictureAdapter.java +++ b/app/src/main/java/com/bobby/pictures/adapter/PictureAdapter.java @@ -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; @@ -25,6 +25,8 @@ */ public class PictureAdapter extends BaseQuickAdapter { + private final String TAG = PictureAdapter.class.getSimpleName(); + public PictureAdapter(@Nullable List data) { super(R.layout.fragment_picture_world_item, data); @@ -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); } - - /** - * 计算一张图片在屏幕中所占的位置宽度 - *

- * 这首先取决于图片本身的宽度,根据图片宽度占屏幕宽度比例(difference),算出三种结果: - *

    - *
  • difference < 0.4 ? screenWidth * 0.3
  • - *
  • difference < 0.75 ? screenWidth * 0.485
  • - *
  • screenWidth * 0.98
  • - *
- * - * @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); - } } \ No newline at end of file diff --git a/app/src/main/java/com/bobby/pictures/util/ExecuteApi.java b/app/src/main/java/com/bobby/pictures/util/ExecuteApi.java index 362994d..1f4df4d 100644 --- a/app/src/main/java/com/bobby/pictures/util/ExecuteApi.java +++ b/app/src/main/java/com/bobby/pictures/util/ExecuteApi.java @@ -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; @@ -23,6 +24,8 @@ */ public final class ExecuteApi { + private final String TAG = ExecuteApi.class.getSimpleName(); + public enum Apis { HOME_LIST(0xff15), @@ -144,13 +147,13 @@ private ArrayList 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(","); @@ -163,12 +166,16 @@ private ArrayList 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; } @@ -206,24 +213,25 @@ private ArrayList 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"); @@ -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; @@ -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++) @@ -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 getPeopleImages(int page, String photoUrl) throws IOException @@ -305,25 +318,16 @@ public ArrayList 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; diff --git a/build.gradle b/build.gradle index b97185d..c935b4f 100644 --- a/build.gradle +++ b/build.gradle @@ -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 diff --git a/gradlew b/gradlew index 9d82f78..cccdd3d 100644 --- a/gradlew +++ b/gradlew @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh ############################################################################## ## @@ -6,20 +6,38 @@ ## ############################################################################## -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" -warn ( ) { +warn () { echo "$*" } -die ( ) { +die () { echo echo "$*" echo @@ -30,6 +48,7 @@ die ( ) { cygwin=false msys=false darwin=false +nonstop=false case "`uname`" in CYGWIN* ) cygwin=true @@ -40,26 +59,11 @@ case "`uname`" in MINGW* ) msys=true ;; + NONSTOP* ) + nonstop=true + ;; esac -# Attempt to set APP_HOME -# Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi -done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null - CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar # Determine the Java command to use to start the JVM. @@ -85,7 +89,7 @@ location of your Java installation." fi # Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then MAX_FD_LIMIT=`ulimit -H -n` if [ $? -eq 0 ] ; then if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then @@ -150,11 +154,19 @@ if $cygwin ; then esac fi -# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules -function splitJvmOpts() { - JVM_OPTS=("$@") +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " } -eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS -JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi -exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat index 8a0b282..f955316 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -8,14 +8,14 @@ @rem Set local scope for the variables with windows NT shell if "%OS%"=="Windows_NT" setlocal -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - set DIRNAME=%~dp0 if "%DIRNAME%" == "" set DIRNAME=. set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome @@ -46,10 +46,9 @@ echo location of your Java installation. goto fail :init -@rem Get command-line arguments, handling Windowz variants +@rem Get command-line arguments, handling Windows variants if not "%OS%" == "Windows_NT" goto win9xME_args -if "%@eval[2+2]" == "4" goto 4NT_args :win9xME_args @rem Slurp the command line arguments. @@ -60,11 +59,6 @@ set _SKIP=2 if "x%~1" == "x" goto execute set CMD_LINE_ARGS=%* -goto execute - -:4NT_args -@rem Get arguments from the 4NT Shell from JP Software -set CMD_LINE_ARGS=%$ :execute @rem Setup the command line