android studio实现视频图片轮播功能

Android Studio 如何实现视频图片轮播

本人用的android studio3.6版本。

res/layout的XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""xmlns:app=""xmlns:tools=""android:id="@+id/RelativeLayout"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"tools:context=".MainActivity"><ImageViewandroid:id="@+id/imgchange"android:layout_width="match_parent"android:layout_height="match_parent"android:visibility="visible"/><VideoViewandroid:id="@+id/videoView"android:layout_width="match_parent"android:layout_height="match_parent"android:visibility="gone"/></RelativeLayout>

在res/drawable中添加图片

在res中加入raw Folder后再res/raw中加入视频文件。

在mainActivity中

package com.example.handlertest;import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;import android.media.MediaPlayer;
import android.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.VideoView;import java.URI;
import java.util.Timer;
import java.util.TimerTask;public class MainActivity extends AppCompatActivity {public static final int PIC = 0;public static final int VID = 1;private static int start = 0;private ImageView imgchange;private VideoView videoView;private TimerTask task;private Timer timer= new Timer();private int Flag;int[] resourceIDs = {R.drawable.a,R.drawable.b,R.raw.r,R.raw.x,R.drawable.c};int[] resType={0,0,1,1,0};//0为图片,1为视频Handler mHandler = new Handler(){@Overridepublic void handleMessage(@NonNull Message msg) {super.handleMessage(msg);//接收消息Log.d("看看msg.what=",String.valueOf(msg.what));switch (msg.what){case PIC:Log.d("图片数据开始", String.valueOf(start));imgchange.setVisibility(View.VISIBLE);videoView.setVisibility(View.GONE);imgchange.setImageResource(resourceIDs[start]);start = (start+1)%5;Log.d("图片数据结束", String.valueOf(start));Flag=resType[start];break;case VID:imgchange.setVisibility(View.GONE);videoView.setVisibility(View.VISIBLE);Log.d("视频数据开始", String.valueOf(start));Flag = 2;首先要将这个标签换掉 不然会出现因为定时器的原因导致视频播放不全的问题。String uri = "android.resource://" + getPackageName() + "/" + resourceIDs[start];start=(start+1)%5;videoView.setVideoURI(Uri.parse(uri));//开始播放videoView.start();//播放完成回调videoView.setOnCompletionListener(new MyPlayerOnCompletionListener());//防止出现视频播放错误的问题videoView.setOnErrorListener(videoErrorListener);break;default:break;}}};//防出现无法播放此视频窗口public MediaPlayer.OnErrorListener videoErrorListener = new MediaPlayer.OnErrorListener() {@Overridepublic boolean onError(MediaPlayer mp, int what, int extra) {return true;}};//回调方法private class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {@Overridepublic void onCompletion(MediaPlayer mp) {/**再次启动图片的轮播,设置了imgstart为初始值*//**多个视频可以在这进行切换,进行一次判断加入还有视频就播放,没有就走下面这一段*/Flag=0;Log.d("图片数据结束", String.valueOf(start));task.cancel();startTask();}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if(getSupportActionBar()!=null)getSupportActionBar().hide();setContentView(R.layout.activity_main);imgchange = findViewById(R.id.imgchange);videoView = findViewById(R.id.videoView);startTask();}public void startTask(){task = new TimerTask() {@Overridepublic void run() {Message msg = Message.obtain();if (Flag == PIC) {Log.d("在run1中的MEG.WHAT=",String.valueOf(msg.what));msg.what = resType[start];mHandler.sendMessage(msg);}else if (Flag==VID){msg.what=resType[start];mHandler.sendMessage(msg);} else {Log.d("run3中的meg.what=",String.valueOf(msg.what));mHandler.sendEmptyMessage(Flag);}}};//定时器开始执行timer.schedule(task,0,20000);}
}

android studio实现视频图片轮播功能

Android Studio 如何实现视频图片轮播

本人用的android studio3.6版本。

res/layout的XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""xmlns:app=""xmlns:tools=""android:id="@+id/RelativeLayout"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"tools:context=".MainActivity"><ImageViewandroid:id="@+id/imgchange"android:layout_width="match_parent"android:layout_height="match_parent"android:visibility="visible"/><VideoViewandroid:id="@+id/videoView"android:layout_width="match_parent"android:layout_height="match_parent"android:visibility="gone"/></RelativeLayout>

在res/drawable中添加图片

在res中加入raw Folder后再res/raw中加入视频文件。

在mainActivity中

package com.example.handlertest;import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;import android.media.MediaPlayer;
import android.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.VideoView;import java.URI;
import java.util.Timer;
import java.util.TimerTask;public class MainActivity extends AppCompatActivity {public static final int PIC = 0;public static final int VID = 1;private static int start = 0;private ImageView imgchange;private VideoView videoView;private TimerTask task;private Timer timer= new Timer();private int Flag;int[] resourceIDs = {R.drawable.a,R.drawable.b,R.raw.r,R.raw.x,R.drawable.c};int[] resType={0,0,1,1,0};//0为图片,1为视频Handler mHandler = new Handler(){@Overridepublic void handleMessage(@NonNull Message msg) {super.handleMessage(msg);//接收消息Log.d("看看msg.what=",String.valueOf(msg.what));switch (msg.what){case PIC:Log.d("图片数据开始", String.valueOf(start));imgchange.setVisibility(View.VISIBLE);videoView.setVisibility(View.GONE);imgchange.setImageResource(resourceIDs[start]);start = (start+1)%5;Log.d("图片数据结束", String.valueOf(start));Flag=resType[start];break;case VID:imgchange.setVisibility(View.GONE);videoView.setVisibility(View.VISIBLE);Log.d("视频数据开始", String.valueOf(start));Flag = 2;首先要将这个标签换掉 不然会出现因为定时器的原因导致视频播放不全的问题。String uri = "android.resource://" + getPackageName() + "/" + resourceIDs[start];start=(start+1)%5;videoView.setVideoURI(Uri.parse(uri));//开始播放videoView.start();//播放完成回调videoView.setOnCompletionListener(new MyPlayerOnCompletionListener());//防止出现视频播放错误的问题videoView.setOnErrorListener(videoErrorListener);break;default:break;}}};//防出现无法播放此视频窗口public MediaPlayer.OnErrorListener videoErrorListener = new MediaPlayer.OnErrorListener() {@Overridepublic boolean onError(MediaPlayer mp, int what, int extra) {return true;}};//回调方法private class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {@Overridepublic void onCompletion(MediaPlayer mp) {/**再次启动图片的轮播,设置了imgstart为初始值*//**多个视频可以在这进行切换,进行一次判断加入还有视频就播放,没有就走下面这一段*/Flag=0;Log.d("图片数据结束", String.valueOf(start));task.cancel();startTask();}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if(getSupportActionBar()!=null)getSupportActionBar().hide();setContentView(R.layout.activity_main);imgchange = findViewById(R.id.imgchange);videoView = findViewById(R.id.videoView);startTask();}public void startTask(){task = new TimerTask() {@Overridepublic void run() {Message msg = Message.obtain();if (Flag == PIC) {Log.d("在run1中的MEG.WHAT=",String.valueOf(msg.what));msg.what = resType[start];mHandler.sendMessage(msg);}else if (Flag==VID){msg.what=resType[start];mHandler.sendMessage(msg);} else {Log.d("run3中的meg.what=",String.valueOf(msg.what));mHandler.sendEmptyMessage(Flag);}}};//定时器开始执行timer.schedule(task,0,20000);}
}