首页 > 编程语言 > Android实现短视频画心效果
2021
07-12

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();//滑动下一个页面 让上个视频没有声音 清除缓存
          }
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧