Android Memory optimized package nine lattice
Last Update:2015-06-16
Source: Internet
Author: User
<span id="Label3"></p><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="font-size: 14px;">as more and more of the market</span> <span style="font-size: 14px;">APP</span> on- <span style="font-size: 14px;">line, A lot of software on the phone memory requirements are 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</span></p></p> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"></p></p> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="margin: 0px; padding: 0px; font-size: 14px;">1, in order to achieve better results we do not need to uitableview, first we want to customize a picture and text through Xib</span></p></p> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"></p></p> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="margin: 0px; padding: 0px; font-size: 14px;">2, the custom main view jrmainscrollview, through the protocol proxy to realize the function, the practice and UITableView similar, Everybody may refer to UITableView</span></p></p> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="margin: 0px; padding: 0px; font-size: 14px;">First: we want to define the data source protocol</span></p></p> <pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;"><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></pre></pre> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="font-size: 14px;"></span></p></p> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="margin: 0px; padding: 0px; font-size: 14px;">Second: we want to define the attribute agreement</span></p></p> <pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;"><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 pitch-( Cgfloat) mainscrollview: (jrmainscrollview *) mainscrollview spaceforitemwithtype: (kjrmainscrollitemspace) type; @end </span></pre></pre> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="font-size: 14px;"></span></p></p> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="margin: 0px; padding: 0px; font-size: 14px;">Note The Get spacing includes the spacing to the left and right and the top and bottom spacing by defining an enumeration implementation</span></p></p> <pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;"><span style= "font-family:arial;font-size:14px;" >typedef enum{kjrmainscrollitemleftspace, kjrmainscrollitemtopspace} kjrmainscrollitemspace;</span></pre></pre> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="font-size: 14px;"></span></p></p> <p style="margin-top: 4px; margin-bottom: 4px; padding: 2px 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal;"><p style="margin-top: 4px; margin-bottom: 4px; padding: 2px 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal;"><span style="font-size: 14px;"></span></p></p> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="margin: 0px; padding: 0px; font-size: 14px;">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></p></p> <pre class="brush:java;toolbar: true; auto-links: false;"><span style= "font-family:arial;font-size:14px;" >//load Child view- (void) _loadsubviews{ //gets The 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></pre> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><br><span style="font-size: 14px;"></span></p></p> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="margin: 0px; padding: 0px; font-size: 14px;">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></p></p> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><br></p></p> <pre class="brush:java;toolbar: true; auto-links: false;"><span style= "font-family:arial;font-size:14px;" >-(void) layoutsubviews{ [super layoutsubviews]; // Recycling facilitates access to 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 heavy new loading uiview *view=[self.jrdatasource mainscrollview :self itematindex:i]; view.frame=rect; [self.current setobject:view forkey:@ (i)]; [self addsubview:view]; } }else if (tempview) {//remove If there is a dictionary and is not inside the line of sight [self.current removeobjectforkey:@ (i)]; [tempView removeFromSuperview]; [self.pool addObject:tempView]; } }//judgment is not 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.contentOffset.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></pre> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="font-size: 14px;"></span></p></p> <p style="margin-top: 4px; margin-bottom: 4px; padding: 2px 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal;"><p style="margin-top: 4px; margin-bottom: 4px; padding: 2px 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal;"><span style="font-size: 14px;"></span></p></p> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="margin: 0px; padding: 0px; font-size: 14px;">5. Operation Buffer Pool Reuse objects</span></p></p> <pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar:true; auto-links:false; ><span style=" font-family:arial;font-size : 14px; " > ** store frame* @property (nonatomic,strong) nsmutablearray * array; store the object currently displayed * @ property nsmutabledictionary * current; buffer pool object (nonatomic, strong) nsmutableset * pool; ** * get reusable objects * * @param identy < #identy description#> * * @return < #return value description#> * - (jrrectview *) dequeuereuseditemwithidenty: (nsstring *) identy{ jrrectview * view="[self.pool anyObject]; " if (view!="nil)" { [self.pool removeobject:view]; } return view;} < span> < pre></pre></pre> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="font-size: 14px;"></span></p></p> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="margin: 0px; padding: 0px; font-size: 14px;">6. Load the view in the host controller and implement the Proxy method</span></p></p> <pre class="brush:java;toolbar: true; auto-links: false;"><span style= "font-family:arial;font-size:14px;" > //load all data- (void) _loadSubviews{ //1 add scrolling view jrmainscrollview * mainscroll=[[jrmainscrollview alloc] initwithframe: 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 - agent Method//get height- (cgfloat) heightforitematview: (jrmainscrollview *) mainscrollview{ return 100;} Get width- (cgfloat) widthforitematview: (jrmainscrollview *) mainScrollView{ return 90;} Get pitch- (cgfloat) mainscrollview: (jrmainscrollview *) mainscrollview spaceforitemwithtype: ( Kjrmainscrollitemspace) type{ if (type==kjrmainscrollitEmleftspace) { return 20; } else if (type==kjrmainscrollitemtopspace) { return 20; } return 20;} </span></pre> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="font-size: 14px;"></span><br></p></p> <p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><p style="margin-bottom: 15px; padding: 0px; white-space: normal; background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; margin-top: 0px !important;"><span style="font-size: 14px;">7, Memory optimization, In addition to the above 6 points, but also to understand</span> <span style="font-size: 14px;">optimization of source code</span> <span style="font-size: 14px;">, One-click clear log (development log) information, one-click optimization to reduce the original packet size after encryption</span></p></p> <p style="padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 21px; white-space: normal; background-color: rgb(255, 255, 255); margin-top: 0px !important; margin-bottom: 0px !important;"><p style="padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 21px; white-space: normal; background-color: rgb(255, 255, 255); margin-top: 0px !important; margin-bottom: 0px !important;"><br></p></p><p><p>Android Memory optimized package nine lattice</p></p></span>