ImageSwitcher和Gallery
ImageSwitcher:图片切换
Gallery:画廊,开发中也可用于做滑动的菜单
1.首先我们来看看主函数的布局吧,说的再多也不如让你拿过来就直接可以速成运行看效果
图片就请见谅啦,自己在百度里用自己喜欢的就行啦
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2.主函数的Activity代码,里面已经包含了点击二次退出Activity功能了
import java.lang.reflect.Field;
import java.util.ArrayList;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class MainActivity extends Activity {
private Gallery mGallery;
private ImageView mImgView;
private ImageShowAdapter mAdapter = new ImageShowAdapter(this);
// 定义一个变量,来标识是否退出
private static boolean isExit = false;
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
isExit = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGallery = (Gallery) findViewById(R.id.gallery);
mImgView = (ImageView) findViewById(R.id.image_view);
try {
mGallery.setAdapter(mAdapter);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mGallery.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
Integer[] iView = mAdapter.getImagesId();
mImgView.setImageDrawable(getResources().getDrawable(iView[position%iView.length]));
}
});
}
public class ImageShowAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<Integer> imgList = new ArrayList<Integer>();
private ArrayList<Object> imgSize = new ArrayList<Object>();
public ImageShowAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(mContext);
i.setImageResource(mImagesId[position%mImagesId.length]);
i.setLayoutParams(new Gallery.LayoutParams(270,190));
return i;
}
public Integer[] getImagesId(){
return mImagesId;
}
public void setImagesId(Integer[] mImagesId){
this.mImagesId = mImagesId;
}
private Integer mImagesId[] = {
R.drawable.s1,
R.drawable.s2,
R.drawable.s3,
R.drawable.s6
};
}
/**
* 点击2次退出
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exit();
return false;
}
return super.onKeyDown(keyCode, event);
}
private void exit() {
if (!isExit) {
isExit = true;
Toast.makeText(getApplicationContext(), "再按一次退出程序",
Toast.LENGTH_SHORT).show();
// 利用handler延迟发送更改状态信息
mHandler.sendEmptyMessageDelayed(0, 2000);
} else {
finish();
System.exit(0);
}
}
}
3.这是适配器啦,直接拿过来就可以用了,简单明了
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class MyAdapter extends BaseAdapter{
private int id_image[];
private Context contex;
public MyAdapter(Context contex,int id_image[]) {
this.contex=contex;
this.id_image=id_image;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return id_image[position%id_image.length];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position%id_image.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView=new ImageView(contex);
imageView.setBackgroundResource(id_image[position%id_image.length]);
imageView.setLayoutParams(new Gallery.LayoutParams(250, 200));
imageView.setScaleType(ScaleType.FIT_XY);
return imageView;
}
}
三部曲,OK啦