Gallery in Android to display images

Source: Internet
Author: User

Public class MainActivity extends Activity {private Gallery gallery; private LayoutInflater inflater; private ImageSwitcher imageSwitcher; private int res [] = new int [] {R. drawable. ic_launcher, R. drawable. an01, R. drawable. an02, R. drawable. an03, R. drawable. an01, R. drawable. an02, R. drawable. an03}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentVi Ew (R. layout. activity_main); // inflater = (LayoutInflater) getSystemService (LAYOUT_INFLATER_SERVICE);/*** is there a suitable adapter in the system */gallery = (Gallery) findViewById (R. id. gallery1); // obtain the ImageView control imageSwitcher = (ImageSwitcher) findViewById (R. id. imageSwitcher1); // initialize imageSwitcher at a factory. setFactory (new ImageSwitcher. viewFactory () {// set the image @ Overridepublic View makeView () {ImageView imageView = new ImageView (Main Activity. this); imageView. setScaleType (ImageView. scaleType. FIT_CENTER); return imageView ;}}); // Add the animated imageSwitcher. setInAnimation (AnimationUtils. loadAnimation (MainActivity. this, android. r. anim. fade_in); imageSwitcher. setOutAnimation (MainActivity. this, android. r. anim. fade_out); List <Map <String, Object> data = new ArrayList <Map <String, Object> (); for (int I = 0; I <res. length; I ++) {Map <String, Object> Map = new HashMap <String, Object> (); map. put ("imageView", res [I]); data. add (map);} SimpleAdapter simpleAdapter = new SimpleAdapter (this, data, R. layout. activity_cell, new String [] {"imageView"}, new int [] {R. id. imageView1}); gallery. setAdapter (simpleAdapter); // registers the event gallery. setOnItemClickListener (new AdapterView. onItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {// obtain the image resource int imageResId = res [position % res. length]; // set imageSwithcer component resource imageSwitcher. setImageResource (imageResId) ;}});}/* public class MyAdapter extends BaseAdapter {@ Overridepublic int getCount () {// TODO Auto-generated method stubreturn Integer. MAX_VALUE ;}@ Overridepublic Object getItem (int position) {return res [position % res. length] ;}@ Overridepublic long getItemId (int position) {// TODO Auto-generated method stubreturn position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {View v = inflater. inflate (R. layout. activity_gallery, null); ImageView iv = (ImageView) v. findViewById (R. id. imageView1); int imageResId = res [position % res. length]; System. out. println (position + "-------" + imageResId); iv. setImageResource (imageResId); return v ;}}*/}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Gallery        android:id="@+id/gallery1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="0dp" />    <ImageSwitcher        android:id="@+id/imageSwitcher1"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" >    </ImageSwitcher></LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/an01" /></LinearLayout>

 

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.