package com.chaoran.component;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.activity.chaoran.R;

import java.util.List;
import java.util.Map;

public class ImageAdpter extends BaseAdapter {
	private List list;
	private Activity context;

	public ImageAdpter(List menuData, Activity context) {
		this.list = menuData;
		this.context = context;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater li = context.getLayoutInflater();
		View view = li.inflate(R.layout.imageadpter, null);
		ImageView iv = (ImageView) view.findViewById(R.id.gridimg);
		// 设置View的height和width：这样保证无论image原来的尺寸，每个图像将重新适合这个指定的尺寸。
//		iv.setLayoutParams(new RelativeLayout.LayoutParams(85, 85));
//		/*
//		 * ImageView.ScaleType.CENTER 但不执行缩放比例
//		 * ImageView.ScaleType.CENTER_CROP
//		 * 按比例统一缩放图片（保持图片的尺寸比例）便于图片的两维（宽度和高度）等于或大于相应的视图维度
//		 * ImageView.ScaleType.CENTER_INSIDE
//		 * 按比例统一缩放图片（保持图片的尺寸比例）便于图片的两维（宽度和高度）等于或小于相应的视图维度
//		 */
		iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
//		iv.setPadding(8, 8, 8, 8);
		Map map = (Map) list.get(position);
		if (map.containsKey("img")&&map.get("img") != null) {

			byte[] byt = (byte[]) map.get("img");
			iv.setImageBitmap(BitmapFactory.decodeByteArray(byt, 0, byt.length));
		}else{
			iv.setImageResource(R.drawable.logo);
		}
		((TextView) view.findViewById(R.id.gridtitle)).setText(map.get("mname")
				.toString());
		// ImageView iv=(ImageView)view.findViewById(R.id.imageView1);
		// iv.setImageResource(images[position]);
		// TextView tv=(TextView)view.findViewById(R.id.ItemText);
		// tv.setText(texts[position]);
		// tv.setTextColor(Color.BLUE);
		// tv.setTextSize(20);
		// return view;
		return view;
	}

}
