gallery畫廊控件

xml代碼:

<LinearLayout 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:orientation="vertical"
  
    tools:context="com.example.gallery.MainActivity$PlaceholderFragment" >

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

java代碼:

package com.example.gallery;


import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.os.Build;


public class MainActivity extends ActionBarActivity {
private Gallery gallery;
private ImageAdapter imageAdapter;
private int[] resIds={R.drawable.face1,R.drawable.face2,R.drawable.face3
,R.drawable.face4,R.drawable.face5,R.drawable.face6,R.drawable.face7
,R.drawable.face8,R.drawable.face9};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
gallery=(Gallery) findViewById(R.id.gallery);
imageAdapter=new ImageAdapter(this);
gallery.setAdapter(imageAdapter);
}


public class ImageAdapter extends BaseAdapter{
private Context context;
int mGalleryItemBackground;//使用簡單計數器,填充背景圖片

public ImageAdapter(Context context){
this.context=context;
//讀取屬性
TypedArray typedArray=obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground=typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return resIds[position];
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
//自定義的適配器,需要用自定義的佈局來顯示,通常Android的通用佈局是不能滿足我們的需求
//可以手工創建一個view視圖,也可以inflate填充一個XML
//從數據源中根據position獲得每一個item的值,填充到制定的xml佈局中
//View convertView是一箇舊的佈局,如果沒有新的佈局填充的時候,將使用舊的佈局
//ViewGroup parent 當前的佈局會被追加到父佈局中
ImageView imageView =new ImageView(context);
imageView.setImageResource(resIds[position%resIds.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(136,138));
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}

}


}


在values包下新建個xml文件attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Gallery">
        <attr name="android:galleryItemBackground"></attr>
    </declare-styleable>
</resources>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章