Skip to content

Commit

Permalink
修复全屏模式滑动问题
Browse files Browse the repository at this point in the history
  • Loading branch information
laishujie committed Jun 22, 2018
1 parent 81c5e04 commit 0c75e79
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 14 deletions.
74 changes: 74 additions & 0 deletions app/src/main/java/com/lai/mtc/comm/widget/MyPagerSnapHelper.java
Original file line number Diff line number Diff line change
@@ -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));
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@
* @time 2018/2/6 18:05
* @describe 预加载的LayoutManager
* getExtraLayoutSpace(),增加不可见View的缓存的空间
*
* <p>
* 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;
}
Expand All @@ -38,4 +43,5 @@ protected int getExtraLayoutSpace(RecyclerView.State state) {
}
return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -32,8 +34,6 @@

public class PreviewAdapter extends CommonAdapter<ComicPreView.PagesBean> {

//记录上一次的位置。防止再次计算
private int lastPosition;
//预览模式
private int module;

Expand Down Expand Up @@ -73,30 +73,37 @@ protected void convert(BaseViewHolder baseViewHolder, ComicPreView.PagesBean pag
.placeholder(new ColorDrawable(Color.BLACK))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.transition(new GenericTransitionOptions<Bitmap>());
//避免重新计算
if (module == 0 && lastPosition <= position) {
lastPosition = position;
if (module == 0) {
RequestListener<Bitmap> requestListener = new RequestListener<Bitmap>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}

@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
public boolean onResourceReady(Bitmap bitmap, Object model, Target<Bitmap> 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;
}
};
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/lai/mtc/mvp/utlis/ScreenUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*
Expand Down

0 comments on commit 0c75e79

Please sign in to comment.