Using the Android GridView and Baseadapter can easily complete the web format picture browser. Here's an example of how Android can access a picture resource. In fact, the so-called picture resources, you copy to the Android project pictures. It's the same as when you put a few pictures in the Web site folder, and then use the tag to refer to it as simple.
For example, in the Android project there are 8 copies of my own pictures, with the ic_launcher.png, a total of 9 pictures.
Below to complete a Web-format picture browser, first read these 9 pictures, click on any one view large image, you can press the top right corner of the menu to return, you can also press the return key.
I. Integration of picture resources
1, this is the same as you do the Web page, first the image you want to read in the app copy to the Android project directory Res\drawable-xx folder, any one can, here to drawable-hdpi as an example.
The only thing to note is that when you copy, your picture uses only lowercase letters, numbers, underscores, and dots as the file name, otherwise the Android project cannot register your image with the R file. The following error will be reported in the console:
If you successfully copy, you can open gen\ your project package name \r.java, you can find that ADT has helped you to register these image resources in the Android project. Later Android programming can use these image resources as you would with app images.
Second, the network format picture Browser writing
1, the first is the modification of res\values\strings.xml. Modified, program name, menu options, and viewactivity view the image description of the activity in the larger image, without this picture description, ADT will appear warning.
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "app_name" > Web Format Picture Viewer </ string> <string name= "Menu_exit" > Exit </string> <string name= "Imageview_description" > Big picture </string></resources>
2, first to mainactivity query more than one picture to start, mainactivity layout file Res\layout\activity_main.xml modified as follows. Just put one with the ID grid view of the GridView, specifying that each row displays two pictures.
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " android:baselinealigned=" false "> <gridview Android:id= "@+id/gridview1" android:layout_width= "match_parent" android:layout_height= "Match_parent" android:numcolumns= "2" > </GridView></LinearLayout>
3, again in the Mainactivity.java to complete the layout of the grid view. Grid view GridView to fit the adapter Baseadapter to achieve the effect.
Package Com.imageviewer;import Android.os.bundle;import Android.view.view;import android.view.viewgroup;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.baseadapter;import Android.widget.gridview;import Android.widget.imageview;import Android.widget.imageview.scaletype;import Android.app.activity;import Android.content.intent;public Class Mainactivity extends Activity {private GridView gridview1;private int[] imageId = new int[] {r.drawable.img1, r.drawable. IMG2,R.DRAWABLE.IMG3, R.drawable.img4, R.drawable.img5, R.drawable.img6,r.drawable.img7, R.drawable.img8, R.drawable.ic_launcher};//put the picture you want to display in an image ID array for easy operation @overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); gridView1 = (GridView) Findviewbyid ( R.ID.GRIDVIEW1);//Get components//write an adapter to complete the layout of the grid view. Baseadapter baseadapter = new Baseadapter () {@Overridepublic view getView (int position, view Convertview, VIewgroup arg2) {ImageView imageview1;if (Convertview = = null) {ImageView1 = new ImageView (mainactivity.this); Imageview1.setadjustviewbounds (TRUE);//auto-scale to aspect ratio imageview1.setscaletype (scaletype.center_inside);// Set picture to maintain aspect ratio display imageview1.setpadding (5, 5, 5, 5);} else {imageView1 = (ImageView) Convertview;} Imageview1.setimageresource (Imageid[position]);//Set the picture to be displayed, the ImageId this array is automatically variable return imageView1;} Gets the current option @overridepublic long getitemid (int position) {return position;} @Overridepublic Object getItem (int position) {return position;} Gets the quantity @overridepublic int GetCount () {return imageid.length;}}; Gridview1.setadapter (baseadapter);//link the adapter to the grid view Gridview1.setonitemclicklistener (new Onitemclicklistener () {// Event @overridepublic void Onitemclick (adapterview<?> arg0, View arg1, int position,//when clicked on any picture of the grid component) Position for clicked Idlong Arg3) {Intent intent=new Intent (mainactivity.this,viewactivity.class);//Activate Viewactivitybundle Bundle=new Bundle (); Bundle.putint ("Imgid", imageid[position]);//pass the ID of the clicked picture to VIEWACTivityintent.putextras (bundle); startactivity (intent);}});}} 4, after a new view of the large image of the Viewactivity.java, how to live in an app multiple activity coexistence and transfer of values in the "Android" between multiple activity using bundle Transfer value (click Open link) has said , not to repeat it here. First create a new class Viewactivity.java that inherits Android.app.Activity in SRC, then register viewactivity in Androidmanifest.xml, and modify the Androidmanifest.xml as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.imageviewer "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/>" <application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= "com.imageviewer.MainActivity" android:label= "@string/app_nam E "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </a ctivity> <activity android:name= "com.imageviewer.ViewActivity" android:label= "@string/A Pp_name "> <!--registration Viewactivity--> </activity> </application></manifest>
5, after the res\layout new Viewactivity.xml as viewactivity layout file, is also very simple, put a imageview picture with the ID view, which picture describes Android: ContentDescription only exists to eliminate the warning.
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " android:orientation=" vertical "> <imageview Android:id= "@+id/imageview1" android:layout_width= "match_parent" android:layout_height= "Match_parent" android:contentdescription= "@string/imageview_description" /></linearlayout>
6, at the same time complete the view large map of the viewactivity menu changes, res\menu\ Main.xml modified as follows, this menu was originally mainactivity from the menu, because mainactivity do not use the menu, why not directly to viewactivity use it? Wasted, the app's menu was said in the "Android" date picker, Time Picker and menu (click to open link), which is not discussed here.
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/menu_ Exit " android:title=" @string/menu_exit "/> </menu>
7, the final Viewactivity.java to write, the completion of the entire project. Where Viewactivity.java involves three parts of the function, one is to mainactivity the ID of the image resources to get, to read, one is to the menu monitoring, one is the return key monitoring, this also in the "Android" A variety of pop-up boxes and the menu key, the return key listening (click the open link) said, here no longer repeat.
Package Com.imageviewer;import Android.app.activity;import Android.content.intent;import android.os.Bundle;import Android.view.keyevent;import Android.view.menu;import Android.view.menuitem;import Android.widget.ImageView; public class Viewactivity extends Activity {private ImageView imageview1;protected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.viewactivity); imageView1 = ( ImageView) Findviewbyid (r.id.imageview1); Intent Intent = Getintent (); Bundle bundle = Intent.getextras (), int imgid = Bundle.getint ("Imgid"); Imageview1.setimageresource (imgid);//Read the corresponding picture, Put in the picture view imageview1}//create a menu method without the method, not in the upper right corner of the setup menus. @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Sets the Menu interface to Res\menu\menu.xmlgetmenuinflater (). Inflate ( R.menu.main, menu); return true;} Handle Menu Event Public boolean onoptionsitemselected (MenuItem Item) {//Gets the Id,int item_id = Item.getitemid () of the currently selected MenuItem; Switch (item_id) {//sets the method to be executed for the menu subkey with ID menu_exit. Case R.id.menu_exit:finish ();Close the activity. break;} return true;} Monitoring of physical Buttons @overridepublic boolean onKeyDown (int keycode, keyevent event) {switch (keycode) {case Keyevent.keycode_back: Finish ();//close the activity. break;} Return Super.onkeydown (KeyCode, event);}}
"Android" Image resource access with web format picture browser