由于最近小马要使用下Gallery与ImageSwitch实现照片的预览功能,特此找个时间写了个小DEMO测试了下,完整的哦,直接开始,今天好激动呢,吼吼,先分段讲下,最后贴出源代码,看代码之前先往你的模拟器“/sdcard/”下PUSH几张图片哦,作为工程中使用的图片资源,看代码:
- package com.xiaoma.www;
-
- import android.app.Activity;
- import android.os.Bundle;
-
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
-
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.Resources;
- import android.content.res.TypedArray;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.WindowManager;
- import android.view.View.OnClickListener;
- 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.Toast;
- import android.widget.ViewSwitcher;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.Gallery.LayoutParams;
-
-
-
-
-
-
-
-
- public class GalleryAddSDPhotoActivity extends Activity implements
- AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
-
- private List<String> ImageList;
- private String[] list;
- private ImageSwitcher mSwitcher;
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.main);
-
-
-
-
- ImageList = getInSDPhoto();
-
-
-
-
-
- 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(GalleryAddSDPhotoActivity.this,
- "点击了ImageSwitch中的图片",
- Toast.LENGTH_SHORT).show();
-
- }
-
- });
-
- Gallery g = (Gallery) findViewById(R.id.mygallery);
-
-
-
-
- g.setAdapter(new ImageAdapter(this, getInSDPhoto()));
-
-
-
-
- g.setOnItemSelectedListener(this);
-
-
- g.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> parent, View v,
- int position, long id) {
- Toast.makeText(GalleryAddSDPhotoActivity.this,
-
- "点击了Gallery画廊中的第 "+(position+1)+"张图片",
- Toast.LENGTH_SHORT).show();
- }
- });
- }
-
-
-
-
-
- private List<String> getInSDPhoto() {
-
-
-
-
- List<String> it = new ArrayList<String>();
-
-
-
-
-
- File f = new File("/sdcard/");
- File[] files = f.listFiles();
-
-
-
-
- for (int i = 0; i < files.length; i++) {
- File file = files[i];
- if (getAllImage(file.getPath()))
- it.add(file.getPath());
- }
- return it;
- }
-
-
-
-
-
-
- private boolean getAllImage(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 context, List<String> list) {
- mContext = context;
- lis = list;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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);
贴图小马就一次性贴出来,这样容易看出它们之间的差别:
FIT_XY效果:
![]()
FIT_START效果:
![]()
FIT_END效果:
![]()
FIT_CENTER效果:
![]()
CENTER_INSIDE效果:
![]()
CENTER_CROP效果:
![]()
CENTER效果:
![]()
下面来看下半部分代码:
-
-
-
-
-
-
-
-
- i.setLayoutParams(new Gallery.LayoutParams(136, 88));
-
-
- i.setBackgroundResource(mGalleryItemBackground);
-
-
- return i;
- }
- }
-
- public void onItemSelected(AdapterView<?> parent, View view, int position,
- long id) {
- String photoURL = list[position];
- Log.i("XiaoMa", String.valueOf(position));
- mSwitcher.setImageURI(Uri.parse(photoURL));
- }
- public void onNothingSelected(AdapterView<?> parent) {
- }
-
-
-
-
-
- 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;
- }
- }
为供朋友们学习交流,如果觉得在这儿看代码不太方便的话,可以下载小马上传的附件,里面是所有代码,在这篇文章中如果看到有地方小马有错误的,请直接批评指正,还有小马在中间提的问题,知情人士请指点下小马,先谢谢啦,吼吼。。O_O
附件:http://down.51cto.com/data/2359572
本文转自华华世界 51CTO博客,原文链接:http://blog.51cto.com/mzh3344258/752580,如需转载请自行联系原作者