網站首頁 編程語言 正文
本文實例為大家分享了Android studio實現動態背景頁面的具體代碼,供大家參考,具體內容如下
第一步:
在res目錄下創建raw文件夾,并把想要導入的視頻放在里面
可以用格式工廠先把視頻格式化,以免視頻內存過大無法運行。
第二步:配置頁面布局xml文件
1.在activity_main.xml文件里加入以下代碼:
//放在大布局框架里 android:fitsSystemWindows="true" //放在布局框架內 ? ?<com.example.lovestoryapp.CustomVideoView ? ? ? ? android:id="@+id/videoview" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" />
2.在layout文件夾里創建videoview.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent"> ? ? <VideoView ? ? ? ? android:id="@+id/videoview" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:layout_marginBottom="-150dp" ? ? ? ? android:layout_alignParentLeft="true" ? ? ? ? android:layout_alignParentRight="true" ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? android:layout_alignParentTop="true" /> ? </RelativeLayout>
第三步:配置java文件
1.創建java文件 CustomVideoView.java
package com.example.lovestoryapp;
?
import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.VideoView;
?
public class CustomVideoView extends VideoView {
?
? ? public CustomVideoView(Context context) {
? ? ? ? super(context);
? ? }
?
? ? public CustomVideoView(Context context, AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? }
?
? ? public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
? ? ? ? super(context, attrs, defStyle);
? ? }
?
? ? @Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
? ? ? ? //我們重新計算高度
? ? ? ? int width = getDefaultSize(0, widthMeasureSpec);
? ? ? ? int height = getDefaultSize(0, heightMeasureSpec);
? ? ? ? setMeasuredDimension(width, height);
? ? }
?
? ? @Override
? ? public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
? ? ? ? super.setOnPreparedListener(l);
? ? }
?
? ? @Override
? ? public boolean onKeyDown(int keyCode, KeyEvent event) {
? ? ? ? return super.onKeyDown(keyCode, event);
? ? }
}
2.在MainActivity.java的Activity方法中加入以下代碼
?//找VideoView控件
? ? ? ? customVideoView = (CustomVideoView)findViewById(R.id.videoview);
? ? ? ? //加載視頻文件
? ? ? ? customVideoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.shipin1));
? ? ? ? //播放
? ? ? ? customVideoView.start();
? ? ? ? //循環播放
? ? ? ? customVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onCompletion(MediaPlayer mediaPlayer) {
? ? ? ? ? ? ? ? customVideoView.start();
? ? ? ? ? ? }
? ? ? ? });
?
? ? }
第四步:運行至模擬器
原文鏈接:https://blog.csdn.net/sweetlcw/article/details/121525303
- 上一篇:C#連接數據庫的幾種方法_C#教程
- 下一篇:C++簡明分析臨時對象是什么_C 語言
相關推薦
- 2022-07-12 k8s conntrack 表項超時導致tcp長連接中斷
- 2023-01-30 基于redis樂觀鎖實現并發排隊_Redis
- 2022-04-02 C#實現NPOI的Excel導出詳解_C#教程
- 2022-10-03 React如何實現全屏監聽Esc鍵_React
- 2022-07-04 python設計模式之裝飾器模式_python
- 2022-10-16 python列表倒序的幾種方法(切片、reverse()、reversed())_python
- 2022-07-01 使用Python讀寫多個sheet文件_python
- 2022-01-29 composer 安裝包提示內存不足的解決辦法
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支