标签:attr smooth 简单的 callbacks space 位移 设置 between 播放
/**
* <pre>
* @author 杨充
* blog : https://github.com/yangchong211
* time : 2019/6/20
* desc : 自定义ViewPager,主要是处理边界极端情况
* revise:
* </pre>
*/
public class VerticalViewPager extends ViewPager {
private boolean isVertical = false;
private long mRecentTouchTime;
public VerticalViewPager(@NonNull Context context) {
super(context);
}
public VerticalViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
private void init() {
setPageTransformer(true, new HorizontalVerticalPageTransformer());
setOverScrollMode(OVER_SCROLL_NEVER);
}
public boolean isVertical() {
return isVertical;
}
public void setVertical(boolean vertical) {
isVertical = vertical;
init();
}
private class HorizontalVerticalPageTransformer implements PageTransformer {
private static final float MIN_SCALE = 0.25f;
@Override
public void transformPage(@NonNull View page, float position) {
if (isVertical) {
if (position < -1) {
page.setAlpha(0);
} else if (position <= 1) {
page.setAlpha(1);
// Counteract the default slide transition
float xPosition = page.getWidth() * -position;
page.setTranslationX(xPosition);
//set Y position to swipe in from top
float yPosition = position * page.getHeight();
page.setTranslationY(yPosition);
} else {
page.setAlpha(0);
}
} else {
int pageWidth = page.getWidth();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
page.setAlpha(0);
} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
page.setAlpha(1);
page.setTranslationX(0);
page.setScaleX(1);
page.setScaleY(1);
} else if (position <= 1) { // (0,1]
// Fade the page out.
page.setAlpha(1 - position);
// Counteract the default slide transition
page.setTranslationX(pageWidth * -position);
page.setTranslationY(0);
// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
page.setScaleX(scaleFactor);
page.setScaleY(scaleFactor);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
page.setAlpha(0);
}
}
}
}
/**
* 交换x轴和y轴的移动距离
* @param event 获取事件类型的封装类MotionEvent
*/
private MotionEvent swapXY(MotionEvent event) {
//获取宽高
float width = getWidth();
float height = getHeight();
//将Y轴的移动距离转变成X轴的移动距离
float swappedX = (event.getY() / height) * width;
//将X轴的移动距离转变成Y轴的移动距离
float swappedY = (event.getX() / width) * height;
//重设event的位置
event.setLocation(swappedX, swappedY);
return event;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
mRecentTouchTime = System.currentTimeMillis();
if (getCurrentItem() == 0 && getChildCount() == 0) {
return false;
}
if (isVertical) {
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
swapXY(ev);
// return touch coordinates to original reference frame for any child views
return intercepted;
} else {
return super.onInterceptTouchEvent(ev);
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (getCurrentItem() == 0 && getChildCount() == 0) {
return false;
}
if (isVertical) {
return super.onTouchEvent(swapXY(ev));
} else {
return super.onTouchEvent(ev);
}
}
}
private void initViewPager() {
List<Video> list = new ArrayList<>();
ArrayList<Fragment> fragments = new ArrayList<>();
for (int a = 0; a< DataProvider.VideoPlayerList.length ; a++){
Video video = new Video(DataProvider.VideoPlayerTitle[a],
10,"",DataProvider.VideoPlayerList[a]);
list.add(video);
fragments.add(VideoFragment.newInstant(DataProvider.VideoPlayerList[a]));
}
vp.setOffscreenPageLimit(1);
vp.setCurrentItem(0);
vp.setOrientation(DirectionalViewPager.VERTICAL);
FragmentManager supportFragmentManager = getSupportFragmentManager();
MyPagerAdapter myPagerAdapter = new MyPagerAdapter(fragments, supportFragmentManager);
vp.setAdapter(myPagerAdapter);
}
class MyPagerAdapter extends FragmentStatePagerAdapter{
private ArrayList<Fragment> list;
public MyPagerAdapter(ArrayList<Fragment> list , FragmentManager fm){
super(fm);
this.list = list;
}
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list!=null ? list.size() : 0;
}
}
public class VideoFragment extends Fragment{
public VideoPlayer videoPlayer;
private String url;
private int index;
@Override
public void onStop() {
super.onStop();
VideoPlayerManager.instance().releaseVideoPlayer();
}
public static Fragment newInstant(String url){
VideoFragment videoFragment = new VideoFragment();
Bundle bundle = new Bundle();
bundle.putString("url",url);
videoFragment.setArguments(bundle);
return videoFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle arguments = getArguments();
if (arguments != null) {
url = arguments.getString("url");
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_video, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
videoPlayer = view.findViewById(R.id.video_player);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d("初始化操作","------"+index++);
VideoPlayerController controller = new VideoPlayerController(getActivity());
videoPlayer.setUp(url,null);
videoPlayer.setPlayerType(ConstantKeys.IjkPlayerType.TYPE_IJK);
videoPlayer.setController(controller);
ImageUtils.loadImgByPicasso(getActivity(),"",
R.drawable.image_default,controller.imageView());
}
}
private int determineTargetPage(int currentPage, float pageOffset, int velocity, int deltaX) {
int targetPage;
if (Math.abs(deltaX) > this.mFlingDistance && Math.abs(velocity) > this.mMinimumVelocity) {
targetPage = velocity > 0 ? currentPage : currentPage + 1;
} else {
float truncator = currentPage >= this.mCurItem ? 0.4F : 0.6F;
targetPage = currentPage + (int)(pageOffset + truncator);
}
if (this.mItems.size() > 0) {
ViewPager.ItemInfo firstItem = (ViewPager.ItemInfo)this.mItems.get(0);
ViewPager.ItemInfo lastItem = (ViewPager.ItemInfo)this.mItems.get(this.mItems.size() - 1);
targetPage = Math.max(firstItem.position, Math.min(targetPage, lastItem.position));
}
return targetPage;
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setAnimationDuration(final int during){
try {
// viewPager平移动画事件
Field mField = ViewPager.class.getDeclaredField("mScroller");
mField.setAccessible(true);
// 动画效果与ViewPager的一致
Interpolator interpolator = new Interpolator() {
@Override
public float getInterpolation(float t) {
t -= 1.0f;
return t * t * t * t * t + 1.0f;
}
};
Scroller mScroller = new Scroller(getContext(),interpolator){
final int time = 2000;
@Override
public void startScroll(int x, int y, int dx, int dy, int duration) {
// 如果手工滚动,则加速滚动
if (System.currentTimeMillis() - mRecentTouchTime > time) {
duration = during;
} else {
duration /= 2;
}
super.startScroll(x, y, dx, dy, duration);
}
@Override
public void startScroll(int x, int y, int dx, int dy) {
super.startScroll(x, y, dx, dy,during);
}
};
mField.set(this, mScroller);
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
e.printStackTrace();
}
}
/**
* 该方法必须调用
* @param recyclerView recyclerView
*/
@Override
public void onAttachedToWindow(RecyclerView recyclerView) {
if (recyclerView == null) {
throw new IllegalArgumentException("The attach RecycleView must not null!!");
}
super.onAttachedToWindow(recyclerView);
this.mRecyclerView = recyclerView;
if (mPagerSnapHelper==null){
init();
}
mPagerSnapHelper.attachToRecyclerView(mRecyclerView);
mRecyclerView.addOnChildAttachStateChangeListener(mChildAttachStateChangeListener);
}
public interface OnPagerListener {
/**
* 初始化完成
*/
void onInitComplete();
/**
* 释放的监听
* @param isNext 是否下一个
* @param position 索引
*/
void onPageRelease(boolean isNext,int position);
/***
* 选中的监听以及判断是否滑动到底部
* @param position 索引
* @param isBottom 是否到了底部
*/
void onPageSelected(int position,boolean isBottom);
}
/**
* 滑动状态的改变
* 缓慢拖拽-> SCROLL_STATE_DRAGGING
* 快速滚动-> SCROLL_STATE_SETTLING
* 空闲状态-> SCROLL_STATE_IDLE
* @param state 状态
*/
@Override
public void onScrollStateChanged(int state) {
switch (state) {
case RecyclerView.SCROLL_STATE_IDLE:
View viewIdle = mPagerSnapHelper.findSnapView(this);
int positionIdle = 0;
if (viewIdle != null) {
positionIdle = getPosition(viewIdle);
}
if (mOnViewPagerListener != null && getChildCount() == 1) {
mOnViewPagerListener.onPageSelected(positionIdle,
positionIdle == getItemCount() - 1);
}
break;
case RecyclerView.SCROLL_STATE_DRAGGING:
View viewDrag = mPagerSnapHelper.findSnapView(this);
if (viewDrag != null) {
int positionDrag = getPosition(viewDrag);
}
break;
case RecyclerView.SCROLL_STATE_SETTLING:
View viewSettling = mPagerSnapHelper.findSnapView(this);
if (viewSettling != null) {
int positionSettling = getPosition(viewSettling);
}
break;
default:
break;
}
}
/**
* 监听竖直方向的相对偏移量
* @param dy y轴滚动值
* @param recycler recycler
* @param state state滚动状态
* @return int值
*/
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
if (getChildCount() == 0 || dy == 0) {
return 0;
}
this.mDrift = dy;
return super.scrollVerticallyBy(dy, recycler, state);
}
/**
* 监听水平方向的相对偏移量
* @param dx x轴滚动值
* @param recycler recycler
* @param state state滚动状态
* @return int值
*/
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
if (getChildCount() == 0 || dx == 0) {
return 0;
}
this.mDrift = dx;
return super.scrollHorizontallyBy(dx, recycler, state);
}
private RecyclerView.OnChildAttachStateChangeListener mChildAttachStateChangeListener =
new RecyclerView.OnChildAttachStateChangeListener() {
/**
* 第一次进入界面的监听,可以做初始化方面的操作
* @param view view
*/
@Override
public void onChildViewAttachedToWindow(@NonNull View view) {
if (mOnViewPagerListener != null && getChildCount() == 1) {
mOnViewPagerListener.onInitComplete();
}
}
/**
* 页面销毁的时候调用该方法,可以做销毁方面的操作
* @param view view
*/
@Override
public void onChildViewDetachedFromWindow(@NonNull View view) {
if (mDrift >= 0){
if (mOnViewPagerListener != null) {
mOnViewPagerListener.onPageRelease(true , getPosition(view));
}
}else {
if (mOnViewPagerListener != null) {
mOnViewPagerListener.onPageRelease(false , getPosition(view));
}
}
}
};
/**
* attach到window窗口时,该方法必须调用
* @param recyclerView recyclerView
*/
@Override
public void onAttachedToWindow(RecyclerView recyclerView) {
//这里省略部分代码
mRecyclerView.addOnChildAttachStateChangeListener(mChildAttachStateChangeListener);
}
/**
* 销毁的时候调用该方法,需要移除监听事件
* @param view view
* @param recycler recycler
*/
@Override
public void onDetachedFromWindow(RecyclerView view, RecyclerView.Recycler recycler) {
super.onDetachedFromWindow(view, recycler);
if (mRecyclerView!=null){
mRecyclerView.removeOnChildAttachStateChangeListener(mChildAttachStateChangeListener);
}
}
/**
* 修改滑动灵敏度
* @param flingDistance 滑动惯性,默认是75
* @param minimumVelocity 最小滑动值,默认是1200
*/
public void setScrollFling(int flingDistance , int minimumVelocity){
try {
Field mFlingDistance = ViewPager.class.getDeclaredField("mFlingDistance");
mFlingDistance.setAccessible(true);
Object o = mFlingDistance.get(this);
Log.d("setScrollFling",o.toString());
//默认值75
mFlingDistance.set(this, flingDistance);
Field mMinimumVelocity = ViewPager.class.getDeclaredField("mMinimumVelocity");
mMinimumVelocity.setAccessible(true);
Object o1 = mMinimumVelocity.get(this);
Log.d("setScrollFling",o1.toString());
//默认值1200
mMinimumVelocity.set(this,minimumVelocity);
} catch (Exception e){
e.printStackTrace();
}
}
try {
//attachToRecyclerView源码上的方法可能会抛出IllegalStateException异常,这里手动捕获一下
RecyclerView.OnFlingListener onFlingListener = mRecyclerView.getOnFlingListener();
//源码中判断了,如果onFlingListener已经存在的话,再次设置就直接抛出异常,那么这里可以判断一下
if (onFlingListener==null){
mPagerSnapHelper.attachToRecyclerView(mRecyclerView);
}
} catch (IllegalStateException e){
e.printStackTrace();
}
@Override
protected void onStop() {
super.onStop();
//从前台切到后台,当视频正在播放或者正在缓冲时,调用该方法暂停视频
VideoPlayerManager.instance().suspendVideoPlayer();
}
@Override
protected void onDestroy() {
super.onDestroy();
//销毁页面,释放,内部的播放器被释放掉,同时如果在全屏、小窗口模式下都会退出
VideoPlayerManager.instance().releaseVideoPlayer();
}
@Override
public void onBackPressed() {
//处理返回键逻辑;如果是全屏,则退出全屏;如果是小窗口,则退出小窗口
if (VideoPlayerManager.instance().onBackPressed()){
return;
}else {
//销毁页面
VideoPlayerManager.instance().releaseVideoPlayer();
}
super.onBackPressed();
}
@Override
protected void onRestart() {
super.onRestart();
//从后台切换到前台,当视频暂停时或者缓冲暂停时,调用该方法重新开启视频播放
VideoPlayerManager.instance().resumeVideoPlayer();
}
标签:attr smooth 简单的 callbacks space 位移 设置 between 播放
原文地址:https://www.cnblogs.com/yc211/p/11479278.html