From 0c75e79ea4360ef32223e2e173daa378442502f7 Mon Sep 17 00:00:00 2001 From: laishujie Date: Fri, 22 Jun 2018 18:58:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=85=A8=E5=B1=8F=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E6=BB=91=E5=8A=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mtc/comm/widget/MyPagerSnapHelper.java | 74 +++++++++++++++++++ .../comm/widget/PreCacheLayoutManager.java | 10 ++- .../comics/activity/ComicPreviewActivity.java | 7 +- .../mvp/ui/comics/adapter/PreviewAdapter.java | 25 ++++--- .../com/lai/mtc/mvp/utlis/ScreenUtils.java | 22 ++++++ 5 files changed, 124 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/com/lai/mtc/comm/widget/MyPagerSnapHelper.java diff --git a/app/src/main/java/com/lai/mtc/comm/widget/MyPagerSnapHelper.java b/app/src/main/java/com/lai/mtc/comm/widget/MyPagerSnapHelper.java new file mode 100644 index 0000000..5908633 --- /dev/null +++ b/app/src/main/java/com/lai/mtc/comm/widget/MyPagerSnapHelper.java @@ -0,0 +1,74 @@ +package com.lai.mtc.comm.widget; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.LinearSmoothScroller; +import android.support.v7.widget.OrientationHelper; +import android.support.v7.widget.PagerSnapHelper; +import android.support.v7.widget.RecyclerView; +import android.util.DisplayMetrics; +import android.view.View; + +public class MyPagerSnapHelper extends PagerSnapHelper { + + private RecyclerView mRecyclerView; + private static final float MILLISECONDS_PER_INCH = 100f; + private static final int MAX_SCROLL_ON_FLING_DURATION = 100; // ms + + private int mExtraSpace = 0; + + public MyPagerSnapHelper(int extraSpace) { + mExtraSpace = extraSpace; + } + + @Override + public void attachToRecyclerView(@Nullable RecyclerView recyclerView) throws IllegalStateException { + super.attachToRecyclerView(recyclerView); + this.mRecyclerView = recyclerView; + } + + @Override + protected LinearSmoothScroller createSnapScroller(RecyclerView.LayoutManager layoutManager) { + if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) { + return null; + } + return new LinearSmoothScroller(mRecyclerView.getContext()) { + @Override + protected void onTargetFound(View targetView, RecyclerView.State state, RecyclerView.SmoothScroller.Action action) { + int[] snapDistances = calculateDistanceToFinalSnap(mRecyclerView.getLayoutManager(), + targetView); + int dx = snapDistances[0]; + final int dy = snapDistances[1]; + if (dx < 0) { + dx += mExtraSpace * mRecyclerView.getWidth(); + if (dx > 0) { + LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager(); + int firstItemPosition = linearLayoutManager.findFirstVisibleItemPosition(); + if (firstItemPosition == 1) { + dx = snapDistances[0] + mRecyclerView.getWidth(); + } else { + dx = -dx; + } + } + } + + final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy))); + if (time > 0) { + action.update(dx, dy, time, mDecelerateInterpolator); + } + } + + @Override + protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { + return MILLISECONDS_PER_INCH / displayMetrics.densityDpi; + } + + @Override + protected int calculateTimeForScrolling(int dx) { + return Math.min(MAX_SCROLL_ON_FLING_DURATION, super.calculateTimeForScrolling(dx)); + } + }; + } + +} diff --git a/app/src/main/java/com/lai/mtc/comm/widget/PreCacheLayoutManager.java b/app/src/main/java/com/lai/mtc/comm/widget/PreCacheLayoutManager.java index 4384a98..b025899 100644 --- a/app/src/main/java/com/lai/mtc/comm/widget/PreCacheLayoutManager.java +++ b/app/src/main/java/com/lai/mtc/comm/widget/PreCacheLayoutManager.java @@ -9,19 +9,24 @@ * @time 2018/2/6 18:05 * @describe 预加载的LayoutManager * getExtraLayoutSpace(),增加不可见View的缓存的空间 - * + *

* http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0209/2452.html - * */ public class PreCacheLayoutManager extends LinearLayoutManager { + public int getExtraSpace() { + return mExtraSpace; + } + private int mExtraSpace = 0; + public PreCacheLayoutManager(Context context) { super(context); } + public void setExtraSpace(int extraSpace) { mExtraSpace = extraSpace; } @@ -38,4 +43,5 @@ protected int getExtraLayoutSpace(RecyclerView.State state) { } return 0; } + } diff --git a/app/src/main/java/com/lai/mtc/mvp/ui/comics/activity/ComicPreviewActivity.java b/app/src/main/java/com/lai/mtc/mvp/ui/comics/activity/ComicPreviewActivity.java index f05630e..0f3cc11 100644 --- a/app/src/main/java/com/lai/mtc/mvp/ui/comics/activity/ComicPreviewActivity.java +++ b/app/src/main/java/com/lai/mtc/mvp/ui/comics/activity/ComicPreviewActivity.java @@ -17,6 +17,7 @@ import com.lai.mtc.R; import com.lai.mtc.bean.ComicListDetail; import com.lai.mtc.bean.ComicPreView; +import com.lai.mtc.comm.widget.MyPagerSnapHelper; import com.lai.mtc.comm.widget.PreCacheLayoutManager; import com.lai.mtc.comm.widget.TouchRecyclerView; import com.lai.mtc.dao.RecordDao; @@ -159,7 +160,7 @@ public void init(Bundle savedInstanceState) { mComicListDetail = (ComicListDetail) getIntent().getSerializableExtra("comicListDetail"); index = getIntent().getIntExtra("index", 1); hideLayout(); - if(mComicListDetail!=null){ + if (mComicListDetail != null) { setToolBar(mToolbar, mComicListDetail.getName(), true); showInfo(mComicListDetail); request(index); @@ -398,7 +399,7 @@ private void initModule(int module) { } else { linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRvList.setLayoutManager(linearLayoutManager); - mPagerSnapHelper = new PagerSnapHelper(); + mPagerSnapHelper = new MyPagerSnapHelper(linearLayoutManager.getExtraSpace()); mPagerSnapHelper.attachToRecyclerView(mRvList); } mPreviewAdapter = null; @@ -452,7 +453,7 @@ public void onBackPressed() { @Override public void finish() { //更新当前数据库记录 - if(mComicListDetail!=null&&firstVisibleItem!=null){ + if (mComicListDetail != null && firstVisibleItem != null) { mRecordDao.updateRecord( mComicListDetail.getId(), firstVisibleItem.getName() diff --git a/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/PreviewAdapter.java b/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/PreviewAdapter.java index cbae51e..453ffde 100644 --- a/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/PreviewAdapter.java +++ b/app/src/main/java/com/lai/mtc/mvp/ui/comics/adapter/PreviewAdapter.java @@ -7,6 +7,7 @@ import android.util.SparseArray; import android.view.ViewGroup; import android.widget.ImageView; +import android.widget.RelativeLayout; import com.bumptech.glide.GenericTransitionOptions; import com.bumptech.glide.load.DataSource; @@ -19,6 +20,7 @@ import com.lai.mtc.bean.ComicPreView; import com.lai.mtc.comm.CommonAdapter; import com.lai.mtc.mvp.utlis.SPUtils; +import com.lai.mtc.mvp.utlis.ScreenUtils; import com.lai.mtc.mvp.utlis.glide.GlideApp; import com.lai.mtc.mvp.utlis.glide.GlideRequest; @@ -32,8 +34,6 @@ public class PreviewAdapter extends CommonAdapter { - //记录上一次的位置。防止再次计算 - private int lastPosition; //预览模式 private int module; @@ -73,9 +73,7 @@ protected void convert(BaseViewHolder baseViewHolder, ComicPreView.PagesBean pag .placeholder(new ColorDrawable(Color.BLACK)) .diskCacheStrategy(DiskCacheStrategy.ALL) .transition(new GenericTransitionOptions()); - //避免重新计算 - if (module == 0 && lastPosition <= position) { - lastPosition = position; + if (module == 0) { RequestListener requestListener = new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -83,20 +81,29 @@ public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + public boolean onResourceReady(Bitmap bitmap, Object model, Target target, DataSource dataSource, boolean isFirstResource) { if (imageView == null) { return false; } - //FIXME: 固定宽。高度的比例自适应进行缩放.可能照成OOM.后期得想办法 + if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) { imageView.setScaleType(ImageView.ScaleType.FIT_XY); } - ViewGroup.LayoutParams params = imageView.getLayoutParams(); + /* ViewGroup.LayoutParams params = imageView.getLayoutParams(); int vw = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight(); float scale = (float) vw / (float) resource.getWidth(); int vh = Math.round(resource.getHeight() * scale); params.height = vh + imageView.getPaddingTop() + imageView.getPaddingBottom(); - imageView.setLayoutParams(params); + imageView.setLayoutParams(params);*/ + + int width = bitmap.getWidth(); + int height = bitmap.getHeight(); + float scale = ((float) height) / width; + ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams(); + layoutParams.width = ScreenUtils.getScreenWidth(mContext); + layoutParams.height = (int) (scale * ScreenUtils.getScreenWidth(mContext)); + imageView.setLayoutParams(layoutParams); + return false; } }; diff --git a/app/src/main/java/com/lai/mtc/mvp/utlis/ScreenUtils.java b/app/src/main/java/com/lai/mtc/mvp/utlis/ScreenUtils.java index 9e0c745..2c6ff7a 100644 --- a/app/src/main/java/com/lai/mtc/mvp/utlis/ScreenUtils.java +++ b/app/src/main/java/com/lai/mtc/mvp/utlis/ScreenUtils.java @@ -44,6 +44,28 @@ public static int getScreenWidth() { return point.x; } + /** + * 获得屏幕高度 + * + * @param context + * @return + */ + public static int getScreenHeight(Context context) + { + WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + DisplayMetrics outMetrics = new DisplayMetrics(); + wm.getDefaultDisplay().getMetrics(outMetrics); + return outMetrics.heightPixels; + } + + public static int getScreenWidth(Context context) + { + WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + DisplayMetrics outMetrics = new DisplayMetrics(); + wm.getDefaultDisplay().getMetrics(outMetrics); + return outMetrics.widthPixels; + } + /** * 获取屏幕的高度(单位:px) *