[Android study Note 3] imageswitcher

Source: Internet
Author: User

Today, we are implementing a simple small program for image switching.

Use the imageswitcher class and set a viewfactory to implement its makeview () method to create a view for displaying images.

Method setimageresource is used to display the specified image resource.

 

 

1. Put 8 images in the/RES/drawable directory and name them sample_0, sample_1,... sample_7, etc;

2. Define the resource ID array in the Code;

static final Integer[] imagelist = {R.drawable.sample_0,R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_4,R.drawable.sample_5,R.drawable.sample_6,R.drawable.sample_7,};

 

 

Activity. Java

Package COM. luoye. allview; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. imageswitcher; import android. widget. imageview; import android. widget. viewswitcher. viewfactory; public class secondactivity extends activity implements onclicklistener, viewfactory {imageswitcher; private button button_pre; private button button_next; static final integer [] imagelist = {R. drawable. sample_0, R. drawable. sample_1, R. drawable. sample_2, R. drawable. sample_3, R. drawable. sample_4, R. drawable. sample_5, R. drawable. sample_6, R. drawable. sample_7 ,}; Private Static int Index = 0; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. secondactivity); imageswitcher = (imageswitcher) findviewbyid (R. id. imageswitch); imageswitcher. setfactory (this); imageswitcher. setimageresource (imagelist [Index]); button_pre = (button) findviewbyid (R. id. button_pre); button_next = (button) findviewbyid (R. id. button_next); button_pre.setonclicklistener (this); button_next.setonclicklistener (this) ;}@ overridepublic view makeview () {// todo auto-generated method stubreturn new imageview (this );} @ overridepublic void onclick (view v) {// implement The onclick method of the button // todo auto-generated method stubswitch (v. GETID () {case R. id. button_next: Index ++; If (Index = imagelist. length) {Index = 0;} imageswitcher. setimageresource (imagelist [Index]); break; case R. id. button_pre: Index --; If (index <0) {Index = imagelist. length-1;} imageswitcher. setimageresource (imagelist [Index]); break; default: break ;}}}

Layout file:

<? 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"> <imageswitcher Android: id = "@ + ID/imageswitch" Android: layout_width = "wrap_content" Android: layout_height = "100dp"> </imageswitcher> <button Android: id = "@ + ID/button_next" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "previous"/> <button Android: id = "@ + ID/button_pre" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Next"/> </linearlayout>

Effect:

Click the next one to implement switching.

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.