Android's improved version of the Coverflow effect control

Source: Internet
Author: User
Tags config final reflection

Recently studied how to implement the Coverflow effect on Android control, in fact, as early as 2010, Neil Davies developed and open up this control, Neil Shen's blog address http://www.inter-fuser.com/ 2010/02/android-coverflow-widget-v2.html. The first is to read the source code, to understand the core ideas, wrote the control again, and added a detailed comment for later inspection, and in the course of use, found that there are two points can be improved: (1) The original picture is located in the middle, left half empty space, more ugly, can be repeated scrolling show, (2) because the picture at the beginning need to load out, so the memory cost is big, very easy to oom, need to compress the memory space of the picture.

This custom control consists of 4 parts for creating and providing imageadapter of picture objects, calculating picture rotation angle and so on custom control galleryflow, compressing sample rate parsing Bitmap tool class Bitmapscaledownutil, And the gallery3dactivity that hosts the custom control.

The first is Imageadapter, the code is as follows:

Package pym.test.gallery3d.widget;
Import Pym.test.gallery3d.util.BitmapScaleDownUtil;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.Bitmap.Config;
Import Android.graphics.Canvas;
Import android.graphics.LinearGradient;
Import Android.graphics.Matrix;
Import Android.graphics.Paint;
Import Android.graphics.PaintFlagsDrawFilter;
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.Gallery;
    
Import Android.widget.ImageView; 
/** * @author pengyiming * @date 2013-9-30 * @function Galleryflow Adapter/public class Imageadapter extends
    {/* Data section BEGIN/* Private final String TAG = "Imageadapter";
        
    Private context Mcontext;
    Picture array private int[] mimageids;
    Picture control array Private imageview[] mimages; Picture Control LayoutparAMS Private Galleryflow.layoutparams Mimageslayoutparams; /* Data segment End/////////////////////////(context, int[] imageids) imageadapter
        Text
        Mimageids = Imageids;
        Mimages = new Imageview[mimageids.length]; Mimageslayoutparams = new Galleryflow.layoutparams (Gallery.LayoutParams.WRAP_CONTENT, Gallery.LayoutParams.WRAP_
    CONTENT); 
     /** * @function Create the bitmap to be drawn according to the specified width and draw to the ImageView control * @param imagewidth * @param imageheight
        * @return void */public void createimages (int imagewidth, int imageheight) {//original image and reflection spacing 5px
            
        Final int gapheight = 5;
        int index = 0;            for (int imageid:mimageids) {/* Step1 sampling method to parse the original image and generate a reflection *//parse the original image, generate the original Bitmap object//
            Bitmap originalimage = Bitmapfactory.decoderesource (Mcontext.getresources (), imageID); Bitmap originalimage = bitmapscaledownutil.decodEsampledbitmapfromresource (Mcontext.getresources (), imageID, ImageWidth, imageheight);
            int width = originalimage.getwidth ();
                
            int height = originalimage.getheight ();
            The y-axis is reversed, in essence, the x-axis flip Matrix matrix = new Matrix ();
            Matrix.setscale (1,-1); And only take the bottom half of the original image to create a reflection Bitmap object Bitmap reflectionimage = Bitmap.createbitmap (originalimage, 0, HEIGHT/2, width, hei
                
            GHT/2, Matrix, false); /* STEP2 Draw///Create a new graph that can contain the original image + Spacing + Reflection Bitmap Object Bitmap bitmapwithreflection = Bitmap.createbitmap (wid
            Th, (height + gapheight + height/2), config.argb_8888);
            Create a canvas on top of the new Diagram Bitmap object Canvas Canvas = new Canvas (bitmapwithreflection);
            Anti-aliasing effect Canvas.setdrawfilter (new Paintflagsdrawfilter (0, Paint.anti_alias_flag));
            Draw the original Canvas.drawbitmap (originalimage, 0, 0, NULL); Draw Spacing Paint Gappaint = new Paint ();
            Gappaint.setcolor (0xFFCCCCCC);
            Canvas.drawrect (0, height, width, height + gapheight, gappaint);
                
            Draw Reflection Canvas.drawbitmap (reflectionimage, 0, height + gapheight, null);
            /* STEP3 render///create a linear gradient renderer for rendering reflection Paint Paint = new Paint (); LinearGradient shader = new LinearGradient (0, height, 0, (height + gapheight + height/2), 0x70ffffff, 0X00FFFFFF, Tilemo De.
            CLAMP);
            Sets the brush renderer Paint.setshader (shader);
            Set picture blending mode Paint.setxfermode (new Porterduffxfermode (mode.dst_in));
                
            Rendering reflection + Spacing Canvas.drawrect (0, height, width, (height + gapheight + height/2), paint);
            /* Step4 the ImageView control to draw * * ImageView ImageView = new ImageView (mcontext);
            Imageview.setimagebitmap (bitmapwithreflection); Imageview.setlayoutparams (Mimageslayoutparams);
                
            Play Log Imageview.settag (index);
            /* STEP5 Release Heap * * Originalimage.recycle ();
Reflectionimage.recycle ();
                
            Bitmapwithreflection.recycle ();
        mimages[index++] = ImageView;
    @Override public int GetCount () {return integer.max_value;
    @Override public Object getitem (int position) {return mimages[position];
    @Override public long getitemid (int position) {return position; @Override public View getview (int position, View Convertview, ViewGroup parent) {/MI
    Mages[position% mimages.length]; }/* Function segment End/}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

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.