温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Android中实现短视频画心效果

发布时间:2021-05-25 15:24:20 来源:亿速云 阅读:206 作者:Leah 栏目:开发技术

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

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

关于如何在Android中实现短视频画心效果问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI