Gallery实现幻灯片式的图片浏览器
来源:互联网 发布:帝国cms多图上传插件 编辑:程序博客网 时间:2024/04/30 11:41
布局文件如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageSwitcher android:layout_width="320dp" android:layout_height="320dp" android:id="@+id/imageSwitcher" android:layout_marginTop="20dp" android:layout_gravity="center_horizontal"/> <Gallery android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/gallery" android:layout_marginTop="25dp" android:unselectedAlpha="0.6" android:spacing="3pt" android:layout_gravity="center_horizontal" /></LinearLayout>
java代码如下:
package com.example.administrator.gallerytest;import android.content.res.TypedArray;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher;public class GalleryTest extends AppCompatActivity { int[] imageIds = new int[]{ R.drawable.cat,//a picture about cat R.drawable.cherrytree,//a picture about cherrytree R.drawable.dog,//a picture about dog R.drawable.house,//a picture about house R.drawable.monkey,//a picture about monkey R.drawable.mountriver,//a picture about mount and river R.drawable.rabbit,//a picture about rabbit R.drawable.yellowflowers,//a picture about yellow flowers R.drawable.tree,//a picture about tree R.drawable.dragon,//a picture about dragon R.drawable.wolf,//a picture about wolf R.drawable.eagle, }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Gallery gallery = (Gallery)findViewById(R.id.gallery); //获取显示图片的ImageSwitcher对象 final ImageSwitcher switcher = (ImageSwitcher)findViewById(R.id.imageSwitcher); //为ImageSwitcher对象设置ViewFactory对象 switcher.setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { ImageView imageView = new ImageView(GalleryTest.this); //图像视图,用于支持 HTML <IMG> 标记。 // 支持通过标记的 HEIGHT 和 WIDTH 属性进行缩放。如果不能加载图像, // 则将呈现任何通过 ALT 属性指定的文本。 imageView.setBackgroundColor(0xff0000); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView.setLayoutParams(new ImageSwitcher.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); return imageView; } }); //设置图片更换的动画效果 switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); //创建一个BaseAdapter对象,该对象负责提供Gallery所显示的每张图片 BaseAdapter adapter = new BaseAdapter() { //data source(数据源)<------>adapter(适配器)<------>listview(视图控件) @Override public int getCount() { return imageIds.length; } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } //该方法返回的View就代表了每个列表项 @Override public View getView(int i, View view, ViewGroup viewGroup) { //创建一个imageView ImageView imageView = new ImageView(GalleryTest.this); imageView.setImageResource(imageIds[i % imageIds.length]); //设置ImageView的缩放类型 imageView.setScaleType(ImageView.ScaleType.FIT_XY); //把图片按照指定的大小在View中显示,拉伸显示图片,不保持原比例, // 全部显示图片填满View.关键字:不保持比例,拉伸显示全图,填满ImageView imageView.setLayoutParams(new Gallery.LayoutParams(75, 100)); TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);//?????? imageView.setBackgroundResource(typedArray.getResourceId( R.styleable.Gallery_android_galleryItemBackground, 0)); return imageView; } }; gallery.setAdapter(adapter); gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { switcher.setImageResource(imageIds[i % imageIds.length]); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); }}
图片都是自己找的,放到drawable文件夹下面。
0 0
- Gallery实现幻灯片式的图片浏览器
- 幻灯片式图片浏览器
- 幻灯片式图片浏览器
- 我的Android进阶之旅------>Android之Gallery和GridView两种方式与ImageSwitcher实现带预览的和幻灯片方式的两种图片浏览器
- Gallery + ViewPager实现图片浏览器
- Android幻灯片式图片浏览器
- Android幻灯片式图片浏览器
- 【Android开发】范例2-实现幻灯片式图片浏览器
- 利用Gallery和ImageView实现图片浏览器
- 实例4:幻灯片式图片浏览器
- 幻灯片模式图片浏览器
- 实现图片缩放的Gallery
- 图片浏览器。。Gallery
- 自定义Gallery控件实现简单3D图片浏览器
- UIImageView实现图片幻灯片
- UIImageView实现图片幻灯片
- UIImageView实现图片幻灯片
- UIImageView实现图片幻灯片
- ANDROID aidl
- php 过滤特殊字符串函数
- session入redis 操作
- OPENCV3.0 双目立体标定
- 求前缀表达式转后缀表达式的方法(pascal)
- Gallery实现幻灯片式的图片浏览器
- SOJ4509: Snowdrop修长廊 ( 斜率优化DP)
- Caffe中的优化方法
- ruby 数据sql操作ActiveRecord
- 使用onclick跳转到指定页面的方法
- 对epoll总结
- nyoj 14 会场安排问题
- StringBuffer、StringBuilder和BigDecimal
- 学习资料整理汇总(不断更新中)