Android custom control-snkelayout (like Gallery)

Source: Internet
Author: User

Author: Wallace Wang Email: wallage@qq.com

Disclaimer: This article is my original work and is only for learning and use.

Reprinted address:

Http://wallage.blog.163.com/blog/static/1738962420108211120850/

Brief Introduction: I believe most people who have used the android gallery control love and hate the gallery control. The Gallery animation works well and is very practical, but there are many restrictions, in terms of layout, gallery can only be placed horizontally. To use vertical Gallery, unless you want to override Gallery. In this article, the snkelayout inherits from framelayout. You can customize the positions of multiple imageviews (greater than or equal to 3) in the snkelayout, and assign the specified ID to the defined imageview; after simple initialization in the main file, you can drag the defined imageview like Gallery, and move it as continuously as a snake. It can not only be dragged horizontally, but also vertically, it can also be dragged in oblique ways, or even circled.

As shown in the following figure (the length and width of the android virtual machine are 800*600 ):

 

(Test version) code: Package com. Snake ;/*
* Author: Wallace Wang
* Email: wallage@qq.com
*/
Import java. util. arraylist;
Import java. util. date;
Import java. util. List; import Android. content. context;
Import Android. content. res. typedarray;
Import Android. Graphics. Bitmap;
Import Android. Graphics. bitmapfactory;
Import Android. Graphics. Canvas;
Import Android. Graphics. matrix;
Import Android. Graphics. paint;
Import Android. Graphics. rect;
Import Android. util. attributeset;
Import Android. util. log;
Import Android. View. gesturedetector;
Import Android. View. motionevent;
Import Android. View. view;
Import Android. View. gesturedetector. ongesturelistener;
Import Android. widget. framelayout;
Import Android. widget. imageview; public class snkelayout extends framelayout {
Private Static final string log_tag = "snkelayout ";
Private gesturedetector mgesturedetector;
Private snkeongesturelistener mgesturelistener;
Private list <View> viewholder;
Private int selectimg;
Private int totalviewnum;
Private view mcontentview;
Private snkeview scrollview;
Private context mcontext;
Private Enum state {
About_to_animate,
Animating,
Animate_end,
Ready,
Tracking
};
Private State mstate;
Private double anistartpos; // value = scrollnum + percent * direction;
Private double anistoppos; // value = scrollnum + percent * direction;
Private date anistarttime;
Private long anitime = 1000;
Private double anispeed= 500;
Private double anidefg = 5;
 
Private int mcontentwidth = 0;
Private int mcontentheight = 0;
Private int clickitem =-1;
Private int direction = 0;
Private int movdirection = 0;
Private double percent = 0;
Private int scrollnum = 0; private pathscale mypathviews; private list <bitmap> BMP recviews;
Private onselectlistener selectlistener;
Private onclicklistener clicklistener;
Private int currentindex = 0; Public snkelayout (context, attributeset attrs ){
Super (context, attrs );
Log. D (log_tag, "init snake layout ");
Mcontext = context;
Typedarray A = context. obtainstyledattributes (attrs,
R. styleable. snkelayout );
Selectimg = A. getinteger (R. styleable. snkelayout_selectimg,-1 );
A. Recycle (); mgesturelistener = new snkeongesturelistener ();
Mgesturedetector = new gesturedetector (mgesturelistener );
Mgesturedetector. setislongpressenabled (false );
BMP recviews = new arraylist <bitmap> ();
Mypathviews = new pathscale ();
Mstate = state. ready;
}
Public void Init (){
For (INT I = 0; I <totalviewnum; I ++ ){
Imageview v = (imageview) viewholder. Get (I );
V. setscaletype (imageview. scaletype. fit_xy );
V. setimagebitmap (BMP recviews. Get (I + currentindex) % BMP recviews. Size ()));
}
}
 
Public void addbitmap (Bitmap B ){
If (B! = NULL)
BMP recviews. Add (B );
}
 
Public void addbitmap (Bitmap B, int position ){
If (B! = NULL)
BMP recviews. Add (Position, B );
}
 
Public void addrec (int rec ){
Bitmap B = bitmapfactory. decoderesource (this. getresources (), REC );
If (B! = NULL)
BMP recviews. Add (B );
}
......
The code is too long. The res/values/attrs. xml file is omitted: <? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Declare-styleable name = "snkelayout">
<! -- Defines the special selected position image -->
<ATTR name = "selectimg" format = "integer"/>
</Declare-styleable>
</Resources> Res/values/ids. xml file <? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Item type = "ID" name = "Snail keimg0"/>
<Item type = "ID" name = "Snail keimg1"/>
<Item type = "ID" name = "Snail keimg2"/>
<Item type = "ID" name = "Snail keimg3"/>
<Item type = "ID" name = "Snail keimg4"/>
<Item type = "ID" name = "Snail keimg5"/>
<Item type = "ID" name = "Snail keimg6"/>
<Item type = "ID" name = "Snail keimg7"/>
<Item type = "ID" name = "Snail keimg8"/>
<Item type = "ID" name = "Snail keimg9"/>
<Item type = "ID" name = "Snail keimg10"/>
<Item type = "ID" name = "Snail keimg11"/>
<Item type = "ID" name = "Snail keimg12"/>
<Item type = "ID" name = "Snail keimg13"/>
<Item type = "ID" name = "Snail keimg14"/>
<Item type = "ID" name = "Snail keimg15"/>
<Item type = "ID" name = "Snail keimg16"/>
<Item type = "ID" name = "Snail keimg17"/>
<Item type = "ID" name = "Snail keimg18"/>
<Item type = "ID" name = "Snail keimg19"/>
<Item type = "ID" name = "Snail keimg20"/>
<Item type = "ID" name = "Snail keimg21"/>
<Item type = "ID" name = "Snail keimg22"/>
<Item type = "ID" name = "Snail keimg23"/>
<Item type = "ID" name = "Snail keimg24"/>
<Item type = "ID" name = "Snail keimg25"/>
<Item type = "ID" name = "Snail keimg26"/>
<Item type = "ID" name = "Snail keimg27"/>
<Item type = "ID" name = "Snail keimg28"/>
<Item type = "ID" name = "Snail keimg29"/>
<Item type = "ID" name = "Snail kecontent"/>
</Resources> main layout file: Main. xml <? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Xmlns: Snake = "http://schemas.android.com/apk/res/com.Snake"
Android: Orientation = "vertical" Android: Background = "@ drawable/BJ"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Com. Snake. snkelayout Android: layout_width = "fill_parent" Android: layout_weight = "1"
Android: Id = "@ + ID/my_snake" Android: layout_height = "fill_parent"
Snake: selectimg = "7">
<Linearlayout Android: layout_width = "fill_parent" Android: Orientation = "vertical"
Android: layout_height = "wrap_content" Android: Id = "@ ID/snail kecontent">
<Linearlayout Android: layout_width = "fill_parent" Android: paddingtop = "70dip"
Android: layout_height = "wrap_content">
<Textview Android: layout_width = "270dip" Android: layout_height = "1dip"/>
<Imageview Android: layout_width = "40dip" Android: Id = "@ ID/snkeimg0"
Android: layout_height = "40dip"/>
</Linearlayout>
<Linearlayout Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Textview Android: layout_width = "300dip" Android: layout_height = "1dip"/>
<Imageview Android: layout_width = "60dip" Android: Id = "@ ID/snail keimg1"
Android: layout_height = "60dip"/>
</Linearlayout>
<Linearlayout Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Textview Android: layout_width = "295dip" Android: layout_height = "1dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snail keimg2"
Android: layout_height = "50dip"/>
</Linearlayout>
<Linearlayout Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Textview Android: layout_width = "290dip" Android: layout_height = "1dip"/>
<Imageview Android: layout_width = "35dip" Android: Id = "@ ID/snail keimg3"
Android: layout_height = "35dip"/>
</Linearlayout>
<Linearlayout Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Textview Android: layout_width = "290dip" Android: layout_height = "1dip"/>
<Imageview Android: layout_width = "35dip" Android: Id = "@ ID/snail keimg4"
Android: layout_height = "35dip"/>
</Linearlayout>
<Linearlayout Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Textview Android: layout_width = "292dip" Android: layout_height = "1dip"/>
<Imageview Android: layout_width = "40dip" Android: Id = "@ ID/snkeimg5"
Android: layout_height = "40dip"/>
</Linearlayout>
<Linearlayout Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Textview Android: layout_width = "320dip" Android: layout_height = "1dip"/>
<Imageview Android: layout_width = "40dip" Android: Id = "@ ID/snail keimg6"
Android: layout_height = "40dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snkeimg7"
Android: layout_height = "55dip" Android: paddingtop = "15dip"
Android: paddingleft = "10dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snail keimg8"
Android: layout_height = "55dip" Android: paddingtop = "15dip"
Android: paddingleft = "10dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snail keimg9"
Android: layout_height = "55dip" Android: paddingtop = "15dip"
Android: paddingleft = "10dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snail keimg10"
Android: layout_height = "50dip" Android: paddingtop = "10dip"
Android: paddingleft = "10dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snail keimg11"
Android: layout_height = "40dip" Android: paddingleft = "10dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snail keimg12"
Android: layout_height = "40dip" Android: paddingleft = "10dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snail keimg13"
Android: layout_height = "50dip" Android: paddingleft = "10dip"
Android: paddingtop = "10dip"/>
</Linearlayout>
<Linearlayout Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Textview Android: layout_width = "260dip" Android: layout_height = "1dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snail keimg21"
Android: layout_height = "25dip"
Android: paddingleft = "25dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snail keimg20"
Android: layout_height = "55dip" Android: paddingtop = "20dip"
Android: paddingleft = "15dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snail keimg19"
Android: layout_height = "65dip" Android: paddingtop = "25dip"
Android: paddingleft = "10dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snail keimg18"
Android: layout_height = "65dip" Android: paddingtop = "25dip"
Android: paddingleft = "10dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snail keimg17"
Android: layout_height = "55dip" Android: paddingtop = "15dip"
Android: paddingleft = "10dip"/>
<Imageview Android: layout_width = "50dip" Android: Id = "@ ID/snail keimg16"
Android: layout_height = "45dip" Android: paddingtop = "5dip"
Android: paddingleft = "10dip"/>
<Imageview Android: layout_width = "45dip" Android: Id = "@ ID/snail keimg15"
Android: layout_height = "35dip" Android: paddingleft = "10dip"/>
<Imageview Android: layout_width = "45dip" Android: Id = "@ ID/snail keimg14"
Android: layout_height = "35dip" Android: paddingleft = "10dip"/>
</Linearlayout>
</Linearlayout>
</COM. Snake. snkelayout>
</Linearlayout> snake. Java file: Package com. Snake; import Android. App. activity;
Import Android. OS. Bundle; public class snake extends activity {
/** Called when the activity is first created .*/
Snkelayout msnake;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Msnake = (snkelayout) findviewbyid (R. Id. my_snake );
Msnake.addrec(r.drawable.png 1 );
Msnake.addrec(r.drawable.png 2 );
Msnake.addrec(r.drawable.png 3 );
Msnake.addrec(r.drawable.png 4 );
Msnake.addrec(r.drawable.png 5 );
Msnake.addrec(r.drawable.png 6 );
Msnake.addrec(r.drawable.png 7 );
Msnake.addrec(r.drawable.png 8 );
Msnake.addrec(r.drawable.png 9 );
Msnake.addrec(r.drawable.png 10 );
Msnake.addrec(r.drawable.png 11 );
Msnake.addrec(r.drawable.png 12 );
Msnake.addrec(r.drawable.png 13 );
Msnake.addrec(r.drawable.png 14 );
Msnake.addrec(r.drawable.png 15 );
Msnake.addrec(r.drawable.png 16 );
Msnake.addrec(r.drawable.png 17 );
Msnake.addrec(r.drawable.png 18 );
Msnake.addrec(r.drawable.png 19 );
Msnake.addrec(r.drawable.png 20 );
Msnake.addrec(r.drawable.png 21 );
Msnake.addrec(r.drawable.png 22 );
Msnake.addrec(r.drawable.png 23 );
Msnake.addrec(r.drawable.png 24 );
Msnake.addrec(r.drawable.png 25 );
Msnake.addrec(r.drawable.png 26 );
Msnake.addrec(r.drawable.png 27 );
Msnake.addrec(r.drawable.png 28 );
Msnake. INIT ();
}
}
Related Article

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.