20140917 Setting Live Wallpaper

Source: Internet
Author: User

In Manifest.xml:

         <service             android:enabled= "true"            android:permission= "Android.permission.BIND_WALLPAPER"            android: Name= "Com.android.settings.common.display.BackgroundSettingService" >            <intent-filter>                < Action android:name= "Android.service.wallpaper.WallpaperService" ></action>            </intent-filter>            <meta-data                 android:name= "Android.service.wallpaper"                 android:resource= "@xml/wallpaper" >            </meta-data>        </service>

  

Check the entries in the settings:

Activity activity = getactivity (); Preferencescreen screenbackgroundsetting = (preferencescreen) findpreference (key_background_setting); Screenbackgroundsetting! = null) {Screenbackgroundsetting.setonpreferenceclicklistener (new Onpreferenceclicklistener () {@Overridepublic Boolean onpreferenceclick (Preference Preference) {//TODO auto-generated Method Stubstartfragmenttransaction (New Commonbackgroundsettingfragment ()); return true;}});

The right-hand interface switches to the following fragment:

Package Com.android.settings.common.display;import Android.app.fragment;import Android.app.wallpaperinfo;import Android.app.wallpapermanager;import Android.content.context;import Android.content.intent;import Android.os.bundle;import Android.util.log;import Android.view.layoutinflater;import Android.view.View;import Android.view.viewgroup;import Android.widget.button;import Android.widget.toast;import Com.android.settings.R; public class Commonbackgroundsettingfragment extends Fragment{private Button btnbackground1;private static final String Tagetpackagename = "Com.android.wallpaper";p rivate static final int request_set_live_wallpaper = 101;private static Final String TAG = "commonbackgroundsettingfragment"; @Overridepublic void OnCreate (Bundle savedinstancestate) {//TODO Auto-generated method Stubsuper.oncreate (savedinstancestate);} @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {view Rootview = Inflater.inflate (R.layout.fragmeNt_background_setting, container, false); btnBackground1 = (Button) Rootview.findviewbyid (R.ID.BTNBACKGROUND1); Btnbackground1.requestfocus (); Btnbackground1.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (); intent.setaction (Wallpapermanager.action_live_wallpaper_chooser); Startactivityforresult (Intent, request_set_live_wallpaper); Toast.maketext (CommonBackgroundSettingFragment.this.getActivity (), getString (r.string.start_background_setting) , Toast.length_long). Show ();}}); return Rootview;} @Overridepublic void Onresume () {//TODO auto-generated method Stubsuper.onresume ();} @Overridepublic void Onactivityresult (int requestcode, int resultcode, Intent data) {if (Requestcode = = Request_set_live_ Wallpaper) {if (Islivewallpaperrunning (This.getactivity (), tagetpackagename)) {Toast.makete                           XT (This.getactivity (), "Livewallpaper is Running", Toast.length_long). Show ();         }else{       Toast.maketext (This.getactivity (), "Livewallpaper is not Running", Toast.length_long). Show ();  }} super.onactivityresult (Requestcode, ResultCode, data);            }/** * Determine if a live wallpaper is already running * * @param context *: Context * @param tagetpackagename * : To determine the package name of the Live wallpaper * @return */public static Boolean islivewallpaperrunning (context context, String Tage Tpackagename) {Wallpapermanager Wallpapermanager = wallpapermanager.getinstance (context);//Get Wallpaper manager Wal Lpaperinfo wallpaperinfo = Wallpapermanager.getwallpaperinfo ();//If the system uses a wallpaper that is live wallpaper then returns the information for that live wallpaper, otherwise it will return NULL if (Wallpa Perinfo! = null) {//If it is a live wallpaper, get the package name of the live wallpaper and compare it with the name of the live wallpaper that you want to know. String Currentlivewallpaperpackagename = Wallpaperi              Nfo.getpackagename ();              if (Currentlivewallpaperpackagename.equals (Tagetpackagename)) {return true;      }} return false; }  }

 

Set the service for live wallpaper:

/** * Livewallpaper Launcher and Preference Template * the MIT License * Copyright (c) mdlab.jp * @author Itoz */pack Age Com.android.settings.common.display;import Java.sql.date;import Java.text.simpledateformat;import Android.annotation.suppresslint;import Android.content.sharedpreferences;import android.content.res.Resources; Import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.os.bundle;import Android.os.Handler;import Android.service.wallpaper.wallpaperservice;import Android.util.log;import Android.view.motionevent;import Android.view.surfaceholder;import android.widget.toast;import COM.ANDROID.SETTINGS.R; @SuppressLint ("NewApi") public class Backgroundsettingservice extends wallpaperservice{private static final String TAG = " Backgroundsettingservice ";                     Private Handler Handler = new Handler ();         @Override public void OnCreate () { LOG.I (TAG, "service OnCreate");      Super.oncreate ();          } @Override public void OnDestroy () {log.i (TAG, "service Ondestory");      Super.ondestroy ();          } @Override Public Engine oncreateengine () {log.i (TAG, "service Oncreateengine");      return new Myengine ();          } private class Myengine extends engine{private float x = 200;          Private float y = 300;          Private Paint Mpaint;                   Private Boolean mvisible = false;              Private Runnable Drawthread = new Runnable () {public void run () {drawwallpaper ();                   }          };              Public Myengine () {log.i (TAG, "myengine");              Mpaint = new Paint ();              Mpaint.setcolor (color.red);              Mpaint.setantialias (TRUE);              Mpaint.setstrokewidth (4);              Mpaint.setstyle (Paint.Style.STROKE);        Mpaint.settextsize (24);  } @Override Public Bundle OnCommand (String action, int x, int y, int z, Bundle extras, Boolean Resul              trequested) {//TODO auto-generated Method Stub log.i (TAG, "OnCommand");          Return Super.oncommand (action, x, Y, z, Extras, resultrequested);              } @Override public void OnCreate (Surfaceholder surfaceholder) {log.i (TAG, "onCreate");              Super.oncreate (Surfaceholder);          The function is to make the wallpaper responsive to touch event, and the default is False settoucheventsenabled (true);              } @Override public void OnDestroy () {log.i (TAG, "OnDestroy");              Super.ondestroy ();          Handler.removecallbacks (Drawthread); } @Override public void onoffsetschanged (float xoffset, float yoffset, float xoffsetstep, float YOFFSE tstep, int xpixeloffset, int ypixeloffset) {//TODO auto-generated Method Stub log.i (TAG, "Onoffset Schanged ");          Super.onoffsetschanged (Xoffset, Yoffset, Xoffsetstep, Yoffsetstep, Xpixeloffset, Ypixeloffset); } @Override public void onvisibilitychanged (Boolean visible) {log.i (TAG, "onvisibilit              Ychanged ");              mvisible = visible;              if (visible) {//Start handler.postdelayed (Drawthread, 50);              } else {handler.removecallbacks (drawthread); }} @Override public void onsurfacechanged (surfaceholder holder, int format, int width, int h              Eight) {log.i (TAG, "onsurfacechanged");          TODO auto-generated Method Stub super.onsurfacechanged (holder, format, width, height); } @Override public void onsurfacecreated (Surfaceholder holder) {log.i (TAG, "onsurfacecreated              ");          super.onsurfacecreated (holder); } @Override public void Onsurfacedestroyed (Surfaceholder holder) {log.i (TAG, "onsurfacedestoryed");              TODO auto-generated Method Stub super.onsurfacedestroyed (holder);          Handler.removecallbacks (Drawthread);              } @Override public void Ontouchevent (Motionevent event) {log.i (TAG, "ontouchevent");          TODO auto-generated Method Stub super.ontouchevent (event); } private void Drawwallpaper () {Surfaceholder holder = Getsurfaceho              Lder ();              Canvas canvas = Holder.lockcanvas ();              Drawtime (canvas);                            Holder.unlockcanvasandpost (canvas);          Cyclic execution handler.postdelayed (drawthread, 50);              } private void Drawtime (canvas canvas) {Date date = new Date (System.currenttimemillis ()); SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");              String datastr = Sdf.format (date);              Canvas.save ();            Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.captain_america);            Bitmap mbitmapdisplayed = Bitmap.createscaledbitmap (Bitmap, Bitmap.getwidth (), Bitmap.getheight (), false);              Canvas.drawbitmap (mbitmapdisplayed, 0, 0, NULL);//Canvas.drawcolor (0XFF00FF00);              Canvas.drawtext (Datastr, x, Y, mpaint);          Canvas.restore (); }      }  }

 

The effect is: The background is "Captain_america.png", while the upper layer has time dynamic update.

20140917 Setting Live Wallpaper

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.