Android之Gallery使用例子

Welcome ·
更新时间:2024-11-15
· 568 次阅读

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

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获取图片具体实现Android入门之Gallery用法实例解析Android入门之Gallery+ImageSwitcher用法实例解析Android开发学习笔记 Gallery和GridView浅析Android控件Gallery3D效果实例代码android Gallery组件实现的iPhone图片滑动效果实例Android控件之Gallery用法实例分析Android编程实现Gallery中每次滑动只显示一页的方法Android App中使用Gallery制作幻灯片播放效果Android编程滑动效果之Gallery仿图像集浏览实现方法Android实现动态向Gallery中添加图片及倒影与3D效果示例



gallery Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号