Android 之 Gallery

來源:互聯網
上載者:User

1    在 xml 布局中添加 Galleryactivity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Gallery        android:id="@+id/gallery"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>


2    自訂 ImageAdapterImageAdapter.java
package com.example.gallery;import java.util.List;import android.content.Context;import android.content.res.TypedArray;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;@SuppressWarnings("deprecation")public class ImageAdapter extends BaseAdapter {        private Context context;    private List<Integer> list;    private TypedArray typedArray;    private int item_background;        public ImageAdapter(Context context ,List<Integer> list)    {        this.context=context;        this.list=list;        this.typedArray = context.obtainStyledAttributes(R.styleable.gallery_style);        item_background=typedArray.getResourceId(R.styleable.gallery_style_android_galleryItemBackground, 0);        typedArray.recycle();    }    @Override    public int getCount() {        return list.size();    }    @Override    public Object getItem(int position) {        return position;    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        ImageView imageView = new ImageView(context);        //設定顯示的圖片        imageView.setImageResource(list.get(position));                //設定伸縮規格        imageView.setScaleType(ImageView.ScaleType.FIT_XY);                //設定布局參數        imageView.setLayoutParams(new Gallery.LayoutParams(150,100));                //設定背景邊框        imageView.setBackgroundResource(item_background);                return imageView;    }}



3    每個 ImageView 的背景參數res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="gallery_style">        <attr name="android:galleryItemBackground" />    </declare-styleable></resources>


4    在 MainActivity 中綁定資料與設定監聽MainActivity.java
package com.example.gallery;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.Toast;@SuppressWarnings("deprecation")public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Gallery gallery=(Gallery)findViewById(R.id.gallery);                ArrayList<Integer>list=new ArrayList<Integer>();        list.add(R.drawable.img1);        list.add(R.drawable.img2);        list.add(R.drawable.img3);        list.add(R.drawable.img4);        list.add(R.drawable.img5);        list.add(R.drawable.img6);        list.add(R.drawable.img7);                ImageAdapter adapter=new ImageAdapter(this,list);        gallery.setAdapter(adapter);                gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {              @Override              public void onItemSelected(AdapterView<?> parent, View v,int position, long id) {                  Toast.makeText(getApplicationContext(), "選擇了:  "+                                String.valueOf(position), Toast.LENGTH_SHORT).show();            }                          @Override              public void onNothingSelected(AdapterView<?> arg0) {              //這裡不做響應              }          });     }}


5    圖片資源註:圖片最好為 png 格式的圖片,由於jpg是壓縮後的圖片,在android 中解壓縮有可能導致記憶體溢出錯誤。 6    結果展示 


註:轉載請註明出處 :)   畢竟代碼是一個一個敲出來的啊,O(∩_∩)O~



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.