Android Imitation fire Fluorescent video desktop magical Livewallpaper

Source: Internet
Author: User

This article has been in my public number hongyangandroid original debut.
Reprint please indicate the source:
http://blog.csdn.net/lmj623565791/article/details/72170299
This article is from Zhang Hongyang's blog

I. Overview

Last week, my public number pushed an Android implementation "transparent screen, which I was particularly interested in after I saw it, and immediately contacted the author for authorization ~ ~

Welcome to scan the QR code on the left to follow my public number, every 7:30 to push the excellent technical blog.

Interested in the reason is that I am the connotation of the senior users of satin, the previous period of time is basically called fire fluorescent video desktop software (that is, the video as a desktop) to the brush screen, so read the author's code, see the Surfaceholder, immediately thought, can be used to play video to achieve the effect of video desktop, So the weekend to try, it is very simple.

So this article is unlimited thanks to the Android implementation of the "Transparent screen" article, the code is also part of the reference from its provided by the transparent camera.

Https://github.com/songixan/Wallpaper

That is true:

Note: This test machine for the Millet 5s, may be different phone will have some compatibility issues, try to solve the next, welcome message.

Ii. Implementation (1) configuration related

First, write an XML file that describes the wallpaper, ET, and so on, and for the thumbnail description sake of settingsActivity simplicity, only the thumbnail is set.

<?xml version="1.0" encoding="utf-8"?><wallpaper xmlns:android="http://schemas.android.com/apk/res/android"    android:thumbnail="@mipmap/ic_launcher" />
(2) Writing code

Wallpaper needs to be shown on the screen, and behind it is actually a service, so implementing a wallpaper requires inheriting from WallpaperService , implementing its abstract method onCreateEngine , as follows:

publicclass VideoLiveWallpaper extends WallpaperService {    publiconCreateEngine() {        returnnew VideoEngine();    }    //...}   

You can see that the return value is an inner class of Engine,engine, which contains onSurfaceCreated , onSurfaceChanged , onSurfaceDestroyed , and onTouchEvent so on, see these methods, immediately think of Surfaceview, For Surfaceview related knowledge, refer to:

    • Android Surfaceview to create lottery turntable

Also, do you remember playing videos on Android?

The conventional approach has been achieved through videoview, in addition to the MediaPlayer with Surfaceview coordination, today This example is similar to the latter.

We just need to mediaplayer the decoded data to the incoming surface.

Class Videoengine extends Engine {PrivateMediaPlayer Mmediaplayer;@Override     Public void onsurfacecreated(Surfaceholder Holder) {L.D ("videoengine#onsurfacecreated");Super. onsurfacecreated (holder); Mmediaplayer =NewMediaPlayer (); Mmediaplayer.setsurface (Holder.getsurface ());Try{Assetmanager ASSETMG = Getapplicationcontext (). Getassets (); Assetfiledescriptor FileDescriptor = ASSETMG.OPENFD ("Test1.mp4"); Mmediaplayer.setdatasource (Filedescriptor.getfiledescriptor (), Filedescriptor.getstartoffset (), FileDes            Criptor.getlength ()); Mmediaplayer.setlooping (true); Mmediaplayer.setvolume (0,0);            Mmediaplayer.prepare ();        Mmediaplayer.start (); }Catch(IOException e)        {E.printstacktrace (); }    }@Override     Public void onvisibilitychanged(BooleanVisible) {L.D ("videoengine#onvisibilitychanged visible ="+ visible);if(visible)        {Mmediaplayer.start (); }Else{Mmediaplayer.pause (); }    }@Override     Public void onsurfacedestroyed(Surfaceholder Holder) {L.D ("Videoengine#onsurfacedestroyed");Super. onsurfacedestroyed (holder);        Mmediaplayer.release (); Mmediaplayer =NULL; }

The code is very simple, in onsurfacecreated to initialize the Mmediaplayer, the core code is to set the Setsurface method, here I set the default mute.

Onvisibilitychanged, that is, when the desktop is not visible, we want to pause playback, and so on back to the desktop to continue.

Release resources when onsurfacedestroyed ~ ~

So our videolivewallpaper is written, do not forget that he is a service, we need to register.

<serviceandroid:name=". Videolivewallpaper "android:label=" @string/app_name "android:permission=" Android.permission.BIND_WALLPAPER "android:process=": Wallpaper ">                    <!--configuration Intent-filter --    <intent-filter>        <action android:name="Android.service.wallpaper.WallpaperService" />     </intent-filter>    <!--configuration Meta-data --    <meta-dataandroid:name="Android.service.wallpaper"android:resource= "@xml/livewallpaper" />                </Service>
(3) Set as Wallpaper

After the registration is complete, we add a button in Mainactivity click Set as Desktop background, the calling code is as follows

publicstaticvoidsetToWallPaper(Context context) {    finalnew Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);    intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,            new ComponentName(context, VideoLiveWallpaper.class));    context.startActivity(intent);}

This completes the initial writing of the code ~ ~

(4) Added support for some parameters

Just now we set the default is mute, maybe sometimes we would like to be able to dynamically control the parameters of the video desktop, normal should try to use settingsActivity , but I think the broadcast is also quite appropriate, nothing more than the service (possibly in a separate process) and activity and other communications AH ~ ~

Here we add a check box that supports setting the sound on or off sound.

 Public Static FinalString video_params_control_action ="Com.zhy.livewallpaper"; Public Static FinalString key_action ="Action"; Public Static Final intAction_voice_silence = the; Public Static Final intAction_voice_normal =111; class Videoengine extends Engine {//Omit other code    PrivateBroadcastreceiver Mvideoparamscontrolreceiver;@Override     Public void onCreate(Surfaceholder Surfaceholder) {Super. OnCreate (Surfaceholder); Intentfilter Intentfilter =NewIntentfilter (video_params_control_action); Registerreceiver (Mvideoparamscontrolreceiver =NewBroadcastreceiver () {@Override             Public void OnReceive(context context, Intent Intent) {L.D ("OnReceive");intAction = Intent.getintextra (Key_action,-1);Switch(action) { CaseACTION_VOICE_NORMAL:mMediaPlayer.setVolume (1.0F1.0f); Break; CaseACTION_VOICE_SILENCE:mMediaPlayer.setVolume (0,0); Break;    }}}, Intentfilter); }@Override     Public void OnDestroy() {unregisterreceiver (mvideoparamscontrolreceiver);Super. OnDestroy (); }}

The engine also has the OnCreate and OnDestroy declaration cycle method, which can register a dynamic broadcast in OnCreate, and then turn on the sound when the action that is sent is received, and a ACTION_VOICE_NORMAL ACTION_VOICE_SILENCE mute state is sent.

Finally, add two static methods directly to the Videolivewallpaper to send the broadcast:

publicstaticvoidvoiceSilence(Context context) {    new Intent(VideoLiveWallpaper.VIDEO_PARAMS_CONTROL_ACTION);    intent.putExtra(VideoLiveWallpaper.KEY_ACTION, VideoLiveWallpaper.ACTION_VOICE_SILENCE);    context.sendBroadcast(intent);}publicstaticvoidvoiceNormal(Context context) {    new Intent(VideoLiveWallpaper.VIDEO_PARAMS_CONTROL_ACTION);    intent.putExtra(VideoLiveWallpaper.KEY_ACTION, VideoLiveWallpaper.ACTION_VOICE_NORMAL);    context.sendBroadcast(intent);}

In the actiivty:

 Public  class mainactivity extends appcompatactivity {    PrivateCheckBox Mcbvoice;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Mcbvoice = (CheckBox) Findviewbyid (R.id.id_cb_voice); Mcbvoice.setoncheckedchangelistener (NewCompoundbutton.oncheckedchangelistener () {@Override                     Public void oncheckedchanged(Compoundbutton Buttonview,BooleanisChecked) {if(isChecked) {//MuteVideolivewallpaper.voicesilence (Getapplicationcontext ()); }Else{Videolivewallpaper.voicenormal (Getapplicationcontext ());    }                    }                }); }}

Monitor the checkbox status and send the broadcast.

OK, so a simple video desktop is finished ~ ~

Source Address:

    • Https://github.com/WanAndroid/LiveWallPaper/tree/master/hongyang/MagicWallPaper

Import this directory directly as a project.

Support My words can pay attention to my public number, every day will push new knowledge ~

Welcome to my public number: Hongyangandroid
(Can give me a message you want to learn articles, support contributions)

Reference
    • Http://www.vogella.com/tutorials/AndroidLiveWallpaper/article.html
    • http://www.jianshu.com/u/befb61deec9c

Android Imitation fire Fluorescent video desktop magical Livewallpaper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.