Android Image Browsing source code

Source: Internet
Author: User

The Android mobile phone operating system is flexible and simple, and is favored by programmers. In particular, its open source code allows us to easily obtain the functional requirements we want. Today, we have brought you some Android Image Browsing methods.
This article is from: anzhuo flight network www.apk way.com
Original article link:
Http://www.apkway.com/forum.php? Mod = viewthread & tid = 697 & extra =
First, layout xml in Android Image Browsing:

Java code:
Java code
[Font =] <? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "Android.com/apk/res/Android"> http://schemas.Android.com/apk/res/Android "Android: layout_width =" fill_parent"
Android: layout_height = "fill_parent">
<ImageSwitcher
Android: id = "@ + id/switcher"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_alignParentTop = "true"
Android: layout_alignParentLeft = "true"/>
<Gallery
Android: id = "@ + id/gallery"
Android: background = "#55000000"
Android: layout_width = "fill_parent"
Android: layout_height = "60dp"
Android: layout_alignParentBottom = "true"
Android: layout_alignParentLeft = "true"
Android: gravity = "center_vertical"
Android: spacing = "16dp"/>
</RelativeLayout> [/font]

The preceding two controls are used in layout. ImageSwitcher is used to display the full graph, and Gallery is used to display the thumbnail. Focus on ImageSwitcher. ViewSwitcher needs to be implemented in ImageSwitcher1. the ViewFactory Interface contains the makeView method, which generates a view for displaying images. the ImageSwitcher call process is like this. First, a Factory must provide a View for it, and then ImageSwitcher can initialize various resources. Note that before using an ImageSwitcher, you must call the setFactory method. Otherwise, the setImageResource method will report a null pointer exception.

The following is the Android Image Browsing code:

Java code:
Java code
[Font =] package com. zx. imageswitcher;

Import Android. app. Activity;
Import Android. content. Context;
Import Android. OS. Bundle;
Import Android. view. View;
Import Android. view. ViewGroup;
Import Android. view. animation. AnimationUtils;
Import Android. widget. AdapterView;
Import Android. widget. BaseAdapter;
Import Android. widget. Gallery;
Import Android. widget. ImageSwitcher;
Import Android. widget. ImageView;
Import Android. widget. ViewSwitcher;
Import Android. widget. Gallery. LayoutParams;

Public class ImageSwitcherTest extends Activity implements AdapterView. OnItemSelectedListener, ViewSwitcher. ViewFactory {
Private ImageSwitcher mSwitcher;
Private Integer [] mThumbIds = {
R. drawable. sample_thumb_0, R. drawable. sample_thumb_1, R. drawable. sample_thumb2. drawable. sample_thumb_3, R. drawable. sample_thumb_4, R. drawable. sample_thumb_5, R. drawable. sample_thumb_6, R. drawable. sample_thumb_7 };
Private Integer [] mImageIds = {
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 };
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MSwitcher = (ImageSwitcher) findViewById (R. id. switcher );
MSwitcher. setFactory (this );
MSwitcher. encode (AnimationUtils. loadAnimation (this, Android. R. anim. fade_in); mSwitcher. setOutAnimation (AnimationUtils. loadAnimation (this, Android. R. anim. fade_out ));
Gallery g = (Gallery) findViewById (R. id. gallery );
G. setAdapter (new ImageAdapter (this ));
G. setOnItemSelectedListener (this );
}
/** Override for ViewSwitcher. ViewFactory # makeView ()*/
Public View makeView (){
ImageView I = new ImageView (this );
I. setBackgroundColor (0xFF000000 );
I. setScaleType (ImageView. ScaleType. FIT_CENTER );
I. setLayoutParams (new ImageSwitcher. LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. FILL_PARENT ));
Return I;
}
/*
* Override
* AdapterView. OnItemSelectedListener # onItemSelected ()
*/
Public void onItemSelected (AdapterView parent, View v, int position, long id) {mSwitcher. setImageResource (mImageIds [position]);
}
/*
* Override for AdapterView. OnItemSelectedListener # onNothingSelected ()
*/
Public void onNothingSelected (AdapterView <?> Arg0 ){
// TODO Auto-generated method stub
}
Public class ImageAdapter extends BaseAdapter {
Public ImageAdapter (Context c ){
MContext = c;
}
Public int getCount (){
Return mThumbIds. length;
}
Public Object getItem (int position ){
Return position;
}
Public long getItemId (int position ){
Return position;
}
Public View getView (int position, View convertView, ViewGroup parent ){
ImageView I = new ImageView (mContext );
I. setImageResource (mThumbIds [position]);
I. setAdjustViewBounds (true );
I. setLayoutParams (new Gallery. LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT ));
I. setBackgroundResource (R. drawable. picture_frame );
Return I;
}
Private Context mContext;
}

} [/Font]

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.