Android Development-Simple Picture browser

Source: Internet
Author: User

Using a linear layout, save the picture in an XML file, call the array store in Java backend Code, add a click event, and then iterate through each of the images in the array after clicking the picture;

The specific code is as follows:

XML code:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
Android:id= "@+id/root"
>
</LinearLayout>

Java code:

Package Com.example.code;

import Android.os.Bundle;
import android.app.Activity;
import Android.view.Menu;
import Android.view.View;
import Android.widget.ImageView;
import android.widget.LinearLayout;

Public class Mainactivity extends Activity {
//define an array to access the picture
int[] Images=new int[]{
R.drawable.a,
R.drawable.android,
R.drawable.android_os,
R.drawable.android3png,
R.drawable.android4png,
    };
int currentimg=0;
@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
//Get LinearLayout Linear layout container
linearlayout main= (linearlayout) Findviewbyid (r.id.root);
//Create Imgview Components
final ImageView image=new ImageView (this);
//Add the Imgview component to the LinearLayout layout container
Main.addview (image);
//Show first picture when initializing
Image.setimageresource (images[0]);

//Add Click event
Image.setonclicklistener (New View.onclicklistener () {
            
@Override
Public void OnClick (View v) {
//TODO auto-generated method stub
//Change the picture shown in ImageView
Image.setimageresource (images[++currentimg% images.length]);
            }
        });
    }
    
}

Android Development-Simple Picture browser

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.