Android学习笔记之Gallery(2)

package xiaosi.gallery;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class GalleryActivity extends Activity {
    /** Called when the activity is first created. */
    private Gallery gallery =null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        gallery = (Gallery)findViewById(R.id.gallery);
        //设置图片适配器
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setSpacing(5);
        
        //设置监听器
        gallery.setOnItemClickListener(new OnItemClickListener() {
        	
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,    long arg3) {
                Toast.makeText(GalleryActivity.this, "点击了第"+arg2+"张图片", Toast.LENGTH_LONG).show();
            }
        });
    }
}

class ImageAdapter extends BaseAdapter{
	
	int mGalleryItemBackground;
    private Context context;
    //图片源数组
    private Integer[] imageInteger={
            R.drawable.a,
            R.drawable.b,
            R.drawable.c,
            R.drawable.d
    };
    public ImageAdapter(Context c){
        context = c;
        TypedArray attr = context.obtainStyledAttributes(R.styleable.HelloGallery);        
        mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);        
        attr.recycle();
    }
    // 获取图片的个数
    public int getCount() {
        return imageInteger.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(context);
        // 给ImageView设置资源
        imageView.setImageResource(imageInteger[position]);
        // 设置显示比例类型  
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        // 设置布局 图片120*80 
        imageView.setLayoutParams(new Gallery.LayoutParams(180, 100));
        imageView.setBackgroundResource(mGalleryItemBackground);        
        return imageView;
    }
}


mian.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

	<Gallery
	    android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:background="?android:galleryItemBackground"/>
</LinearLayout>


创建一个新的XML文件在res/values/目录下 attrs.xml命名。

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


这是一个定制的styleable资源,可以应用于一个布局。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值