Implementation of the "Android UI" case 01Cover flow3d effect

Source: Internet
Author: User

This example mainly introduces the implementation of COVERFLOW3D. Let's start by introducing cover Flow. Cover flow is Apple's first way to display the cover of multiple songs in the form of a GUI.

This case is excerpted from the network http://www.cnblogs.com/yyyyy5101/archive/2011/12/14/2287871.html , the case in the real machine test, there are differences in the display effect, so that the demonstration project there is a certain compatibility problem, to browse this article of people with lofty ideals, can be corrected and modified. Finally, although different machines show a difference, but the display effect is really good.
"Reproduced use, please specify the source: http://blog.csdn.net/mahoking "
The demonstration engineering design for this case three class files are: Activity (coverflowactivity), Gallery (Galleryflow), Baseadapter (Imageadapter), and layout file activity_02_ Gallery.xml.

Coverflowactivity

Import Android.app.activity;import Android.os.bundle;public class Coverflowactivity extends Activity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_02_gallery); integer[] images = {R.DRAWABLE.IMAGE01, r.drawable.image02,r.drawable.image03};i Mageadapter adapter = new Imageadapter (this, images); Adapter.createreflectedimages (); Galleryflow Galleryflow = (galleryflow) Findviewbyid (R.id.activity_01_galleryflow); Galleryflow.setadapter (adapter) ;}}

Gallery is a browse image of Android components (not just pictures, can be used adapter), Gallery Chinese meaning is also the meaning of the gallery, that is, the picture line, and then manually swipe to read. This article is just a primer, and if you want to learn more about gallery, you need to know it yourself.

       galleryflow  Gallery during the drag picture, The Getchildstatictransformation method is constantly called, in which the z-axis rotation is implemented with the camera.

Import Android.content.context;import Android.graphics.camera;import Android.graphics.matrix;import Android.util.attributeset;import Android.view.view;import Android.view.animation.transformation;import Android.widget.gallery;import Android.widget.imageview;public class Galleryflow extends Gallery {private Camera Mcamera = new Camera ();p rivate int mmaxrotationangle = 60;private int mmaxzoom = -120;private int mcoveflowcenter;public G Alleryflow (Context context) {super (context); this.setstatictransformationsenabled (true);} Public Galleryflow (context context, AttributeSet Attrs) {Super (context, attrs); This.setstatictransformationsenabled ( true);} Public Galleryflow (context 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 int Getmaxzoom () {return mmaxzoom;} Public Void setmaxzoom (int maxzoom) {mmaxzoom = Maxzoom;} private int Getcenterofcoverflow () {return (GetWidth ()-Getpaddingleft ()-getpaddingright ())/Getpaddingleft ();} private static int Getcenterofview (view view) {return view.getleft () + view.getwidth ()/2;} @Overrideprotected 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);} else {rotationangle = (int) (((float) (mcoveflowcenter-childcenter)/childwidth) * mmaxrotationangle); if (Math.Abs (rota Tionangle) > Mmaxrotationangle) {rotationangle = (RotationAngle < 0)?-mmaxrotationangle:mmaxrotationangle;} Transformimagebitmap ((ImageView) child, T, RotationAngle);} return true;} @Overrideprotected void onsizechanged (int w, int h, int oldw, int oldh) {Mcoveflowcenter = Getcenterofcoverflow (); Super.onsizechanged (W, H, OLDW, OLDH);} private void Transformimagebitmap (ImageView child, Transformation t,int rotationangle) {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);//on the z axis the camera is moving from the perspective, the actual effect is to enlarge the image. If you move on the y-axis, the picture moves up or down, and the image moves left and right on the x-axis. Mcamera.translate (0.0f, 0.0f, 100.0f);//As the angle of the view gets less, zoom inif (Rotation < Mmaxrotationangle) { float Zoomamount = (float) (Mmaxzoom + (rotation * 1.5)); Mcamera.translate (0.0f, 0.0f, Zoomamount);} Rotate on the y-axis and flip the picture vertically in the opposite direction. If rotated on the x-axis, the corresponding picture is flipped sideways. Mcamera.rotatey (RotationAngle); Mcamera.getmatrix (Imagematrix); Imagematrix.pretranslate (-(IMAGEWIDTH/2),-( IMAGEHEIGHT/2)); Imagematrix.posttranslate ((IMAGEWIDTH/2), (IMAGEHEIGHT/2)); Mcamera.restore ();}}

imageadapter

Import Android.content.context;import Android.content.res.resources;import Android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.lineargradient;import Android.graphics.matrix;import Android.graphics.paint;import Android.graphics.porterduff.mode;import Android.graphics.porterduffxfermode;import Android.graphics.shader.tilemode;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.imageview;public class Imageadapter extends BaseAdapter {int MGaller    Yitembackground;    Private Context Mcontext;    Private integer[] Mimageids;     Private imageview[] mimages;        Public Imageadapter (Context C, integer[] imageids) {mcontext = C;        Mimageids = Imageids;    Mimages = new Imageview[mimageids.length];        } public boolean createreflectedimages () {final int reflectiongap = 4;         int index = 0; For(int imageid:mimageids)            {Bitmap originalimage = Bitmapfactory.decoderesource (mcontext. Getresources (), imageId);            int width = originalimage.getwidth ();             int height = originalimage.getheight ();            Matrix matrix = new Matrix ();            Matrix.prescale (1,-1); Bitmap reflectionimage = Bitmap.createbitmap (originalimage, 0, HEIGHT/2, width, height/2, Matrix, F            Alse);            Bitmap bitmapwithreflection = Bitmap.createbitmap (width, (height + height/2), config.argb_8888);            Canvas canvas = new canvas (bitmapwithreflection);            Canvas.drawbitmap (originalimage, 0, 0, NULL);            Paint Deafaultpaint = new paint ();            Canvas.drawrect (0, height, width, height + reflectiongap, deafaultpaint);            Canvas.drawbitmap (reflectionimage, 0, height + reflectiongap, null);            Paint paint = new paint (); Lineargradient shader = new LinearGradient (0, Originalimage. GetHeight (), 0, Bitmapwithreflection.getheight ()            + Reflectiongap, 0X70FFFFFF, 0X00FFFFFF, Tilemode.clamp);            Paint.setshader (shader);            Paint.setxfermode (New Porterduffxfermode (mode.dst_in));            Canvas.drawrect (0, height, width, bitmapwithreflection.getheight () + reflectiongap, paint);            ImageView ImageView = new ImageView (mcontext);            Imageview.setimagebitmap (bitmapwithreflection); Imageview.setlayoutparams (New Galleryflow.layoutparams (180*2, 240*2));//Imageview.setlayoutparams (new GalleryF Low.            Layoutparams (180*4, 240*4));//Imageview.setscaletype (Scaletype.matrix);        mimages[index++] = ImageView;    } return true;    } private Resources Getresources () {//TODO auto-generated method stub return null;    } public int GetCount () {return mimageids.length;} public Object GetItem (int position) {return position;    } public long Getitemid (int position) {return position;    Public View GetView (int position, View Convertview, ViewGroup parent) {return mimages[position]; } Public Float Getscale (boolean focused, int offset) {return Math.max (0, 1.0f/(float) Math.pow (2, Math.Abs (o    (Ffset))); } }


Activity_02_gallery.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <com.mahaochen.app.uisharing.example02.galleryflow         android:layout_width= " Match_parent "        android:layout_height=" match_parent "        android:id=" @+id/activity_01_galleryflow "/></ Linearlayout>

Demo Effect:



Implementation of the "Android UI" case 01Cover flow3d effect

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.