Android learning --- Gallery gallery view, android --- Gallery

Source: Internet
Author: User

Android learning --- Gallery gallery view, android --- Gallery

Gallery and Spinner share the same parent class: AbsPinner, indicating that Gallery and Spinner are both a list box. The difference between them is that the Spinner displays a vertical list selection box, while the Gallery displays a horizontal list selection box. The role of the Spinner is to allow users to choose from, while Gallery allows users to drag to view the previous one and the next one.

The Gallery usage is similar to the usage of the Spinner. You only need to provide a content Adapter for it. The getView method of the Adapter returns the View as the list item of the Gallery list. If the program needs to monitor changes in the Gallery selection, you can add the OnItemSelectedListener listener.


Gallery xml attributes

 



The following uses a slide example to familiarize yourself with Gallery.


(1) activity_main.xml layout: An ImageSwitcher and Gallery ImageSwitcher are used to display the image selected by Gallery.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <ImageSwitcher         android:id="@+id/imgSwt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <Gallery         android:id="@+id/gallery"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="25dp"        android:unselectedAlpha="0.7"        android:spacing="2pt"/></LinearLayout>

(2) MainActivity. java

Package com. example. gallery; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. viewGroup; import android. view. animation. animationUtils; import android. widget. adapterView; import android. widget. adapterView. onItemSelectedListener; import android. widget. baseAdapter; import android. widget. gallery; import android. widget. imageSwitcher; impo Rt android. widget. imageView; import android. widget. radioGroup. layoutParams; import android. widget. viewSwitcher. viewFactory; public class MainActivity extends Activity {// define the private ImageSwitcher imgSwt = null; private Gallery gallery = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the layout component imgSwt = (ImageSwit Cher) findViewById (R. id. imgSwt); gallery = (Gallery) findViewById (R. id. gallery); // image final int images [] = new int [] {R. drawable. name01, R. drawable. name02, R. drawable. name03, R. drawable. name04, R. drawable. name05, R. drawable. name06, R. drawable. name07, R. drawable. name08, R. drawable. name09, R. drawable. name10, R. drawable. name11, R. drawable. name12}; // sets the image switching effect imgSwt. setInAnimation (AnimationUtils. loadAnimation (this, Droid. r. anim. fade_in); imgSwt. setOutAnimation (AnimationUtils. loadAnimation (this, android. r. anim. fade_out); // sets the ViewFactory object imgSwt. setFactory (new ViewFactory () {@ Overridepublic View makeView () {ImageView imageView = new ImageView (MainActivity. this); imageView. setScaleType (ImageView. scaleType. FIT_CENTER); imageView. setLayoutParams (new ImageSwitcher. layoutParams (350,350); return imageView ;}}); // create a Base Adapter object, which provides Gallery to display all images. BaseAdapter adapter = new BaseAdapter () {@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {// create imageviewImageView imageView = new ImageView (MainActivity. this); imageView. setImageResource (images [position]); imageView. setScaleType (ImageView. scaleType. FIT_XY); imageView. setLayoutParams (new Gallery. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParam S. WRAP_CONTENT); return imageView ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic Object getItem (int position) {return position ;}@ Overridepublic int getCount () {return images. length ;}}; // set the adapter Gallery for gallery. setAdapter (adapter); // Add event gallery. setOnItemSelectedListener (new OnItemSelectedListener () {@ Overridepublic void onItemSelected (AdapterView <?> Parent, View view, int position, long id) {imgSwt. setImageResource (images [position]) ;}@ Overridepublic void onNothingSelected (AdapterView <?> Parent) {// TODO Auto-generated method stub }});}}

The running effect is as follows:



Android Gallery Error

Paste the Error Log

Android adds the navigation bar view under the Gallery. When you drag the Gallery, the navigation bar displays the progress based on the position of the current image in the Gallery.

Gallery is a ListView-like view that enables switching between images by dragging left and right slowly.

Package com. test;

Import java. util. ArrayList;
Import java. util. List;

Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. OS. Bundle;
Import android. text. Layout;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. Gallery;
Import android. widget. ImageView;
Import android. widget. LinearLayout. LayoutParams;

Public class SpinnerActivity extends Activity {
Private Gallery gallery;
Private List <Bitmap> list = new ArrayList <Bitmap> ();;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Gallery = (Gallery) findViewById (R. id. images );
Init ();
Gallery. setAdapter (new MyAdapter ());

}

Private void init (){
// Obtain the image object from the image factory and add it to the set
List. add (BitmapFactory. decodeResource (getResources (), R. drawable. icon ));
List. add (BitmapFactory. decodeResource (getResources (), R. drawable. icon ));
List. add (BitmapFactory. decodeResource (getResources (), R. drawable. icon ));
List. add (BitmapFactory. decodeResource (getResources (), R. drawable. icon ));
List. add (BitmapFactory. decodeResource (getResources (), R. drawable. icon ));

}

Class MyAdapter extends BaseAdapter... the remaining full text>

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.