大家好,继续在昨天Gallery控件实现的基础上加入ImageSwitcher控件,实现滑动切换照片、预览照片的效果。
还是,废话不多说先上效果图:
布局文件:
<RelativeLayout 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:background="#AFEEEE"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageSwitcher
android:id="@+id/imageswitcher"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ImageSwitcher>
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:spacing="16dp" />
</RelativeLayout>
代码实现部分:
package com.icedcap.gallerytest2;
import java.lang.reflect.Field;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
public class MainActivity extends Activity {
private static final String TAG = "GallertTest2";
private ImageSwitcher is;
private Gallery gallery;
private int downX, upX;// index = 0;
private ArrayList<Integer> imageRes = new ArrayList<Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 这次不用数组来声明要显示的图片,而是采用反射机制来获取图片
Field[] imagesField = R.drawable.class.getDeclaredFields();
for (Field field : imagesField) {
int index = 0;
try {
index = field.getInt(R.drawable.class);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imageRes.add(index);
}
initView();
}
private void initView() {
// 设置ImageSwitcher控件
is = (ImageSwitcher) findViewById(R.id.imageswitcher);
is.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
// imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.CENTER);// 居中
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(// 自适应图片大小
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}
});
is.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
is.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
is.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
downX = (int) event.getX();// 取得按下时的坐标
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
upX = (int) event.getX();// 取得松开时的坐标
int index = 0;
if (upX - downX > 100)// 从左拖到右,即看前一张
{
// 如果是第一,则去到尾部
if (gallery.getSelectedItemPosition() == 0)
index = gallery.getCount() - 1;
else
index = gallery.getSelectedItemPosition() - 1;
} else if (downX - upX > 100)// 从右拖到左,即看后一张
{
// 如果是最后,则去到第一
if (gallery.getSelectedItemPosition() == (gallery
.getCount() - 1))
index = 0;
else
index = gallery.getSelectedItemPosition() + 1;
}
// 改变gallery图片所选,自动触发ImageSwitcher的setOnItemSelectedListener
gallery.setSelection(index, true);
return true;
}
return false;
}
});
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
is.setImageResource(imageRes.get(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
@Override
public 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;
}
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return imageRes.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);
i.setImageResource(imageRes.get(position));
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return i;
}
private Context mContext;
}
}
最后依旧是代码下载地址: http://download.csdn.net/detail/icedcap/6990515