Amazing 3D special effect album function Android (lower)
Tank, I spent some time reorganizing it yesterday. I added a lot of comments to make everyone understand.
After finishing the work, you can implement a superb 3D effect on the basis of the original one.ProgramManagement function, so it is more useful, not just to display images.
Implementation results:
Display the list of all installed programs in 3D. Click an image to display the first one dynamically. You can open the program with a long press. (As shown in the previous blog, I will not post it here)
The main process is as follows:
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Instantiate the launcher list to obtain the application information list (including images)
Getlauncher ();
Final coverflow cf = new coverflow (this); // rewrite Garry's getchildstatictransformation to generate cascade and zoom-in Effects
// Fill the http://www.cnblogs.com/tankaixiong/ of the picture I want (original)
Cf. setadapter (New imageadapter (this ));
// Customize the filling mode of the image
Imageadapter = new imageadapter (this );
Cf. setadapter (imageadapter );
Cf. setanimationduration (1500 );
Cf. setonitemclicklistener (this );
Cf. setonitemlongclicklistener (lonclick );
Setcontentview (CF );
}
Step 1:
Create an object class to save program information:
Launcheritem
Package com. Android. tank;
Import Android. content. componentname;
Import Android. Graphics. drawable. drawable;
Public class launcheritem {
Drawable icon;
String name;
Componentname component;
Launcheritem (drawable D, string S, componentname CN ){
Icon = D;
Name = s;
Component = cn;
}
Public drawable getIcon (){
Return icon;
}
Public void seticon (drawable icon ){
This. Icon = icon;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public componentname getcomponent (){
Return component;
}
Public void setcomponent (componentname component ){
This. Component = component;
}
};
Step 2: Store
// Save the list of information (including image information) obtained from the application. You can also set a set of images by yourself.
List <launcheritem> lvalue;
// Obtain the App list information
Public void getlauncher (){
Lvalue = new arraylist <launcheritem> ();
Packagemanager pkgmgt = This. getpackagemanager (); // This method is critical.
// To query all launcher & load into list <>
Intent it = new intent (intent. action_main );
It. addcategory (intent. category_launcher );
List <resolveinfo> Ra = pkgmgt. queryintentactivities (it, 0); // query
// Store the http://www.cnblogs.com/tankaixiong/ in the collection (original)
For (INT I = 0; I <Ra. Size (); I ++ ){
Activityinfo AI = Ra. Get (I). activityinfo;
// String ainfo = ai. tostring ();
Drawable icon = ai. loadicon (pkgmgt );
String label = ai. loadlabel (pkgmgt). tostring ();
Componentname c = new componentname (AI. applicationinfo. packagename,
AI. Name );
Launcheritem item = new launcheritem (icon, label, C );
Lvalue. Add (item );
}
}
Step 3: rewrite baseadapter
Public class imageadapter extends baseadapter {
Int mgalleryitembackground;
Private context mcontext;
Public imageadapter (context ){
Mcontext = context;
Typedarray = obtainstyledattributes (R. styleable. Gallery );
Mgalleryitembackground = typedarray. getresourceid (
R. styleable. gallery_android_galleryitembackground, 0 );
}
// Improve at 1st, and return a large value, for example, integer. max_value
Public int getcount (){
Return resids. length;
}
Public object getitem (INT position ){
Return position;
}
Public long getitemid (INT position ){
Return position;
}
Public View getview (INT position, view convertview, viewgroup parent ){
Imageview IV = new imageview (mcontext );
Iv. setimagedrawable (lvalue. Get (position). Icon );
Iv. setimagebitmap (myimgview. createreflectedimage (myimgview
. Drawabletobitmap (lvalue. Get (position). Icon); // Add the processed image
Iv. setlayoutparams (new gallery. layoutparams (80, 60 ));
Return IV;
}
Step 4: process images and produce special effects
Package com. Android. tank;
Import Android. Graphics. Bitmap;
Import Android. Graphics. Canvas;
Import Android. Graphics. lineargradient;
Import Android. Graphics. matrix;
Import Android. Graphics. paint;
Import Android. Graphics. pixelformat;
Import Android. Graphics. porterduduxfermode;
Import Android. Graphics. bitmap. config;
Import Android. Graphics. porterduff. mode;
Import Android. Graphics. shader. tilemode;
Import Android. Graphics. drawable. drawable;
Public class myimgview {
/**
* Add a reflection. The principle is to flip the image first, and enlarge the transparency from top to bottom.
* Http://www.cnblogs.com/tankaixiong/ (original)
* @ Param originalimage
* @ Return
*/
Public static bitmap createreflectedimage (Bitmap originalimage ){
// The gap we want between the reflection and the original image
Final int reflectiongap = 4;
Int width = originalimage. getwidth ();
Int Height = originalimage. getheight ();
// This will not scale but will flip on the Y axis
Matrix matrix = new matrix ();
Matrix. prescale (1,-1 );
// Create a bitmap with the flip matrix applied to it.
// We only want the bottom half of the image
Bitmap reflectionimage = bitmap. createbitmap (originalimage, 0,
Height/2, width, height/2, matrix, false );
// Create a new bitmap with same width but taller to fit reflection
Bitmap bitmapwithreflection = bitmap. createbitmap (width,
(Height + height/2), config. argb_8888 );
// create a new canvas with the bitmap that's big enough for
// The image plus gap plus reflection
canvas = new canvas (bitmapwithreflection);
// draw in the original image
canvas. drawbitmap (originalimage, 0, 0, null);
// draw in the gap http://www.cnblogs.com/tankaixiong/ (original)
paint defaultpaint = new paint ();
canvas. drawrect (0, height, width, height + reflectiongap, defaultpaint);
// draw in the reflection
canvas. drawbitmap (reflectionimage, 0, height + reflectiongap, null);
// Create a shader that is a linear gradient that covers the reflection
Paint paint = new paint ();
Lineargradient shader = new lineargradient (0,
Originalimage. getheight (), 0, bitmapwithreflection. getheight ()
+ Reflectiongap, 0x70ffffff, 0x00ffffff, tilemode. Clamp );
// Set the paint to use this shader (linear gradient)
Paint. setshader (shader );
// Set the transfer mode to be Porter Duff and destination in
Paint. setxfermode (New porterduxfermode (mode. dst_in ));
// Draw a rectangle using the paint with our linear gradient
Canvas. drawrect (0, height, width, bitmapwithreflection. getheight ()
+ Reflectiongap, paint );
Return bitmapwithreflection;
}
// Converts the drawable type to bitmap
Public static bitmap drawabletobitmap (drawable ){
Bitmap bitmap = bitmap
. Createbitmap (
Drawable. getintrinsicwidth (),
Drawable. getintrinsicheight (),
Drawable. getopacity ()! = Pixelformat. opaque? Bitmap. config. argb_8888
: Bitmap. config. rgb_565 );
Canvas canvas = new canvas (Bitmap );
// Canvas. setbitmap (Bitmap );
Drawable. setbounds (0, 0, drawable. getintrinsicwidth (), drawable
. Getintrinsicheight ());
Drawable. Draw (canvas );
Return bitmap;
}
}
Step 5:
Rewrite Garry to achieve cascade effect.
Package com. Android. tank;
Import Android. content. context;
Import Android. Graphics. camera;
Import Android. Graphics. matrix;
Import Android. util. attributeset;
Import Android. util. log;
Import Android. View. view;
Import Android. View. animation. transformation;
Import Android. widget. Gallery;
Import Android. widget. imageview;
// Customize the gallery
Public class coverflow extends gallery {
Private camera mcamera = new camera ();
Private int mmaxrotationangle = 50;
Private int mmaxzoom =-500;
Private int mcoveflowcenter;
Private Boolean malphamode = true;
Private Boolean mcirclemode = false;
Public coverflow (context ){
Super (context );
This. setstatictransformationsenabled (true );
}
Public coverflow (context, attributeset attrs ){
Super (context, attrs );
This. setstatictransformationsenabled (true );
}
Public coverflow (context, attributeset attrs, int defstyle ){
Super (context, attrs, defstyle );
This. setstatictransformationsenabled (true );
}
Public int getmaxrotationangle (){
Return mmaxrotationangle;
}
Public void setmaxrotationangle (INT maxrotationangle ){
Mmaxrotationangle = maxrotationangle;
}
Public Boolean getcirclemode (){
Return mcirclemode;
}
Public void setcirclemode (Boolean iscircle ){
Mcirclemode = iscircle;
}
Public Boolean getalphamode (){
Return malphamode;
}
Public void setalphamode (Boolean isalpha ){
Malphamode = isalpha;
}
Public int getmaxzoom (){
Return mmaxzoom;
}
Public void setmaxzoom (INT maxzoom ){
Mmaxzoom = maxzoom;
}
Private int getcenterofcoverflow (){
Return (getwidth ()-getpaddingleft ()-getpaddingright ()/2
+ Getpaddingleft ();
}
Private Static int getcenterofview (view ){
Return view. getleft () + view. getwidth ()/2;
}
// Rewrite the garray method to generate cascade and zoom-in effects.
@ Override
Protected Boolean getchildstatictransformation (view child, transformation T ){
Final int childcenter = getcenterofview (child );
Final int childwidth = Child. getwidth ();
Int rotationangle = 0;
T. Clear ();
T. settransformationtype (transformation. type_matrix );
If (childcenter = mcoveflowcenter ){
Transformimagebitmap (imageview) Child, T, 0, 0 );
} Else {
Rotationangle = (INT) (float) (mcoveflowcenter-childcenter)/childwidth) * mmaxrotationangle );
// Log. D ("test", "recanglenum:" + math. Floor (mcoveflowcenter-
// Childcenter)/childwidth ));
If (math. Abs (rotationangle)> mmaxrotationangle ){
Rotationangle = (rotationangle <0 )? -Mmaxrotationangle
: Mmaxrotationangle;
}
Transformimagebitmap (imageview) Child, T, rotationangle,
(INT) math. Floor (mcoveflowcenter-childcenter)/(childwidth = 0? 1: childwidth )));
}
Return true;
}
/**
* This is called during layout when the size of this view has changed. If
* You were just added to the view hierarchy, you're called with the old
* Values of 0.
* Http://www.cnblogs.com/tankaixiong/ (original)
* @ Param W
* Current width of this view.
* @ Param H
* Current height of this view.
* @ Param oldw
* Old width of this view.
* @ Param oldh
* Old height of this view.
*/
Protected void onsizechanged (int w, int H, int oldw, int oldh ){
Mcoveflowcenter = getcenterofcoverflow ();
Super. onsizechanged (W, H, oldw, oldh );
}
/**
* Transform the image bitmap by the angle passed
* Http://www.cnblogs.com/tankaixiong/ (original)
* @ Param imageview
* Imageview the imageview whose bitmap we want to rotate
* @ Param t
* Transformation
* @ Param rotationangle
* The angle by which to rotate the bitmap
*/
Private void transformimagebitmap (imageview child, transformation T,
Int rotationangle, int d ){
Mcamera. Save ();
Final matrix imagematrix = T. getmatrix ();
Final int imageheight = Child. getlayoutparams (). height;
Final int imagewidth = Child. getlayoutparams (). width;
Final int rotation = math. Abs (rotationangle );
Mcamera. Translate (0.0f, 0.0f, 100366f );
// As the angle of the view gets less, zoom in
If (rotation <= mmaxrotationangle ){
Float zoomamount = (float) (mmaxzoom + (rotation * 1.5 ));
Mcamera. Translate (0.0f, 0.0f, zoomamount );
If (mcirclemode ){
If (rotation <40)
Mcamera. Translate (0.0f, 155, 0.0f );
Else
Mcamera. Translate (0.0f, (255-rotation * 2.5f), 0.0f );
}
If (malphamode ){
(Imageview) (child). setalpha (INT) (255-rotation * 2.5 ));
}
}
Mcamera. rotatey (rotationangle );
Mcamera. getmatrix (imagematrix );
Imagematrix. pretranslate (-(imagewidth/2),-(imageheight/2 ));
Imagematrix. posttranslate (imagewidth/2), (imageheight/2 ));
Mcamera. Restore ();
}
}
Finally, the presentation layer:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Gallery Android: Id = "@ + ID/Gallery" Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" Android: layout_margintop = "30dp"/>
<Imageswitcher Android: Id = "@ + ID/imageswitcher"
Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"
Android: layout_margintop = "30dp"/>
</Linearlayout>
Well, that's probably the case. It took me a lot of time to sort out so many things and post them. I hope it will help you!
Here we provide the source code download address (please respect tank's painstaking efforts ):
Http://files.cnblogs.com/tankaixiong/MyApplicationMenu.rar