Android Memory optimized package nine lattice

Source: Internet
Author: User

With more and more apps on the market online, a lot of software on the phone memory requirements is also very large, so we must be in the development of how to optimize the memory, to optimize their own apps as much as possible. Today we'll take a look at the optimization of the nine Gongge. The following is the software

1, in order to achieve better results we do not need to UITableView, first we want to customize a picture and text through Xib

2, the custom main view Jrmainscrollview, through the protocol proxy to realize the function, the practice and UITableView similar, everybody may refer to UITableView

First: We want to define the data source protocol

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >//Data source Protocol @protocol Jrmainscrolldatasource <nsobject>//get the total quantity-(Nsinteger) numberofitems;//get the number of columns-( Nsinteger) numberofcolumsofrow;//Get item-(UIView *) Mainscrollview: (Jrmainscrollview *) Mainscrollview ItemAtIndex: ( Nsinteger) index; @end </span>

Second: We want to define the attribute agreement

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >//Property Protocol @protocol jrmainscrolldelegate <NSObject> @optional//Get height-(cgfloat)  Heightforitematview: ( Jrmainscrollview *) mainscrollview;//Get width-(cgfloat)  Widthforitematview: (Jrmainscrollview *) mainscrollview;// Get spacing-(cgfloat) Mainscrollview: (Jrmainscrollview *) Mainscrollview Spaceforitemwithtype: (kjrmainscrollitemspace) type; @end </span>

Note The Get spacing includes the spacing to the left and right and the top and bottom spacing by defining an enumeration implementation

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >typedef enum{    kjrmainscrollitemleftspace,    kjrmainscrollitemtopspace} kjrmainscrollitemspace;</ Span>

3, the internal layout of the implementation, calculate all the current frame, and into the array during this time, the use of the attribute parameters need to be obtained from the agent, the code is as follows

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;"    >//Load Sub-view-(void) _loadsubviews{//Get total number and number of columns Nsinteger totalitems=[self.jrdatasource Numberofitems];    Nsinteger Colum=[self.jrdatasource Numberofcolumsofrow];    Get width and height cgfloat itemwidth=[self.jrdelegate widthforitematview:self];    CGFloat itemheigt=[self.jrdelegate heightforitematview:self];    Get up and down spacing cgfloat leftspace=[self.jrdelegate mainscrollview:self spaceforitemwithtype:kjrmainscrollitemleftspace];    CGFloat topspace=[self.jrdelegate mainscrollview:self Spaceforitemwithtype:kjrmainscrollitemtopspace];    CGFloat space= (kwidth-2*leftspace-colum*itemwidth)/(colum-1) +itemwidth;        for (int i=0;i<totalitems;i++) {int clo=i%colum;        int row=i/colum;        CGRect Frame=cgrectmake (Leftspace+clo*space, 20+row* (itemheigt+topspace), Itemwidth, itemHeigt);        [Self.array Addobject:[nsvalue Valuewithcgrect:frame]; }self.contentsize=cgsizemake (0, Cgrectgetmaxy ([[Self.array Lastobject] CgrectvaLue])); Self.showsverticalscrollindicator=no;} </span>

4, judge whether the current frame is within the current screen visual range, if you are in the rendering of the view, if not ignored.

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;"    >-(void) layoutsubviews{[Super Layoutsubviews];        Loop convenience Gets the frame for (int i=0;i<self.array.count;i++) in the screen {UIView * tempview= (UIView *) self.current[@ (i)];        CGRect Rect=[self.array[i] Cgrectvalue];  if ([self isinscreenwith:rect]) {if (!tempview) {////The dictionary does not need to be heavily loaded with new UIView *view=[self.jrdatasource                Mainscrollview:self ITEMATINDEX:I];                View.frame=rect;                [Self.current Setobject:view forkey:@ (i)];            [Self addsubview:view];            }else if (Tempview) {//If there is a dictionary and is not inside the line of sight then remove [self.current removeobjectforkey:@ (i)];            [Tempview Removefromsuperview];        [Self.pool Addobject:tempview]; The}//is judged not to be within the field of view, where there are two cases, the Y value is inside the screen, or the Maxy value is inside the screen-(BOOL) Isinscreenwith: (cgrect) frame{cgfloat setminy=self.contentof    Fset.y;        CGFloat setmaxy=self.contentoffset.y+kheight; BOOL Condition1=frame.origin.y>=setminy&&frame.origin.y<=setmaxy;    BOOL Condition2=cgrectgetmaxy (frame) >=setminy&&cgrectgetmaxy (frame) <=setMaxy; if (condition1| |    Condition2) {return YES;        } return NO; }</span>

5. Operation Buffer Pool Reuse objects

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >/** store frame*/@property (nonatomic,strong) Nsmutablearray * array;/** Store the object currently displayed */@property (Nonatomic,strong) Nsmutabledictionary * current;/** Storage Buffer Pool Object */@property (nonatomic,strong) nsmutableset * pool;/** *  GET REUSABLE objects * *  @param identy < #identy description#> * *  @return < #return value description#> */-(Jrrectview *) dequeuere Useditemwithidenty: (NSString *) identy{    Jrrectview * view=[self.pool Anyobject];    if (view!=nil) {        [Self.pool Removeobject:view];    }    return view;} </span>

6. Load the view in the host controller and implement the Proxy method

< Span style= "font-family:arial; Font-size:14px ">

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" >//Load all data-(void) _loadsubviews{//1 add scrolling view Jrmainscrollview * Mainscroll=[[jrmainscrollview Alloc] Initwithfram    E:self.view.bounds];    mainscroll.jrdatasource=self;    mainscroll.jrdelegate=self;    [Mainscroll Reloadviews]; [Self.view Addsubview:mainscroll];} #pragma mark-Data source Method-(Nsinteger) numberofitems{return 132;} -(Nsinteger) numberofcolumsofrow{return 3;} -(UIView *) Mainscrollview: (Jrmainscrollview *) Mainscrollview Itematindex: (nsinteger) index{Jrrectview *cell=[    Mainscrollview dequeuereuseditemwithidenty:@ "Test"];    if (cell==nil) {cell=[[[nsbundle mainbundle] loadnibnamed:@ "rect" Owner:nil Options:nil] lastobject];    } cell.titlelabel.text=[nsstring stringwithformat:@ "Download"];    NSString * imagename=[nsstring stringwithformat:@ "%d", Arc4random_uniform (20) +256];    UIImage *image=[uiimage Imagenamed:imagename];    Cell.image.image=image; return cell;} #pragma mark-Proxy method//Get height-(cgfloat) HEIGHTFOritematview: (Jrmainscrollview *) mainscrollview{return 100;} Get width-(cgfloat) Widthforitematview: (Jrmainscrollview *) mainscrollview{return 90;} Get spacing-(cgfloat) Mainscrollview: (Jrmainscrollview *) Mainscrollview Spaceforitemwithtype: (kjrmainscrollitemspace)    type{if (type==kjrmainscrollitemleftspace) {return 20;    }else if (type==kjrmainscrollitemtopspace) {return 20; } return 20;} </span>
7, memory optimization, in addition to the above 6 points, but also to understand the optimization of the source code , a key to clear log (development log) information, one-click Optimization to reduce the size of the original packet after encryption

Android Memory optimized package nine lattice

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.