功能:实现图片的加载(带进度条)、缓存、手势缩放、ViewPaper展示、设置壁纸等常用功能
效果图:
主要实现:
图片加载框架: Android-Universal-Image-Loader
Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示。
Universal-Image-Loader初始化:
File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "imageloader/Cache"); //初始化Android-Universal-Image-Loader图片加载框架 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) .diskCache(new UnlimitedDiscCache(cacheDir)) //自定义缓存路径 .denyCacheImageMultipleSizesInMemory() .diskCacheFileNameGenerator(new Md5FileNameGenerator())//将保存的时候的URI名称用MD5 加密 .tasksProcessingOrder(QueueProcessingType.LIFO) .writeDebugLogs() // Remove for release app .build(); ImageLoader.getInstance().init(config); private void initDisplayImageOptions() { disOptions = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.empty_photo) // 设置图片在下载期间显示的图片 .showImageForEmptyUri(R.drawable.empty_photo)// 设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(R.drawable.empty_photo) // 设置图片加载/解码过程中错误时候显示的图片 .cacheInMemory(true) .cacheOnDisk(true) .considerExifParams(true) .bitmapConfig(Bitmap.Config.RGB_565) // .delayBeforeLoading(1000)//设置的下载前的延迟时间 // .displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角,弧度为多少 .displayer(new FadeInBitmapDisplayer(100))// 是否图片加载好后渐入的动画时间 .build(); }加载图片:
@Override public View getView(int index, View convertView, ViewGroup parent) { final GridHolder holder; if (convertView == null) { LayoutInflater layoutInflator = LayoutInflater.from(parent.getContext()); convertView = layoutInflator.inflate(R.layout.grid_item, null); holder = new GridHolder(); holder.appImage = (ImageView) convertView.findViewById(R.id.itemImage); holder.appName = (TextView) convertView.findViewById(R.id.itemText); holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progress); convertView.setTag(holder); } else { holder = (GridHolder) convertView.getTag(); } GridInfo info = list.get(index); if (info != null) { holder.appName.setText(info.getName()); // imageLoader.displayImage(info.getPicUrl(), // holder.appImage,disOptions); imageLoader.displayImage(info.getPicUrl(), holder.appImage, disOptions, new SimpleImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { holder.progressBar.setProgress(0); holder.progressBar.setVisibility(View.VISIBLE); } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { // TODO Auto-generated method stub holder.progressBar.setVisibility(View.GONE); } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { holder.progressBar.setVisibility(View.GONE); } }, new ImageLoadingProgressListener() { @Override public void onProgressUpdate(String imageUri, View view, int current, int total) { holder.progressBar.setProgress(Math.round(100.0f * current / total)); } }); } return convertView; }缓存清理:
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.item_clear_memory_cache: imageLoader.clearMemoryCache();//清除内存中的缓存 return true; case R.id.item_clear_disc_cache: imageLoader.clearDiskCache();//清除SD卡中的缓存 return true; default: return false; } }
String imageUri = "http://site.com/image.png"; // from Web String imageUri = "file:///mnt/sdcard/image.png"; // from SD card String imageUri = "content://media/external/audio/albumart/13"; // from content provider String imageUri = "assets://image.png"; // from assets String imageUri = "drawable://" + R.drawable.image; // from drawables
图片手势缩放框架:GestureImageView
单图浏览插件,支持图片多点缩放,支持图片拖动,开放单击和双击事件,点击事件与触摸事件不冲突。
GestureImageView使用和配置是非常简单的。入手非常容易。
<com.polites.android.GestureImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:scaleType="centerCrop" gesture-image:max-scale="10.0" gesture-image:min-scale="0.7" gesture-image:strict="false" /> gesture-image:min-scale 缩放最小值 gesture-image:max-scale 缩放最大值 gesture-image:strict 是否精确保存图片至手机相册:
/** * 保存图片 */ private void savePic() { showShortToast("保存至手机相册"); MediaStore.Images.Media.insertImage(getContentResolver(), bitmapPic, "", ""); sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + SD_PATH))); }分享图片:
/** * 分享图片 */ private void sharePic() { Intent sintent = new Intent(Intent.ACTION_SEND); sintent.setType("image/*"); File shareFile = new File(SD_PATH, "share.jpg"); try { shareFile.createNewFile(); saveBitmapFile(bitmapPic, shareFile); } catch (IOException e) { e.printStackTrace(); } saveBitmapFile(bitmapPic, shareFile); Uri u = Uri.fromFile(shareFile); sintent.putExtra(Intent.EXTRA_STREAM, u); startActivity(Intent.createChooser(sintent, "分享图片")); }设置壁纸:
/** * 设置壁纸 */ private void setWallpaper() { wallpaperFile = new File(SD_PATH, "wallpaper.jpg"); try { if (!wallpaperFile.exists()) { wallpaperFile.createNewFile(); } saveBitmapFile(bitmapPic, wallpaperFile); } catch (IOException e) { e.printStackTrace(); } Intent intent = new Intent("com.android.camera.action.CROP"); int width = WallpaperManager.getInstance(this).getDesiredMinimumWidth(); int height = WallpaperManager.getInstance(this).getDesiredMinimumHeight(); intent.setDataAndType(Uri.parse("file://" + wallpaperFile.getPath()), "image/*"); intent.putExtra("crop", "true"); intent.putExtra("outputX", width); intent.putExtra("outputY", height); intent.putExtra("aspectX", width); intent.putExtra("aspectY", height); intent.putExtra("scale", true); intent.putExtra("scaleUpIfNeeded", true); intent.putExtra("noFaceDetection", true); intent.putExtra("output", Uri.parse("file://" + wallpaperFile.getPath())); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.name()); startActivityForResult(intent, SET_WALLPAPER); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SET_WALLPAPER &&resultCode!=0&& data != null) { WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); try { wallpaperManager .setBitmap(BitmapFactory.decodeFile(wallpaperFile.getAbsolutePath())); showShortToast("设置壁纸成功"); } catch (IOException e) { showShortToast("设置壁纸失败"); e.printStackTrace(); } } }