源程序是这样的
public class GridViewTest extends Activity
{
private static final String TAG = "==CrazyIt.org==";
int[] imageIds = new int[]
{
R.drawable.bomb5 , R.drawable.bomb6 , R.drawable.bomb7 
, R.drawable.bomb8 , R.drawable.bomb9 , R.drawable.bomb10
, R.drawable.bomb11 , R.drawable.bomb12 , R.drawable.bomb13
, R.drawable.bomb14 , R.drawable.bomb15 , R.drawable.bomb16
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个List对象,List对象的元素是Map
List<Map<String, Object>> listItems 
= new ArrayList<Map<String, Object>>();
for (int i = 0; i < imageIds.length; i++)
{
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("image" , imageIds[i]);
listItems.add(listItem);
}
//获取显示图片的ImageSwitcher
final ImageSwitcher switcher = (ImageSwitcher)
findViewById(R.id.switcher);
//设置图片更换的动画效果
switcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
//为ImageSwitcher设置图片切换的动画效果
switcher.setFactory(new ViewFactory()
{
@Override
public View makeView()
{
ImageView imageView = new ImageView(GridViewTest.this);
imageView.setBackgroundColor(0xff0000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return imageView;
}
});
//创建一个SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this
, listItems 
//使用/layout/cell.xml文件作为界面布局
, R.layout.cell
, new String[]{"image"}
, new int[]{R.id.image1});
GridView grid = (GridView)findViewById(R.id.grid01);
//为GridView设置Adapter
grid.setAdapter(simpleAdapter);
//添加列表项被选中的监听器
grid.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, 
int position , long id)
{
//显示当前被选中的图片
switcher.setImageResource(imageIds[position % imageIds.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent){}
});
//添加列表项被单击的监听器
grid.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent
, View view, int position, long id)
{
//显示被单击的图片的图片
switcher.setImageResource(imageIds[position % imageIds.length]);
}
});
}
}其中ImageView imageView = new ImageView(GridViewTest.this);构造函数传GridViewTest.this这个是什么意思
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
这个又是什么意思?

解决方案 »

  1.   

    ImageView的构造函数要传入一个Context,activity.this就是一个context
    下面的是imageview设置布局参数
      

  2.   

    ImageView imageView = new ImageView(GridViewTest.this); 
    ImageView();构造方法需要一个Context对象
    文档中的:
    Public Constructors
    ImageView(Context context)
    ImageView(Context context, AttributeSet attrs)
    ImageView(Context context, AttributeSet attrs, int defStyle)imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    ImageView的方法:
    public void setLayoutParams (ViewGroup.LayoutParams params);
    继承自View类
    传递进一个ViewGroup.LayoutParams类以设置View的属性ViewGroup.LayoutParams类包含了一系列View属性
    new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
    实例化了一个LayoutParams包含width=WRAP_CONTENT height=WRAP_CONTENT 的属性 表示自适应宽高
    使用的是构造函数ViewGroup.LayoutParams(int width, int height)