Android之Gallery使用例子-创新互联

刚刚学习Android,用Gallery作了一个小demo,用来记录一下。

成都创新互联主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、响应式网站、程序开发、网站优化、微网站、微信小程序开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都网站制作、做网站、外贸营销网站建设、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。
package com.example.galleryex02;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class MainActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setTitle("电影海报");

    Gallery gallery = (Gallery) findViewById(R.id.gallery1);

    MyGalleryAdapter galAdapter = new MyGalleryAdapter(this);
    gallery.setAdapter(galAdapter);
  }

  public class MyGalleryAdapter extends BaseAdapter { 
    Context context;
    Integer[] posterID = { R.drawable.mov11, R.drawable.mov12,
        R.drawable.mov13, R.drawable.mov14, R.drawable.mov15,
        R.drawable.mov16, R.drawable.mov17, R.drawable.mov18,
        R.drawable.mov19, R.drawable.mov20 };

    public MyGalleryAdapter(Context c) {
      context = c;
    }

    public int getCount() {
      // TODO Auto-generated method stub
      return posterID.length;
    }

    public Object getItem(int arg0) {
      // TODO Auto-generated method stub
      return null;
    }

    public long getItemId(int position) {
      // TODO Auto-generated method stub
      return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageview = new ImageView(context);
      imageview.setLayoutParams(new Gallery.LayoutParams(100, 150)); 
      //设置image大小

      imageview.setScaleType(ImageView.ScaleType.FIT_CENTER); 
      imageview.setPadding(5, 5, 5, 5);
      imageview.setImageResource(posterID[position]);

      final int pos = position;

      //点击galley的image的时候
      imageview.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

          ImageView ivPoster = (ImageView) findViewById(R.id.ivPoster);
          ivPoster.setScaleType(ImageView.ScaleType.FIT_CENTER);
          ivPoster.setImageResource(posterID[pos]);

          return false;
        }
      });

      return imageview;
    }
  }
}



网页题目:Android之Gallery使用例子-创新互联
本文路径:http://scjbc.cn/article/ipiop.html

其他资讯