In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces Android how to achieve dynamic wallpaper related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this Android how to achieve dynamic wallpaper article will have a harvest, let's take a look at it.
I. Overview:
The wallpaper runs in an Android service called WallpaperService. When the user selects a wallpaper, the WallpaperService corresponding to the wallpaper starts and starts to draw the wallpaper.
Engine is an inner class in WallpaperService, which realizes the creation of wallpaper window and the maintenance of Surface. Engine implements SurfaceView internally, and we only need to use MediaPlayer + SurfaceView internally to play dynamic wallpaper.
Second, to achieve:
WallpaperService needs a xml to configure and then declare it in AndroidManifest.xml
Inheriting WallpaperService to implement our own wallpaper service VideoLiveWallpaper
Public class VideoLiveWallpaper extends WallpaperService {@ Override public Engine onCreateEngine () {return new VideoEngine ();} class VideoEngine extends Engine {private MediaPlayer mMediaPlayer; @ Override public void onCreate (SurfaceHolder surfaceHolder) {super.onCreate (surfaceHolder);} @ Override public void onDestroy () {super.onDestroy () } @ Override public void onSurfaceCreated (SurfaceHolder holder) {super.onSurfaceCreated (holder); mMediaPlayer = new MediaPlayer (); mMediaPlayer.setSurface (holder.getSurface ()); try {mMediaPlayer.setDataSource (new File (FileUtil.getDCIMCameraDir (), "hlj_wallpaper") .getAbsolutePath ()); mMediaPlayer.setLooping (true) MMediaPlayer.setVolume (0,0); mMediaPlayer.prepare (); mMediaPlayer.setOnPreparedListener (new MediaPlayer.OnPreparedListener () {@ Override public void onPrepared (MediaPlayer mp) {mMediaPlayer.start ();}}) } catch (IOException e) {e.printStackTrace ();}} @ Override public void onSurfaceDestroyed (SurfaceHolder holder) {super.onSurfaceDestroyed (holder); mMediaPlayer.release (); mMediaPlayer = null } @ Override public void onVisibilityChanged (boolean visible) {if (visible) {mMediaPlayer.start ();} else {mMediaPlayer.pause ();}
Then declare the service and declare the xml configuration we wrote above.
The key point is in the onSurfaceCreated method, in order to dynamically switch different wallpapers, I specify to load the video files in a fixed directory, and then copy new files to this directory constantly, because once the wallpaper switch method is enabled, the method will be called, so when the call is dynamically notified to change the path does not work.
So I emptied the wallpaper before changing it.
Try {WallpaperManager.getInstance (getContext ()) .clear ();} catch (IOException e) {e.printStackTrace ();}
Then copy the wallpaper that needs to be replaced to the specified directory
CopyFile (file.getAbsolutePath (), new File (FileUtil.getDCIMCameraDir (), "hlj_wallpaper") .getAbsolutePath ()) / * copy a single file * * @ param oldPath String original file path such as: c:/fqf.txt * @ param newPath String copy path such as: f:/fqf.txt * @ return boolean * / public void copyFile (final String oldPath, final String newPath) {progressBar.setVisibility (View.VISIBLE) Observable.create (new Observable.OnSubscribe () {@ Override public void call (Subscriber)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.