Picture Viewer in Android

Source: Internet
Author: User

In this case, use Eclipse to develop the Android2.1 version of the picture Viewer.

1) First, open Eclipse, create a new Android2.1 version of the project SHOWTU, open the Strings.xml in the Res/values directory, and replace the code with code:

Path: Showtu/res/valus/string.xml

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string    name= "Hello" >hello world, showtuactivity!</string>    <string name= "app_name" >showtu</string><string name= "pre" > Previous vice </string><string name= "after" > Next </string></resources>


2) Open Main.xml under Res/layout file, there are two button controls and one ImageView control in the layout file. The code is as follows:

Path: Showtu/res/layout/main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G t;<!--Horizontal linear layout--><linearlayout android:id= "@+id/linearlayout01" android:orientation= "Horizontal" Android: Layout_width= "wrap_content" android:layout_gravity= "center" android:layout_height= "Wrap_content" > <!--button Controls- -><button android:text= "@string/pre" android:id= "@+id/button01" android:textcolor= "#000000" Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" ></button><button android:text= "@string/ After "android:id=" @+id/button02 "android:textcolor=" #000000 "android:layout_width=" Wrap_content "Android:layout_ height= "Wrap_content" ></button></linearlayout><imageview android:id= "@+id/ImageView01" Android : layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_gravity= "center" > </ImageView> <textview android:layout_width= "Fill_parent" android:layout_height = "Wrap_content" android:text= "@string/hello"/> </LinearLayout>


3) Next, the main activity file Showtuactivity.java, the code is as follows:

Path: Src/com.tu.ui/showtuactivity.java

Package Com.tu.ui;import Android.app.activity;import Android.os.bundle;import android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;import Android.widget.toast;public class Showtuactivity extends activity {/** Called when the activity is first created. */im Ageview iv;//declaration ImageView Control int count;//counter int drawableids[]={//picture ID array R.drawable.pic0,r.drawable.pic1, R.drawable.pic2,r.drawable.pic3,r.drawable.pic4,r.drawable.pic5,r.drawable.pic6,r.drawable.pic7,    R.DRAWABLE.PIC8,R.DRAWABLE.PIC9};        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);//Set Layout iv= (ImageView) Findviewbyid (R.ID.IMAGEVIEW01);//Get ImageView references Iv.setima Geresource (R.DRAWABLE.PIC0);//Set image button pre_btn= (button) Findviewbyid (r.id.button01);//Get a reference to the previous button PRE_BTN.S Etonclicklistener (//Add listener New Onclicklistener () {@Overridepublic void OnClick (View v) {if (count>0) {Iv.setimageresource (Drawableids[--count]);//Set Image}else{toast.maketext (        Showtuactivity.this, "This is the first picture", Toast.length_short). Show ();}}        }        ); Button after_btn= (button) Findviewbyid (R.ID.BUTTON02);//Gets the reference of the Next button After_btn.setonclicklistener (//Add listener new O Nclicklistener () {@Overridepublic void OnClick (View v) {if (count<drawableids.length-1) {Iv.setimageresource (Drawa        Bleids[++count]);//Set Image}else{toast.maketext (Showtuactivity.this, "This is the last picture", Toast.length_short). Show ();}}    }        ); }}

The effect is as follows:

Figure (1)

Figure (2)

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.