Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize dynamic background Page with Android studio

Shulou Source: shulou.com Published: 2022-05-31 20:02:02 10月18日 Update

This article introduces the relevant knowledge of "Android studio how to realize dynamic background page". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Step 1:

Create a raw folder in the res directory and put the videos you want to import into it

You can use the format factory to format the video first, so that the video memory is too large to run.

Step 2: Configure the page layout xml file

1. Add the following code to activity_main.xml:

//put in large layout frame android:fitsSystemWindows="true"//put in layout frame

2. Create videoview.xml in the layout folder

Step 3: Configure java files

1. Create java file 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) { //we recalculate altitude 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. Add the following code to the Activity method of MainActivity.java

//Find VideoView controls customVideoView = (CustomVideoView)findViewById(R.id.videoview); //Load video files customVideoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.shipin1)); //Play customVideoView.start(); //loop customVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { customVideoView.start(); } }); }

Step 4: Run to Simulator

"Android studio how to achieve dynamic background page" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: File video page layout dynamic background code content folder more format framework knowledge run configuration practical learned next memory dilemma Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS Microsoft Shulou Information Redmi