Android-進階UI控制項_Gallery圖片切換

來源:互聯網
上載者:User

標籤:

代碼

Activity01

package com.lxt008;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;public class Activity01 extends Activity{    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //獲得Gallery對象        Gallery g = (Gallery) findViewById(R.id.Gallery01);        //添加ImageAdapter給Gallery對象        g.setAdapter(new ImageAdapter(this));        //設定Gallery的背景        g.setBackgroundResource(R.drawable.bg0);                //設定Gallery的事件監聽        g.setOnItemClickListener(new OnItemClickListener() {            public void onItemClick(AdapterView<?> parent, View v, int position, long id)            {                Toast.makeText(Activity01.this,"你選擇了"+(position+1)+" 號圖片",                     Toast.LENGTH_SHORT).show();            }        });    }}

ImageAdapter

package com.lxt008;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter{    // 定義Context    private Context        mContext;            // 定義整型數組 即圖片源    private Integer[]    mImageIds =     {                                     R.drawable.img1,             R.drawable.img2,             R.drawable.img3,             R.drawable.img4,             R.drawable.img5,             R.drawable.img6,             R.drawable.img7,            R.drawable.img8,            };    // 聲明 ImageAdapter    public ImageAdapter(Context c)    {        mContext = c;    }    // 擷取圖片的個數    public int getCount()    {        return mImageIds.length;    }    // 擷取圖片在庫中的位置    public Object getItem(int position)    {        return position;    }    // 擷取圖片ID    public long getItemId(int position)    {        return position;    }    public View getView(int position, View convertView, ViewGroup parent)    {        ImageView imageview = new ImageView(mContext);        // 給ImageView設定資源        imageview.setImageResource(mImageIds[position]);        // 設定布局 圖片120×120顯示        imageview.setLayoutParams(new Gallery.LayoutParams(120, 120));        // 設定顯示比例類型        imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);        return imageview;    }}

 

布局檔案

<?xml version="1.0" encoding="utf-8"?><Gallery xmlns:android="http://schemas.android.com/apk/res/android"   android:id="@+id/Gallery01"  android:layout_width="fill_parent"  android:layout_height="wrap_content"/>

 

Android-進階UI控制項_Gallery圖片切換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.