Android入门之Gallery(纯Java)
Android入门之Gallery(纯Java)
1、GalleryTest.java
- import Android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.widget.*;
- import android.widget.AdapterView.OnItemSelectedListener;
- public class GalleryTest extends Activity {
- private LinearLayout mainView = null; // 相对布局
- private Gallery gallery = null; // gallery
- private TextView tv=null; //文本
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- mainView_init();
- setContentView(mainView);
- }
- /*mainView初始化*/
- void mainView_init() {
- mainView = new LinearLayout(this);
- LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(-1, -1);
- mainView.setLayoutParams(lp);
- mainView.setOrientation(LinearLayout.VERTICAL);
- tv_init();
- gallery_init();
- mainView.addView(gallery);
- mainView.addView(tv);
- }
- /*tv初始化*/
- void tv_init(){
- tv=new TextView(this);
- LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2);
- tv.setLayoutParams(lp);
- tv.setTextSize(30);
- tv.setText("这里显示图片选中响应");
- tv.setGravity(Gravity.CENTER_HORIZONTAL);
- }
- /*gallery初始化*/
- void gallery_init() {
- gallery = new Gallery(this);
- LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(-1, -2);
- // lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
- gallery.setLayoutParams(lp);
- gallery.setSpacing(15);// 空隙
- //
- ImageAdapter ia = new ImageAdapter(this);
- gallery.setAdapter(ia);
- //
- OnItemSelectedListener oisl = new OnItemSelectedListener() {
- public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
- tv.setText("图片"+(arg2+1)+"被选中");
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- tv.setText("未选中任何图片");
- }
- };
- gallery.setOnItemSelectedListener(oisl);
- }
- }
2、ImageAdapter.java
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Matrix;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.Gallery;
- import android.widget.ImageView;
- public class ImageAdapter extends BaseAdapter {
- private Context context;
- //图片源数组
- private Integer[] imageInteger = { R.drawable.icon,R.drawable.p1,R.drawable.p2, R.drawable.p3,R.drawable.p4};
- public ImageAdapter(Context c) {
- context = c;
- }
- public int getCount() {
- return imageInteger.length;
- }
- public Object getItem(int position) {
- return position;
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView imageView = new ImageView(context);
- imageView.setLayoutParams(new Gallery.LayoutParams(-2,120));
- /*图片缩放处理,如果先设置了iamgeView的大小,则必须注意加载的图片大小不能超出,否则无法显示*/
- Bitmap bm = BitmapFactory.decodeResource(context.getResources(),imageInteger[position]);
- //原始尺寸
- int width=bm.getWidth();
- int height=bm.getHeight();
- //缩放比例
- if(height>120){
- float scaleHeight=((float)120)/height;
- float scaleWidth=scaleHeight;
- // 创建操作图片用的matrix对象
- Matrix matrix = new Matrix();
- // 缩放图片动作
- matrix.postScale(scaleWidth, scaleHeight);
- //旋转图片 动作
- //matrix.postRotate(45);
- // 创建新的图片
- Bitmap resizedBitmap = Bitmap.createBitmap(bm,0,0,width, height,matrix,true);
- imageView.setImageBitmap(resizedBitmap);
- }else{
- imageView.setImageBitmap(bm);
- }
- imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
- return imageView;
- }
- }
评论暂时关闭