Gallery实现从指定目录读取文件 画廊似展示图片
GalleryPictureShowActivity
package com.lenovo.halo.gallery; import java.io.File; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Message; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.view.View.OnClickListener; 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.Toast; import android.widget.ViewSwitcher; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery.LayoutParams; public class GalleryPictureShowActivity extends Activity implements AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory { private static final String TAG = "MyLauncher"; private List<String> ImageList; private String[] list; private ImageSwitcher mSwitcher; int current = 0; String photoURL; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // //去掉状态栏,且显示自己程序名称 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.pictureshow); ImageList = new ArrayList<String>(); ImageList = getSD(); list = ImageList.toArray(new String[ImageList.size()]); mSwitcher = (ImageSwitcher) findViewById(R.id.switcher); mSwitcher.setFactory(this); mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); mSwitcher.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Toast.makeText(BgPictureShowActivity.this, "点击了", // 1).show(); } }); Gallery g = (Gallery) findViewById(R.id.mygallery); g.setAdapter(new ImageAdapter(this, getSD())); g.setOnItemSelectedListener(this); g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(GalleryPictureShowActivity.this, position+"", 1).show(); } }); } private List<String> getSD() { List<String> it = new ArrayList<String>(); String path = Environment.getExternalStorageDirectory() + ""; Log.e("path", "+++++++++++" + path); File f = new File(path); // File f = new File("/sdcard/"); File[] files = f.listFiles(); for (int i = 0; i < files.length; i++) { File file = files[i]; if (getImageFile(file.getPath())) it.add(file.getPath()); } return it; } private boolean getImageFile(String fName) { boolean re; String end = fName .substring(fName.lastIndexOf(".") + 1, fName.length()) .toLowerCase(); if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp")) { re = true; } else { re = false; } return re; } public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; private List<String> lis; public ImageAdapter(Context c, List<String> li) { mContext = c; lis = li; TypedArray a = obtainStyledAttributes(R.styleable.Gallery); mGalleryItemBackground = a.getResourceId( R.styleable.Gallery_android_galleryItemBackground, 0); a.recycle(); } public int getCount() { return lis.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView i = new ImageView(mContext); Bitmap bm = BitmapFactory.decodeFile(lis.get(position).toString()); i.setImageBitmap(bm); i.setScaleType(ImageView.ScaleType.FIT_XY); i.setLayoutParams(new Gallery.LayoutParams(136, 88)); i.setBackgroundResource(mGalleryItemBackground); return i; } } public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { photoURL = list[position]; mSwitcher.setImageURI(Uri.parse(photoURL)); } public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } public View makeView() { ImageView i = new ImageView(this); i.setBackgroundColor(0xFF000000); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return i; } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { ImageView i = new ImageView(this); if (event.getAction() == KeyEvent.ACTION_UP) { switch (keyCode) { case KeyEvent.KEYCODE_ENTER: current++; if (current == ImageList.size()) { current = 0; } mSwitcher.setImageURI(Uri.parse(photoURL)); default: break; } } return super.onKeyDown(keyCode, event); } }
定义的XML
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#55000000" android:orientation="vertical" tools:context="com.lenovo.halo.gallery.MainActivity" > <ImageSwitcher android:id="@+id/switcher" android:layout_width="wrap_content" android:layout_height="350dip" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" /> <Gallery android:id="@+id/mygallery" android:layout_width="fill_parent" android:layout_height="80dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:gravity="center_vertical" android:spacing="16dp" /> </RelativeLayout>
styleable.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Gallery" > <attr name="android:galleryItemBackground" /> </declare-styleable> </resources>
在mainfist.xml
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
相关推荐
android Gallery3D 最新源码
Gallery3D源码分析
这个demo实现了类似iOS上面的CoverFlow功能。 主要有两个功能 1, 实现图片的倒影效果 2,重写gallery的一些方法实现Y轴上的旋转,Z轴上的缩放。
Gallery实现了3D效果,让你体验完美的图片浏览效果
Android控件开发之Gallery3D酷炫效果(带源码),功能简单,欢迎下载
Android Gallery 3D效果 非常好的伪3D效果...
自定义gallery ,实现3D效果图片展示,带图片倒影
android gallery3d效果的简单实现 有遮盖 http://blog.csdn.net/kiritor/article/details/8701025
Gallery3D效果 一个完整的工程 Gallery3D效果
Android Gallery3D 源码 已编译
jquery插件TN3Gallery,用来做图片浏览的,很不错,很强大
Gallery3D.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
最为经典的 Gallery 例子!值得下载看看
1.gallery循环 2.java反射机制获取图片 3.可以从gallery中删除图片 初学必看。。
Gallery 3D 分析 附源码
Gallery画廊控件用法实例源代码,包含2D及3D两种用法
gallery3-appfog是Gallery在appfog的版本。 Gallery是一款用PHP开发的基于WEB的相片管理系统,Gallery 非常易于使用,包括一个配置向导,对于相片的操作包括自动生成缩略图、相片的大小改变、选择、排序等。 ...
Gallery3D 源码 图库
安卓Android源码——Gallery3D.zip
Page Flip Image Gallery插件使用: 1>激活插件后,我们接下来就是创建Flippingbook了,单击“FlippingBook”--->”Images“进行新建Gallery,点击"Create New Geallery"按钮,为Gallery命名(用英文命名),新建...