美文网首页
仿照抖音的视频播放类型

仿照抖音的视频播放类型

作者: 君袅 | 来源:发表于2019-05-07 23:39 被阅读0次

直接看代码就行咯:

/**
 * ViewPagerLayoutManager
 */
public class ViewPagerLayoutManagerActivity extends AppCompatActivity {
    private static final String TAG = "ViewPagerActivity";
    private RecyclerView mRecyclerView;
    private MyAdapter mAdapter;
    private ViewPagerLayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager_layout_manager);

        initView();

        initListener();
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.recycler);

        mLayoutManager = new ViewPagerLayoutManager(this, OrientationHelper.VERTICAL);
        mAdapter = new MyAdapter();
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void initListener() {
        mLayoutManager.setOnViewPagerListener(new OnViewPagerListener() {

            @Override
            public void onInitComplete() {
                Log.e(TAG, "onInitComplete");
                playVideo(0);
            }

            @Override
            public void onPageRelease(boolean isNext, int position) {
                Log.e(TAG, "释放位置:" + position + " 下一页:" + isNext);
                int index = 0;
                if (isNext) {
                    index = 0;
                } else {
                    index = 1;
                }
                releaseVideo(index);
            }

            @Override
            public void onPageSelected(int position, boolean isBottom) {
                Log.e(TAG, "选中位置:" + position + "  是否是滑动到底部:" + isBottom);
                playVideo(0);
            }
        }
        );
    }

    private void playVideo(int position) {
        View itemView = mRecyclerView.getChildAt(0);
        final VideoView videoView = itemView.findViewById(R.id.video_view);
        final ImageView imgPlay = itemView.findViewById(R.id.img_play);
        final ImageView imgThumb = itemView.findViewById(R.id.img_thumb);
        final RelativeLayout rootView = itemView.findViewById(R.id.root_view);
        final MediaPlayer[] mediaPlayer = new MediaPlayer[1];
        videoView.start();
        videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                mediaPlayer[0] = mp;
                mp.setLooping(true);
                imgThumb.animate().alpha(0).setDuration(200).start();
                return false;
            }
        });
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {

            }
        });


        imgPlay.setOnClickListener(new View.OnClickListener() {
            boolean isPlaying = true;

            @Override
            public void onClick(View v) {
                if (videoView.isPlaying()) {
                    imgPlay.animate().alpha(1f).start();
                    videoView.pause();
                    isPlaying = false;
                } else {
                    imgPlay.animate().alpha(0f).start();
                    videoView.start();
                    isPlaying = true;
                }
            }
        });
    }

    private void releaseVideo(int index) {
        View itemView = mRecyclerView.getChildAt(index);
        final VideoView videoView = itemView.findViewById(R.id.video_view);
        final ImageView imgThumb = itemView.findViewById(R.id.img_thumb);
        final ImageView imgPlay = itemView.findViewById(R.id.img_play);
        videoView.stopPlayback();
        imgThumb.animate().alpha(1).start();
        imgPlay.animate().alpha(0f).start();
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private int[] imgs = {R.mipmap.img_video_1, R.mipmap.img_video_2,R.drawable.animation_like};
        private int[] videos = {R.raw.video_1, R.raw.video_2};



        public MyAdapter() {
        }


        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view_pager, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.img_thumb.setImageResource(imgs[position % 2]);
            holder.videoView.setVideoURI(Uri.parse("https://o6yh618n9.qnssl.com/7zAkFsjl_8682607551.mp4"));
//            holder.videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + videos[position % 2]));
        }

        @Override
        public int getItemCount() {
            return 20;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            ImageView img_thumb;
            VideoView videoView;
            ImageView img_play;
            RelativeLayout rootView;

            public ViewHolder(View itemView) {
                super(itemView);
                img_thumb = itemView.findViewById(R.id.img_thumb);
                videoView = itemView.findViewById(R.id.video_view);
                img_play = itemView.findViewById(R.id.img_play);
                rootView = itemView.findViewById(R.id.root_view);
            }
        }
    }
}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.dingmouren.example.layoutmanagergroup.activity.ViewPagerLayoutManagerActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:orientation="horizontal"
        android:layout_marginTop="56dp"
        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_centerHorizontal="true"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="推荐"
                android:textColor="@android:color/white"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_marginRight="16dp"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="附近"
                android:textColor="#f2f2f2"
                android:textSize="17sp"
                android:textStyle="bold"
                />
        </LinearLayout>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/search_icon"
            android:layout_alignParentRight="true"
            android:tint="#f2f2f2"
            android:layout_marginRight="16dp"
            />
    </RelativeLayout>


    <VideoView
        android:id="@+id/line_bottom"
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_above="@+id/bottom"
        android:background="@android:color/white"
        />
    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:textColor="@android:color/white"
            android:text="首页"
            android:textSize="18sp"
            android:textStyle="bold"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="#f2f2f2"
            android:text="关注"
            android:textSize="17sp"
            android:textStyle="bold"
            />
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
           android:src="@mipmap/add_bg"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="#f2f2f2"
            android:text="消息"
            android:textSize="17sp"
            android:textStyle="bold"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="#f2f2f2"
            android:text="我"
            android:textSize="17sp"
            android:textStyle="bold"
            />
    </LinearLayout>
</RelativeLayout>

item引用的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.dingmouren.example.layoutmanagergroup.widget.FullScreenVideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="false"
        android:focusable="false"
        />
    <ImageView
        android:id="@+id/img_thumb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:clickable="false"
        android:focusable="false"
        android:src="@drawable/ic_launcher_background"
        android:visibility="visible"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:gravity="center_horizontal"
        >
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <de.hdodenhof.circleimageview.CircleImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:src="@mipmap/header_icon_2"
                app:civ_border_color="@android:color/white"
                app:civ_border_width="2dp" />
            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:background="@drawable/circle_big_red"
                android:src="@mipmap/add_icon"
                android:tint="@android:color/white"
                android:scaleType="centerInside"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="40dp"
                />
        </RelativeLayout>

        <TextView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:drawableTop="@mipmap/heart_icon"
            android:layout_marginTop="16dp"
            android:text="1.6w"
            android:textColor="@android:color/white"
            android:gravity="center"
            />
        <TextView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:drawableTop="@mipmap/msg_icon"
            android:layout_marginTop="16dp"
            android:text="1.6w"
            android:textColor="@android:color/white"
            android:gravity="center"
            />
        <TextView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:drawableTop="@mipmap/share_icon"
            android:layout_marginTop="16dp"
            android:text="1.6w"
            android:textColor="@android:color/white"
            android:gravity="center"
            />
    </LinearLayout>
    <ImageView
        android:id="@+id/img_play"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/play_arrow"
        android:clickable="true"
        android:focusable="true"
        android:alpha="0"
        android:layout_centerInParent="true"
        />
</RelativeLayout>

//自定义 LinearLayoutManager

 
public class ViewPagerLayoutManager extends LinearLayoutManager {
    private static final String TAG = "ViewPagerLayoutManager";
    private PagerSnapHelper mPagerSnapHelper;
    private OnViewPagerListener mOnViewPagerListener;
    private RecyclerView mRecyclerView;
    private int mDrift;//位移,用来判断移动方向

    public ViewPagerLayoutManager(Context context, int orientation) {
        super(context, orientation, false);
        init();
    }

    public ViewPagerLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
        init();
    }

    private void init() {
        mPagerSnapHelper = new PagerSnapHelper();

    }


    @Override
    public void onAttachedToWindow(RecyclerView view) {
        super.onAttachedToWindow(view);
        mPagerSnapHelper.attachToRecyclerView(view);
        this.mRecyclerView = view;
        mRecyclerView.addOnChildAttachStateChangeListener(mChildAttachStateChangeListener);
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        super.onLayoutChildren(recycler, state);
//
    }

    /**
     * 滑动状态的改变
     * 缓慢拖拽-> 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 = getPosition(viewIdle);
                if (mOnViewPagerListener != null && getChildCount() == 1) {
                    mOnViewPagerListener.onPageSelected(positionIdle,positionIdle == getItemCount() - 1);
                }
                break;
            case RecyclerView.SCROLL_STATE_DRAGGING:
                View viewDrag = mPagerSnapHelper.findSnapView(this);
                int positionDrag = getPosition(viewDrag);
                break;
            case RecyclerView.SCROLL_STATE_SETTLING:
                View viewSettling = mPagerSnapHelper.findSnapView(this);
                int positionSettling = getPosition(viewSettling);
                break;

        }
    }



    /**
     * 监听竖直方向的相对偏移量
     * @param dy
     * @param recycler
     * @param state
     * @return
     */
    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        this.mDrift = dy;
        return super.scrollVerticallyBy(dy, recycler, state);
    }


    /**
     * 监听水平方向的相对偏移量
     * @param dx
     * @param recycler
     * @param state
     * @return
     */
    @Override
    public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
        this.mDrift = dx;
        return super.scrollHorizontallyBy(dx, recycler, state);
    }

    /**
     * 设置监听
     * @param listener
     */
    public void setOnViewPagerListener(OnViewPagerListener listener){
        this.mOnViewPagerListener = listener;
    }

    private RecyclerView.OnChildAttachStateChangeListener mChildAttachStateChangeListener = new RecyclerView.OnChildAttachStateChangeListener() {
        @Override
        public void onChildViewAttachedToWindow(View view) {
            if (mOnViewPagerListener != null && getChildCount() == 1) {
                mOnViewPagerListener.onInitComplete();
            }
        }

        @Override
        public void onChildViewDetachedFromWindow(View view) {
            if (mDrift >= 0){
                if (mOnViewPagerListener != null) mOnViewPagerListener.onPageRelease(true,getPosition(view));
            }else {
                if (mOnViewPagerListener != null) mOnViewPagerListener.onPageRelease(false,getPosition(view));
            }

        }
    };
}

public interface OnViewPagerListener {

    /*初始化完成*/
    void onInitComplete();

    /*释放的监听*/
    void onPageRelease(boolean isNext,int position);

    /*选中的监听以及判断是否滑动到底部*/
    void onPageSelected(int position,boolean isBottom);


}

相关文章

  • 仿照抖音的视频播放类型

    直接看代码就行咯: 布局 item引用的xml //自定义 LinearLayoutManager

  • 抖音引流方法:令爆粉效率提升一倍的抖音引流话术

    【抖音引流方法,抖音引流话术,抖音爆粉】 随着抖音长视频权限的开放,单个短视频的播放时间也是越来越长,15秒短视频...

  • 怎么利用抖音賺钱?

    怎么运营好你的抖音账号? 一、抖音号的注册和养号 1.做抖音的目的是提高视频的播放量 我们把提高视频的播放量分为:...

  • iOS仿抖音—评论视图滑动消失

    iOS仿抖音短视频 iOS仿抖音—左右滑动切换效果iOS仿抖音—上下滑动播放视频iOS仿抖音—评论视图滑动消失 前...

  • iOS仿抖音—播放视图滑动隐藏

    iOS仿抖音短视频 iOS仿抖音—左右滑动切换效果iOS仿抖音—上下滑动播放视频iOS仿抖音—评论视图滑动消失iO...

  • iOS仿抖音—加载点赞动画效果

    iOS仿抖音短视频 iOS仿抖音—左右滑动切换效果iOS仿抖音—上下滑动播放视频iOS仿抖音—评论视图滑动消失iO...

  • 抖D音播放量为什么上不去

    抖D音播放量为什么上不去 抖音播放量很少一直都是广大抖音玩家的痛点,都想自己的视频能上热门,那么到底多少播放量才能...

  • 抖音怎么提高播放量

    抖音播放量特别低怎么办?很多小伙伴在抖音上发布了自己的短视频作品之后,发现自己抖音的播放量超级的低,那么抖音怎么提...

  • 抖音的赞有什么用

    抖音点赞的多可以上热门吸粉,抖音数以万计的视频,如何从中挑选优秀的视频呢,那就是评断视频的播放量,如这个视频播放了...

  • 抖音破播放是什么?

    抖音破播放,最近运营圈传得很玄的一个概念。 我们知道,一般的抖音账号,发一个平庸的视频,抖音只会给500左右的播放...

网友评论

      本文标题:仿照抖音的视频播放类型

      本文链接:https://www.haomeiwen.com/subject/poauoqtx.html