package com.example.image03;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
LinearLayout mLinearLayout;
private int[] imgs = { R.drawable.b, R.drawable.c, R.drawable.d,R.drawable.e };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLinearLayout = (LinearLayout) findViewById(R.id.mygallery);

for (int i = 0; i < imgs.length; i++) {
mLinearLayout.addView(getImageView(imgs[i]));
}
}

private View getImageView(int i) {
// TODO Auto-generated method stub
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),i);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setLayoutParams(new LayoutParams(250, 250));
layout.setGravity(Gravity.CENTER);

ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(bitmap);
layout.addView(imageView);

return layout;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}