Android實現短視頻畫心效果

本文實例為大傢分享瞭Android實現短視頻畫心效果的具體代碼,供大傢參考,具體內容如下

佈局

主佈局

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/ree"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>
    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></SurfaceView>
</RelativeLayout>

適配器佈局`

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
        android:id="@+id/detail"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Activity頁面邏輯

package com.example.pagfany;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements BaseLinearLayoutManager.OnListener, SurfaceHolder.Callback {

    @BindView(R.id.ree)
    RecyclerView ree;
    @BindView(R.id.surface)
    SurfaceView surface;
    private BaseeAdap baseeAdap;
    private SurfaceHolder surfaceHolder;
    private Display display;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {//清除畫板上的內容
            super.handleMessage(msg);
           if (msg.what==1){
               Canvas canvas = surfaceHolder.lockCanvas();
               Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
               paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
               paint.setColor(Color.TRANSPARENT);
               RectF rectF = new RectF(0,0,display.getWidth(),display.getHeight());
               canvas.drawRect(rectF,paint);

               surfaceHolder.unlockCanvasAndPost(canvas);
           }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        BaseLinearLayoutManager baseLinearLayoutManager = new BaseLinearLayoutManager(this);
        baseLinearLayoutManager.setOnListener(this);//接口回調
        ree.setLayoutManager(baseLinearLayoutManager);
        List<StringBean> beans = new ArrayList<>();
        for (int i = 0; i < shuzu.videoUrls.length; i++) {
            beans.add(new StringBean(shuzu.videoUrls[i]));
        }
        baseeAdap = new BaseeAdap(R.layout.basevideo, beans);
        ree.setAdapter(baseeAdap);

        display=getWindowManager().getDefaultDisplay();//通過管理器獲取屏幕信息

        surface.setZOrderOnTop(true);//置頂
        surface.getHolder().setFormat(PixelFormat.TRANSPARENT);//畫板透明社
        surface.getHolder().addCallback(this);//接口回調

    }

    @Override
    public void init() {

    }

    @Override
    public void reselat() {
        baseeAdap.getreselat();//調用適配器的方法
    }

    @Override
    public void getplay(int position) {
        baseeAdap.gepalay(position);//滑動播放!!有錯誤
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        baseeAdap.getAllreselat();//關閉程序清空緩存
    }
    private long time=0;
    private float y;
    private float x;
    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        surfaceHolder=holder;
        surface.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){//雙擊
                    case MotionEvent.ACTION_DOWN://按下按鈕
                        if (time==0){
                            time=System.currentTimeMillis();//第一次時間
                        }else {
                            if (System.currentTimeMillis()-time<250){//當前時間 減去time
                                 y = event.getY();
                                 x = event.getX();//點擊的坐標
                                huaxin();//調用花心的方法
                                handler.sendEmptyMessageAtTime(1,500);
                            }else {
                                time=System.currentTimeMillis();
                            }
                        }
                        break;
                }
                return false;
            }
        });
    }

    private void huaxin() {
        Canvas canvas = surfaceHolder.lockCanvas();
        Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));//當想設置圖片重疊的相交狀態時
        paint.setColor(Color.TRANSPARENT);

        RectF rectF = new RectF(0,0,display.getWidth(),display.getHeight());
        canvas.drawRect(rectF,paint);//清空畫板

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        canvas.drawBitmap(bitmap,x,y,null);//註意不要用畫筆 當前畫筆是透明色
        surfaceHolder.unlockCanvasAndPost(canvas);

    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

    }
}

適配器

package com.example.pagfany;

import android.util.Log;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.List;

public class BaseeAdap extends BaseQuickAdapter<StringBean, BaseViewHolder> {
    private StandardGSYVideoPlayer standardGSYVideoPlayer;
    public BaseeAdap(int layoutResId, @Nullable List<StringBean> data) {
        super(layoutResId, data);
    }
    private int aa=-1;
    @Override
    protected void convert(BaseViewHolder helper, StringBean item) {
      standardGSYVideoPlayer =helper.getView(R.id.detail);
        standardGSYVideoPlayer.setUp(item.getA(),true,"美女");
        standardGSYVideoPlayer.startPlayLogic();
    }

    public void getreselat() {
        standardGSYVideoPlayer.release();
    }
    public void getAllreselat() {
        GSYVideoManager.releaseAllVideos();
    }

    public void gepalay(int position) {
        standardGSYVideoPlayer.setUp(shuzu.videoUrls[position],true,"美女");
        standardGSYVideoPlayer.startPlayLogic();
    }
}

自定義線性佈局

package com.example.pagfany;

import android.content.Context;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

public class BaseLinearLayoutManager extends LinearLayoutManager implements RecyclerView.OnChildAttachStateChangeListener {
    public BaseLinearLayoutManager(Context context) {
        super(context);
    }

    public BaseLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }
    private OnListener onListener;//接口

    public void setOnListener(OnListener onListener) {
        this.onListener = onListener;
    }

    interface OnListener{
        void init();
        void reselat();
        void getplay(int position);
    }

    private PagerSnapHelper pagerSnapHelper;//
    @Override
    public void onAttachedToWindow(RecyclerView view) {
        super.onAttachedToWindow(view);
        pagerSnapHelper=new PagerSnapHelper();
        pagerSnapHelper.attachToRecyclerView(view);//RecyclerView的滑動
        view.addOnChildAttachStateChangeListener(this);//滑動狀態監聽
    }

    private static final String TAG = "BaseLinearLayoutManager";
    @Override
    public void onScrollStateChanged(int state) {
        super.onScrollStateChanged(state);
        switch (state){
            case RecyclerView.SCROLL_STATE_IDLE:
                View snapView = pagerSnapHelper.findSnapView(this);
                int position = getPosition(snapView);
                onListener.getplay(position);
                Log.i(TAG, "onScrollStateChanged: "+position);
                break;
        }
    }

    @Override
    public void onChildViewAttachedToWindow(@NonNull View view) {
        Log.i(TAG, "onChildViewAttachedToWindow: "+132456);
    }

    @Override
    public void onChildViewDetachedFromWindow(@NonNull View view) {
          if (onListener!=null){
              onListener.reselat();//滑動下一個頁面 讓上個視頻沒有聲音 清除緩存
          }
    }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: