GridView: A view that shows items in two-dimensional scrolling grid. The items in the grid come from the listadapter associated and this view. To put it simply, the GridView is the icon display of the files we see in our explorer.
As mentioned above, the GridView item is from ListAdapter, so the code of the GridView is generally used in the oncreate of the activity:
@Override publicvoid onCreate (Bundle savedinstancestate) { Super. OnCreate (savedinstancestate); Setcontentview (r.layout.grid_2); = (GridView) Findviewbyid (R.id.mygrid); G.setadapter (new imageadapter (this));
View Code
and Imageadapter is generally extends baseadapter. Baseadapter is implements ListAdapter Spinneradapter, but many times the custom adapter is the method of the parent ListAdapter interface of the override adapter:
int GetCount () Gets the items number of the current adapter Object getItem (int position) gets the corresponding position item long Getitemid (int position) gets the corresponding position item in the list in the row ID View getView (int position, view Convertview, ViewGroup pare NT) Gets the view of the data to be displayed in the specified position these method functions are almost the same as swing, based on MVC. Presumably the principle of the process is this: The program needs to display the GridView, then to the data one by one to show through a for loop, first call Adapter.getcount () to get how many data, and then position++ GetItem, GetView get the view that you want to show, this looks like a show! Here is an example of the photo grid in the official sample, I omitted some code:
Public classImageadapterextendsBaseadapter { PublicImageadapter (Context c) {Mcontext=C; } Public intGetCount () {returnmthumbids.length; } PublicObject GetItem (intposition) { returnposition; } Public LongGetitemid (intposition) { returnposition; } PublicView GetView (intposition, View Convertview, ViewGroup parent) {ImageView ImageView; if(Convertview = =NULL) {ImageView=NewImageView (Mcontext); Imageview.setlayoutparams (NewGridview.layoutparams (45, 45));//setting ImageView width and heightImageview.setadjustviewbounds (false); Imageview.setscaletype (ImageView.ScaleType.CENTER_CROP); Imageview.setpadding (8, 8, 8, 8); } Else{ImageView=(ImageView) Convertview; } imageview.setimageresource (Mthumbids[position]); returnImageView; } PrivateContext Mcontext; Privateinteger[] Mthumbids ={r.drawable.sample_thumb_0, r.drawable.sample_thumb_1, r.drawable.sample_thumb_2, R. Drawable.sample_thumb_3, R.drawable.sample_thumb_4, R.drawable.sample_thumb_5, r.drawable. Sample_thumb_6, r.drawable.sample_thumb_7}; } View CodeNote The code inside the GetView to determine if the Convertview is null for reuse, to reduce the creation of objects, and to reduce memory consumption. xml the contents of the layout file, it only indicates the GridView:
<?xml version= "1.0" encoding= "Utf-8"?> <gridview xmlns:android= "http://schemas.android.com/apk/res/ Android " android:id=" @+id/mygrid " android:layout_width=" Fill_parent " Android : Layout_height= "Fill_parent" android:padding= "10DP" android:verticalspacing= "10DP" android:horizontalspacing= "10DP" android:numcolumns= "Auto_fit" android:columnwidth= "60DP" android:stretchmode= "ColumnWidth" android: Gravity= "center"
View Code
Can see GetView, and ImageView is the focus that affects the display effect of the picture. and the number of columns is indeterminate, depending on the width of each imageview and the width of the screen. Next look at ImageView.
ImageView: Displays an arbitrary image, such as an icon. The ImageView class can load images from various sources (such as resources or content providers), takes care of computing Its measurement from the image so the it can is used in any layout manager, and provides various display options such as Scaling and tinting. ImageView is used to show the Image,icon. Here we focus on understanding ImageView's attribute Android:scaletype, namely Imageview.setscaletype (Imageview.scaletype). Android:scaletype is a size that controls how the picture resized/moved to the ImageView. The meaning difference of Imageview.scaletype/android:scaletype value:
Center/centerCentered on the original size of the picture, when the picture is longer/wider than the length/width of the view, the center portion of the image is displayed
Center_crop/centercropProportionally enlarges the size of the image so that the image is long (wide) equal to or greater than the length (width) of the view
Center_inside/centerinsideCenter the contents of the picture in full, by scaling it down or the original size to make the picture long/wide equal to or less than the length/width of the view
Fit_center/fitcenterEnlarges/shrinks the image to the width of the view, centered on the display
Fit_end/fitendEnlarges/shrinks the image to the width of the view, displayed in the lower part of the view
Fit_start/fitstartEnlarges/shrinks the image to the width of the view, displayed in the upper part of the view
Fit_xy/fitxyPut the picturedo not proportionallyEnlarge/Shrink to view size display
Matrix/matrixUsing a matrix to draw a start I do not understand matrix matrices, the web search found that the original matrix matrices can be scaled to zoom the image to display, here do not expand in-depth understanding, just paste the relevant statements, reduce the image:
//get the high and wide bitmap intBmpwidth=bmp.getwidth (); intbmpheight=bmp.getheight (); //Set scale down Doublescale=0.8; //figure out the scale to be narrowed down.Scalewidth= (float) (scalewidth*Scale ); ScaleHeight=(float) (scaleheight*Scale ); //Bitmap object after generating resizeMatrix matrix=NewMatrix (); Matrix.postscale (ScaleWidth, ScaleHeight); Bitmap resizebmp=bitmap.createbitmap (BMP, 0, 0, bmpwidth, bmpheight, Matrix,true);
View Code
* * To note that, I found that the drawable folder inside the image name can not be capitalized.
Android UI Learning-The use of the GridView and ImageView