Android 3D Gallery and Horizontalscrollview replacement gallery

Source: Internet
Author: User
Tags deprecated

Because gallery creates a new view each time the picture is switched, it causes too much wasted resources. So it has been deprecated (Deprecated) and can be replaced with Horizontalscrollview. Today contrasts the differences between the following traditional gallery and Horizontalscrollview. Check out the demo:

A 3D Gallery with a shadow is added, but the slide effect is not very good.


Here is the core code, and all the code can be viewed on my github. Link at the end of the article.

Package Com.zms.simplegallery;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.support.v7.app.actionbaractivity;import Android.os.Bundle;import Android.util.log;import Android.view.gravity;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.viewgroup;import Android.view.viewgroup.layoutparams;import Android.widget.adapterview;import Android.widget.baseadapter;import Android.widget.gallery;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.textview;import Android.widget.toast;import Java.io.file;public class Main extends Actionbaractivity {/** * General Gallery */PR    Ivate Gallery Gallerynormal;    /** * Cycle Gallery */private Gallery galleryloop;    /** * 3D Gallery */private Gallery3d Gallery3d;    /** * Horizontalscrollview Gallery */private static final String tag = "GH"; private static FINAL String path_sdcard = "/mnt/sdcard";    Private LinearLayout hsvgallery;    Private LinearLayout HSVGALLERYFROMSD;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);        Gallerynormal = (Gallery) Findviewbyid (r.id.gallerynormal);        Gallerynormal.setadapter (New Imageadapter (this)); Gallerynormal.setspacing (5);        Entry Spacing Gallerynormal.setonitemclicklistener (new Myitemonclicklistener ());        Galleryloop = (Gallery) Findviewbyid (R.id.galleryloop);        Galleryloop.setadapter (New Loopimageadapter (this));        Galleryloop.setspacing (5);        Galleryloop.setonitemclicklistener (New Myitemonclicklistener ()); integer[] Images = {r.drawable.pic_1, r.drawable.pic_2, R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_        5, R.drawable.pic_6, r.drawable.pic_7, r.drawable.pic_8}; Gallery3dadapter adapter = new Gallery3dadapter (This, IMages); Adapter.createreflectedimages ();        Create a reflection effect Gallery3d = (Gallery3d) This.findviewbyid (R.id.gallery3d);        Gallery3d.setfadingedgelength (0);        Gallery3d.setspacing (-100);        Gallery3d.setadapter (adapter);        Gallery3d.setonitemclicklistener (New Myitemonclicklistener ());        Gallery3d.setselection (4);        Hsvgallery = (linearlayout) Findviewbyid (r.id.hsvgallery);        HSVGALLERYFROMSD = (linearlayout) Findviewbyid (R.ID.HSVGALLERYFROMSD); Pic In the drawable integer[] Images2 = {r.drawable.pic_1, r.drawable.pic_2, R.drawable.pic_3, R.D Rawable.pic_4, R.drawable.pic_5, R.drawable.pic_6, r.drawable.pic_7, R.drawable.pic_8, R.dra        Wable.pic_9};        for (Integer id:images2) {Hsvgallery.addview (Insertimage (id));        }//pic in the sdcard String TargetPath = path_sdcard + "/dcim/camera/";        File Targetdirector = new file (TargetPath); File[] Files= Targetdirector.listfiles ();            if (Files! = null) {LOG.D (tag, files.length + "");            for (File file:files) {Hsvgalleryfromsd.addview (Insertphoto (File.getabsolutepath ()));        }}} Private class Myitemonclicklistener implements Adapterview.onitemclicklistener {@Override            public void Onitemclick (adapterview<?> parent, view view, int position, long ID) {            TextView tvhint = (TextView) Findviewbyid (r.id.tvhint);                if (view = = gallerynormal) {int num = position + 1;            Tvhint.settext ("General gallery: clicked" + num);                } else if (view = = galleryloop) {int num = (position + 1)% 9;                if (num = = 0) {num = 9;            } tvhint.settext ("Loop gallery: Clicked" + num);                } else if (view = = gallery3d) {int num = position + 1; Tvhint.settexT ("3D Gallery: Clicked" + num);        }}}//ordinary Gallery adapter class Imageadapter extends Baseadapter {private context context;        Public Imageadapter (Context context) {This.context = context; } private integer[] Imageinteger = {r.drawable.pic_1, r.drawable.pic_2, R.drawable.pic_3, r.drawable . Pic_4, R.drawable.pic_5, R.drawable.pic_6, r.drawable.pic_7, R.drawable.pic_8, R.DRAWABLE.P        Ic_9};        Public Object getItem (int position) {return position;        } public long Getitemid (int position) {return position;        }//General gallery and Loop Gallery, this function returns a value different from public int GetCount () {return imageinteger.length; Public View GetView (int position, View Convertview, ViewGroup parent) {ImageView ImageView = new Imag            Eview (context);            Imageview.setimageresource (Imageinteger[position]); Imageview.setscAletype (ImageView.ScaleType.FIT_XY); Display scale type Imageview.setlayoutparams (new Gallery.layoutparams (100, 100));        Picture size return imageView;        }}//Loop Gallery Adapter class Loopimageadapter extends Baseadapter {private context context;        Public Loopimageadapter (Context context) {This.context = context; } private integer[] Imageinteger = {r.drawable.pic_1, r.drawable.pic_2, R.drawable.pic_3, r.drawable . Pic_4, R.drawable.pic_5, R.drawable.pic_6, r.drawable.pic_7, R.drawable.pic_8, R.DRAWABLE.P        Ic_9};        Public Object getItem (int position) {return position;        } public long Getitemid (int position) {return position;        } public int GetCount () {return integer.max_value; Public View GetView (int position, View Convertview, ViewGroup parent) {ImageView ImageView = new Imag      Eview (context);      Imageview.setimageresource (imageinteger[position% imageinteger.length]);            Imageview.setscaletype (ImageView.ScaleType.FIT_XY);            Imageview.setlayoutparams (New Gallery.layoutparams (100, 100));        return imageView;        }} private View insertimage (Integer ID) {linearlayout layout = new LinearLayout (Getapplicationcontext ());        Layout.setlayoutparams (New Layoutparams (320, 320));        Layout.setgravity (Gravity.center);        ImageView ImageView = new ImageView (Getapplicationcontext ());        Imageview.setlayoutparams (New Layoutparams (300, 300));        Imageview.setbackgroundresource (ID);        Layout.addview (ImageView);    return layout; } private View Insertphoto (String absolutepath) {//TODO auto-generated method stub Bitmap BM = Decodesa        Mplebitmapfromuri (Absolutepath, 200, 200);        LinearLayout layout = new LinearLayout (Getapplicationcontext ()); Layout.setlayoutparams (New Layoutparams (250, 250));        Layout.setgravity (Gravity.center);        ImageView ImageView = new ImageView (Getapplicationcontext ());        Imageview.setlayoutparams (New Layoutparams (220, 220));        Imageview.setscaletype (ImageView.ScaleType.CENTER_CROP);        Imageview.setimagebitmap (BM);        Layout.addview (ImageView);    return layout; } private Bitmap Decodesamplebitmapfromuri (String absolutepath, int reqwidth, int reqheight) {//TODO Auto-gene        Rated method stub Bitmap BM = null; First decode with injustdecodebounds=true to check dimensions final bitmapfactory.options Options = new BITMAPFA Ctory.        Options ();        Options.injustdecodebounds = true;        Bitmapfactory.decodefile (Absolutepath, Options);        Calculate insamplesize options.insamplesize = calculateinsamplesize (options, Reqwidth, reqheight);        Decode bitmap with insamplesize set options.injustdecodebounds = false; BM = Bitmapfactory.decodefile (absolutepath, Options);    return BM; } private int Calculateinsamplesize (bitmapfactory.options Options, int reqwidth, I NT Reqheight) {//TODO auto-generated Method Stub//Raw height and width of image final int height        = Options.outheight;        Final int width = options.outwidth;        int insamplesize = 1; if (Height > Reqheight | | width > reqwidth) {if (width > height) {insamplesize = Math            . Round (float) height/(float) reqheight);            } else {insamplesize = Math.Round (float) width/(float) reqwidth);    }} return insamplesize; } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio        n Bar if it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//HanDLE Action Bar Item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }}

Reprint Please specify source: Zhou Mushi's CSDN blogHttp://blog.csdn.net/zhoumushui 
my github: Zhou Mushi's GitHubHttps://github.com/zhoumushui

Android 3D Gallery and Horizontalscrollview replacement gallery

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.