Android?studio實現(xiàn)動態(tài)背景頁面
本文實例為大家分享了Android studio實現(xiàn)動態(tài)背景頁面的具體代碼,供大家參考,具體內(nèi)容如下
第一步:
在res目錄下創(chuàng)建raw文件夾,并把想要導(dǎo)入的視頻放在里面
可以用格式工廠先把視頻格式化,以免視頻內(nèi)存過大無法運行。
第二步:配置頁面布局xml文件
1.在activity_main.xml文件里加入以下代碼:

//放在大布局框架里 android:fitsSystemWindows="true" //放在布局框架內(nèi) ? ?<com.example.lovestoryapp.CustomVideoView ? ? ? ? android:id="@+id/videoview" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" />
2.在layout文件夾里創(chuàng)建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.創(chuàng)建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();
? ? ? ? //循環(huán)播放
? ? ? ? customVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onCompletion(MediaPlayer mediaPlayer) {
? ? ? ? ? ? ? ? customVideoView.start();
? ? ? ? ? ? }
? ? ? ? });
?
? ? }第四步:運行至模擬器
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程視頻播放API之MediaPlayer用法示例
這篇文章主要介紹了Android編程視頻播放API之MediaPlayer用法,結(jié)合實例形式分析了基于Android API實現(xiàn)視頻播放功能的多媒體文件讀取、判斷、事件響應(yīng)及流媒體播放等相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-08-08
android編程實現(xiàn)sd卡讀取數(shù)據(jù)庫的方法
這篇文章主要介紹了android編程實現(xiàn)sd卡讀取數(shù)據(jù)庫的方法,涉及Android權(quán)限控制及針對sd卡與數(shù)據(jù)庫的相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
解決Bitmap通過getWidth和getHeight獲取尺寸不符的問題
這篇文章主要介紹了解決Bitmap通過getWidth和getHeight獲取尺寸不符的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
詳解Android應(yīng)用開發(fā)中Intent的作用及使用方法
這篇文章主要介紹了Android應(yīng)用開發(fā)中Intent的作用與用法,包括如何激活A(yù)ctivity組件與Intent的投遞等,需要的朋友可以參考下2016-03-03
詳解Android中的ActivityThread和APP啟動過程
ActivityThread就是我們常說的主線程或UI線程,ActivityThread的main方法是整個APP的入口,本篇深入學(xué)習(xí)下ActivityThread,順便了解下APP和Activity的啟動過程。2021-06-06
Android Studio 3.1.X中導(dǎo)入項目的正確方法分享
這篇文章主要給大家介紹了關(guān)于Android Studio 3.1.X中導(dǎo)入項目的正確方法,文中一步步將解決的方法以及可能遇到的問題介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
設(shè)置Android設(shè)備WIFI在休眠時永不斷開的代碼實現(xiàn)
這篇文章主要介紹了設(shè)置Android設(shè)備WIFI在休眠時永不斷開的代碼實現(xiàn),需要的朋友可以參考下2014-07-07
Android AutoCompleteTextView連接數(shù)據(jù)庫自動提示的方法(附demo源碼下載)
這篇文章主要介紹了Android AutoCompleteTextView連接數(shù)據(jù)庫自動提示的方法,結(jié)合實例形式分析了AutoCompleteTextView操作數(shù)據(jù)庫的原理與具體技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02

