I believe that last Christmas opened a mobile phone Taobao children's shoes will be fresh memories of the time: Full screen snow, next to a light snow people to control the play of box background music, people have a kind of immersive feeling, and even can not help but want to go shopping hard (wrong), is probably the following this way:
Well, it's really cool, so let's take a step-by-step look at how that is achieved:
First, the realization of the snow View
First of all, the top layer of the full screen snowflakes most likely is a top-level view, and this view is dynamically loaded to control the display (do not update Taobao can see this effect). Then we have to realize the snowflake effect of View, life is short, take it. Open Gank.io, search for "snowflake":
It seems that the 7th library is what we want, point into the source code, direct download do not explain, remember the star a support author. So now we have a complete View of the snow effect in our project.
Second, the realization of the Snowman viewer View
This is a picture of a snowman + a button can be achieved, not much explanation. The next need for a Christmas audio, direct online audio playback is undoubtedly a good space-saving solution. "My skateboard Shoes" foil the lonely and sweet atmosphere is undoubtedly the most suitable for Christmas, so we got the "Divine Comedy" URL One:
Http://cdn.ifancc.com/TomaToDo/bgms/my_hbx.mp3
Next, find a picture of a snowman as the background of the player, then Armstrong ... No, this is it:
Well, quite lovely and festive. So the player core code is as follows:
Package com.kot32.christmasview.player;
Import Android.content.Context;
Import Android.media.AudioManager;
Import Android.media.MediaPlayer;
Import Android.util.AttributeSet;
Import Android.view.View;
Import Android.widget.Toast;
Import COM.KOT32.CHRISTMASVIEW.R;
Import java.io.IOException;
/** * Created by Kot32 on 16/12/8.
* * public class Myplayer extends View {public MediaPlayer MediaPlayer;
Public Myplayer {Super (context);
Init ();
Public Myplayer (context, AttributeSet attrs) {Super (context, attrs);
Init ();
private void Init () {setbackgroundresource (R.drawable.pig);
MediaPlayer = new MediaPlayer ();
Mediaplayer.setaudiostreamtype (Audiomanager.stream_music);
Playurl ("Http://172.20.248.106/IXC5b415fcacfc3c439e25a3e74533d2239/TomaToDo/bgms/my_hbx.mp3");
Toast.maketext (GetContext (), "Start playing", Toast.length_short). Show (); Setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {if (!mediaplayer).IsPlaying ()) {Mediaplayer.start ();
Toast.maketext (GetContext (), "continue playing", Toast.length_short). Show ();
else {mediaplayer.pause ();
Toast.maketext (GetContext (), "Pause playback", Toast.length_short). Show ();
}
}
});
public void Playurl (String videourl) {try {mediaplayer.reset ();
Mediaplayer.setdatasource (Videourl);
Mediaplayer.prepare ();//prepare automatically play Mediaplayer.start ();
catch (IllegalArgumentException e) {e.printstacktrace ();
catch (IllegalStateException e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
}} @Override protected void Ondetachedfromwindow () {Super.ondetachedfromwindow ();
try {mediaplayer.stop ();
Mediaplayer.release ();
}catch (Exception e) {e.printstacktrace (); }
}
}
Three, dynamic loading ideas
The above basic implementation of the local snowflakes and play music effect, then without updating the main program, how to dynamically load the two view into the main program?
First we understand that Android's dexclassloader has the ability to load arbitrary classes in any apk, with the following limitations:
The loaded activity cannot be found and initialized by the framework because it is not declared in the host Manifest file.
The loaded activity does not have a life cycle, for the same reason.
The resource file ID of the loaded class is confused with the main program.
Since we're just loading the view, not loading the entire activity, the first two problems won't be met, and the third problem can be solved.
In the main program we have to do these three things:
To keep the empty space of the viewgroup that can load the view.
To get the updated patch package
After you load the view from the APK package, put it in the ViewGroup.
As a result, not only Christmas, but also a variety of activities can be online to load the view of the activity.
Iv. Start loading
Before loading the view, first realize that this view is a reference to the image resource (piggy image), so we have to solve the resource problem:
private void Initresource () {Resources
= GetContext (). Getresources ();
try {
Assetmanager Newmanager = AssetManager.class.newInstance ();
Method Addassetpath = Newmanager.getclass (). GetMethod ("Addassetpath", string.class);
Addassetpath.invoke (Newmanager, Dynamicviewmanager.getinstance (). Getupdatefilefullpath ());
Newresources = new Resources (Newmanager,
resources.getdisplaymetrics (), resources.getconfiguration ()) ;
Reflect.onobject (GetContext ()). Set ("Mresources", newresources);
catch (Exception e) {
e.printstacktrace ();
}
}
The above code works by assigning a resource manager with an external update package path to the app's original resource manager, which means that the plug-in resource can now be accessed in the host.
The core load code is as follows:
Dexclassloader ClassLoader = new Dexclassloader (Apkfile.getabsolutepath (), Dex_ou
T_put_dir ", NULL, GetClass (). getClassLoader ());
Class Newviewclazz = Classloader.loadclass ("View ' s package name");
Constructor con = newviewclazz.getconstructor (context.class); The Resource lie to View if (Dynamicview = = null) {Dynamicview = (View) con.newinstance (GetContext ())
; }//replace the "View" mresources and recovery the activity ' s avoid disorder to resources reflect.onobject (GetContext ()). s
ET ("mresources", null);
GetContext (). Getresources (); Relativelayout.layoutparams layoutparams = new Relativelayout.layoutparams (displayutil.dip2px (GetContext (),
ViewInfo.layoutParams.width), displayutil.dip2px (GetContext (), viewInfo.layoutParams.height));
Layoutparams.addrule (Relativelayout.center_in_parent, relativelayout.true);
AddView (Dynamicview, layoutparams);
The middle action for mresources is to reset the mresources of the host's activity to avoid conflicts with plug-ins when resources are used in the activity.
However, I have the wit of the update package download, version management, dynamic loading are encapsulated, so the correct loading mode is:
Reference it: Https://github.com/kot32go/dynamic-load-view
And then:
1. The host declares:
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:app= "Http://schemas.android.com/apk/res-auto" android:layout_width= "Match_parent" android:layout_height= " Match_parent "android:background=" @drawable/tb_bg "> < Com.kot32.dynamicloadviewlibrary.core.DynamicViewGroup android:layout_width= "Match_parent" android:layout_height = "Match_parent" app:uuid= "Activity_frame" > <textview android:layout_width= "wrap_content" android:layout_he ight= "Wrap_content" android:text= "original page"/> </com.kot32.dynamicloadviewlibrary.core.DynamicViewGroup> <
Com.kot32.dynamicloadviewlibrary.core.DynamicViewGroup android:layout_width= "60DP" android:layout_height= "60DP" Android:layout_alignparentright= "true" android:layout_centervertical= "true" app:uuid= "Activity_player" > </ Com.kot32.dynamicloadviewlibrary.core.dynamicviewgroup> </relativelayout>
The above declaration of the layout of the main interface, of course, in addition to the original "original page" TextView, there will be no other things, that is, before the advent of Christmas program. Note: The UUID will match the online package.
2. Play the plug-in package
In fact, the program that contains the two view (snowflakes and snowmen) that we have written before is packaged into APK. Can not sign.
3. Put the plug-in package on the server
In the JSON returned by the server, declare the plug-in package address and some parameters for dynamic view, where the demo request address is:
http://tomatodo.ifancc.com/php/dynamicView.php
The return value is:
{"
version": "
Downloadpath": "http://obfgb7oet.bkt.clouddn.com/patch106.apk",
"FileName": " patch106.apk ",
" ViewInfo ": [
{
" PackageName ":" Com.kot32.testdynamicviewproject.snow.widgets.SnowingView ",
" uuid ":" Activity_frame ",
" Layoutparams ": {
"width":-1,
"height":-1
}
},
{
"PackageName": " Com.kot32.testdynamicviewproject.player.MyPlayer ",
" uuid ":" Activity_player ",
" Layoutparams ": {
" Width ":-1,
" height ":-1
}
}
]
}
We have declared the version of this online package, the package name and layout parameters for each view, and the most important and the UUID declared aligned in the host program.
The above is a small set of Android to introduce the use of dynamic load to realize the mobile phone Taobao holiday effects, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!