Gallery和ImageSwitcher
来源:互联网 发布:21天学通c语言app 编辑:程序博客网 时间:2024/05/17 06:14
activity_main.xml:
效果图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Gallery android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_marginTop="150dp" android:layout_width="match_parent" android:layout_height="wrap_content" > </ImageSwitcher></LinearLayout>自定义的ImageAdapter:
public class ImageAdapter extends BaseAdapter {private int[] res;private Context context;public ImageAdapter(int[] res,Context context) {super();this.res = res;this.context=context;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn Integer.MAX_VALUE; //以便“无限”循环显示(一般情况下滚动不到这个最大值)}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn res[position];}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubImageView imageView=new ImageView(context);imageView.setBackgroundResource(res[position%res.length]); //无限“循环”显示imageView.setLayoutParams(new Gallery.LayoutParams(200, 150)); //imageView.setScaleType(ScaleType.FIT_XY); //设置拉伸效果return imageView;}}MainActivity.java
public class MainActivity extends Activity implements OnItemSelectedListener,ViewFactory{//准备数据源private int[] resIcon = { R.drawable.item1, R.drawable.item2,R.drawable.item3, R.drawable.item4, R.drawable.item5,R.drawable.item6, R.drawable.item7, R.drawable.item8,R.drawable.item9, R.drawable.item10, R.drawable.item11,R.drawable.item12 };private Gallery gallery;private ImageAdapter adapter;private ImageSwitcher imageSwitcher;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);gallery=(Gallery) findViewById(R.id.gallery);imageSwitcher=(ImageSwitcher) findViewById(R.id.imageSwitcher);//gallery加载适配器adapter=new ImageAdapter(resIcon, this);gallery.setAdapter(adapter);//设置监听器gallery.setOnItemSelectedListener(this);//ImageSwitcherimageSwitcher.setFactory(this);imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));}@Overridepublic 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;}@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position,long id) {// imageView.setBackgroundResource(res[position%res.length]); //无限“循环”显示imageSwitcher.setBackgroundResource(resIcon[position%resIcon.length]);}@Overridepublic View makeView() {// TODO Auto-generated method stubImageView imageView=new ImageView(this);imageView.setScaleType(ScaleType.FIT_CENTER);return imageView;}@Overridepublic void onNothingSelected(AdapterView<?> parent) {// TODO Auto-generated method stub}}
效果图:
0 0
- Gallery和ImageSwitcher实例
- Android Gallery 和 ImageSwitcher
- Gallery和ImageSwitcher
- Gallery和ImageSwitcher
- ImageSwitcher 和 Gallery 的使用
- ImageSwitcher和Gallery的运用
- ImageSwitcher和Gallery的使用
- Android:ImageSwitcher 和 Gallery用法
- Gallery、ImageSwitcher
- Android ImageSwitcher和Gallery 综合使用
- Android ImageSwitcher和Gallery 综合使用
- Android 中的ImageSwitcher 和Gallery结合使用
- android ImageSwitcher和Gallery组合使用
- ImageSwitcher和Gallery组件的应用
- ImageSwitcher和Gallery组件的应用
- Android ImageSwitcher和Gallery 综合使用
- 使用ImageSwitcher和Gallery实现左右滑动
- 使用ImageSwitcher和Gallery实现左右滑动
- 常见sql语句效率优化方式
- 8、osg中拾取模型对象
- 年月日计算星期公式
- IE中如何阻止冒泡
- hdu 1028 Ignatius and the Princess III (母函数)
- Gallery和ImageSwitcher
- 大型网站后台架构的演变
- 静态编译Qt5.4.1和Qt WebKit
- 在Linux下编译proc程序报错:fcc-f-02104 unable to connect to oracle
- mybatis-redis的使用
- CentOS-U盘挂载与卸载
- java时间转化类,一小时前,刚刚一个月前
- 白话树莓派点滴一:入手开机体验
- Debian 8常用技巧