package cn.yinuo.picture;

import java.io.File;



import cn.yinuo.picture.R;
import cn.yinuo.search.Search;
import cn.yinuo.service.ImageServiceBean;
import android.app.Activity;
import android.os.Bundle;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ImageListActivity extends Activity {
private ImageSwitcher imageSwitcher;
private Gallery gallery;
private int index = 0;//记录当前浏览的图片索引
private ImageServiceBean imageService;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button search = (Button) findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Intent intent=new Intent(ImageListActivity.this,Search.class);
startActivity(intent);

}
});

imageService = new ImageServiceBean(this);

imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
imageSwitcher.setFactory(new ImageSwitcher.ViewFactory() {
public View makeView() {
ImageView i = new ImageView(ImageListActivity.this);
i.setImageURI(imageService.getBigBitmap(index));//默认显示第一张图
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return i;
}
});
imageSwitcher.setOnClickListener(new View.OnClickListener() {//点击全屏显示图片
public void onClick(View v) {
showPicture();
}
});

gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));//绑定数据
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
imageSwitcher.setImageURI(imageService.getBigBitmap(position));
index = position;
}
});
gallery.setSelection(index);

Button quit = (Button) findViewById(R.id.quit);
quit.setOnClickListener(new View.OnClickListener() {//退出应用
public void onClick(View v) {
finish();
}
});

}


//打开全屏显示图片
private void showPicture() {
Intent intent = new Intent(ImageListActivity.this, ShowActivity.class);
intent.putExtra("imageuri", imageService.getBigBitmap(index).toString());
startActivity(intent);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { //处理按键事件,注:Gallery已经处理了左右键移动图片,我们只需处理确认键打开全图
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER://确认键
index = gallery.getSelectedItemPosition();//获取当前图片的索引
imageSwitcher.setImageURI(imageService.getBigBitmap(index));//显示对应索引图片
showPicture();
break;
}
return super.onKeyDown(keyCode, event);
}

@Override
protected void onDestroy() {
super.onDestroy();
if(this.getCacheDir().exists()){//清除缓存图片
for(File file : this.getCacheDir().listFiles()){
file.delete();
}
}
android.os.Process.killProcess(android.os.Process.myPid());//杀死进程,关闭应用
}

private final class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;

TypedArray a = obtainStyledAttributes(R.styleable.Gallery);

mGalleryItemBackground = a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);

a.recycle();
}

/*返回图片数目 */
public int getCount() {
return imageService.getLength();
}

public long getItemId(int position) {
return position;
}
/* 覆盖的方法getView,返回一View对象 */
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
/* 设置图片给imageView对象 */
i.setImageURI(imageService.getSmallBitmap(position));
/* 重新设置图片的宽高 */
i.setScaleType(ImageView.ScaleType.FIT_XY);
/* 重新设置Layout的宽高 */
i.setLayoutParams(new Gallery.LayoutParams(70, 60));
/* 设置Gallery背景图 */
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
public Object getItem(int position) {
return imageService.getSmallBitmap(position);
}
}
}

更多相关文章

  1. Android 图片压缩
  2. android 网络请求图片
  3. android Animation图片渐变动画 Demo
  4. 使用android的bitmap类实现图片的拼接
  5. android下图片压缩
  6. android 图片展示
  7. Android播放GIF图片
  8. android 图片进度条

随机推荐

  1. 聚焦 Android 11: UI 与 Compose
  2. Android 开发中 getContext 和 getActivi
  3. WorkManager 在 Kotlin 中的实践
  4. 【视频讲解】Android(安卓)ApiDemos示例
  5. 移动原生应用开发 - AndroidX使用指南
  6. Android WindowManager 全局悬浮窗 + Acc
  7. 微信支付奇葩问题 system:access_denied
  8. Android栗子の图片验证码生成实例代码
  9. Android应用界面开发——第二周笔记
  10. Android 设置EditText不显示光标