Android?studio實(shí)現(xiàn)動(dòng)態(tài)背景頁(yè)面
本文實(shí)例為大家分享了Android studio實(shí)現(xiàn)動(dòng)態(tài)背景頁(yè)面的具體代碼,供大家參考,具體內(nèi)容如下
第一步:
在res目錄下創(chuàng)建raw文件夾,并把想要導(dǎo)入的視頻放在里面
可以用格式工廠先把視頻格式化,以免視頻內(nèi)存過(guò)大無(wú)法運(yùn)行。
第二步:配置頁(yè)面布局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) {
? ? ? ? //我們重新計(jì)算高度
? ? ? ? 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();
? ? ? ? ? ? }
? ? ? ? });
?
? ? }第四步:運(yùn)行至模擬器
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android簡(jiǎn)單實(shí)現(xiàn)app每月簽到功能
這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)單實(shí)現(xiàn)app每月簽到功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android SeekBar實(shí)現(xiàn)滑動(dòng)條效果
這篇文章主要為大家詳細(xì)介紹了Android SeekBar實(shí)現(xiàn)滑動(dòng)條效果,可以改變并顯示當(dāng)前進(jìn)度的拖動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android屏蔽軟鍵盤(pán)自動(dòng)彈出的解決方案
在編輯框輸入內(nèi)容時(shí)會(huì)彈出軟鍵盤(pán),而手機(jī)屏幕區(qū)域有限往往會(huì)遮住輸入界面,怎么實(shí)現(xiàn)這種效果呢?下面小編給大家分享了Android屏蔽軟鍵盤(pán)自動(dòng)彈出的解決方案,需要的朋友參考下吧2017-01-01
ViewPager 滑動(dòng)一半的判斷方法以及左滑右滑判斷實(shí)例
下面小編就為大家?guī)?lái)一篇ViewPager 滑動(dòng)一半的判斷方法以及左滑右滑判斷實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
Flutter?webview與網(wǎng)頁(yè)通訊交互實(shí)現(xiàn)
最近要在Flutter項(xiàng)目的基礎(chǔ)上加一個(gè)實(shí)時(shí)定位的功能,下面這篇文章主要給大家介紹了關(guān)于Flutter?webview與網(wǎng)頁(yè)通訊交互實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
超簡(jiǎn)單Android集成華為HMS Scankit 掃碼SDK實(shí)現(xiàn)掃一掃二維碼
這篇文章主要介紹了超簡(jiǎn)單Android集成華為HMS Scankit 掃碼SDK實(shí)現(xiàn)掃一掃二維碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
LayoutAnimation給ListView中的item設(shè)置動(dòng)態(tài)出場(chǎng)效果(實(shí)例)
下面小編就為大家?guī)?lái)一篇LayoutAnimation給ListView中的item設(shè)置動(dòng)態(tài)出場(chǎng)效果(實(shí)例)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10

