Android & lt; gallery drag components from scratch.. & gt;, androidgallery

Source: Internet
Author: User

Learn android <gallery drag component. ..> from scratch, androidgallery

The Gallery component can display images in the Gallery. Play images cyclically in the gallery

The inheritance structure is as follows:

Java. lang. Object
Bytes Android. view. View

Bytes Android. view. ViewGroup


Bytes Android. widget. AdapterView <T extends android. widget. Adapter>



Bytes Android. widget. AbsSpinner




Bytes Android. widget. Gallery
Using the Gallery component, you also need to use the Adapter for Data configuration.

Here we use BaseAdapter for Data configuration

Xml file


<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <Gallery        android:layout_marginTop="20dp"        android:id="@+id/gallery"        android:layout_width="fill_parent"        android:layout_height="wrap_content"         /></RelativeLayout></span>

JAVA files


<Span style = "font-size: 18px;"> package com. example. gallery; import android. OS. bundle; import android. r. integer; import android. app. activity; import android. view. menu; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. gallery; import android. widget. gallery. layoutParams; import android. widget. imageView; public class MainActivity extends Activity {private Gallery gallery; // declare Galleryprivate int images [] = {R. drawable. a1, R. drawable. a2, R. drawable. a3, R. drawable. a4, R. drawable. a5, R. drawable. a6, R. drawable. a7, R. drawable. a8, R. drawable. a9, R. drawable. a10, R. drawable. a11, R. drawable. a12, R. drawable. a13, R. drawable. a14}; // image data @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); gallery = (Gallery) this. findViewById (R. id. gallery); // obtain Gallerygallery. setAdapter (new MyAdapter ();} // The new Class inherits BaseAdapterclass MyAdapter extends BaseAdapter {@ Overridepublic int getCount () {// TODO Auto-generated method stub // The maximum return Integer of the Integer data volume. MAX_VALUE ;}@ Overridepublic Object getItem (int position) {// TODO Auto-generated method stubreturn position ;}@ Overridepublic long getItemId (int position) {// TODO Auto-generated method stubreturn position;} @ Overridepublic View getView (int position, View myview, ViewGroup parent) {// TODO Auto-generated method stub // create the View object ImageView image = new ImageView (getApplicationContext (); // set the image data. setImageResource (images [position % images. length]); // sets the image alignment mode. setScaleType (ImageView. scaleType. FIT_XY); // set the image size. setLayoutParams (new Gallery. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); // return ImageView return image ;}}</span>

Results:


Gallery loads data through the Adapter and then displays the Gallery in the Ui.

Next prediction: GridView grid View






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.