模仿QQ快速显示图片效果

编程入门 行业动态 更新时间:2024-10-26 06:37:55

模仿QQ快速显示图片<a href=https://www.elefans.com/category/jswz/34/1770448.html style=效果"/>

模仿QQ快速显示图片效果

!第一次写博客,有什么不好的地方请见谅。

实现功能:
1. 截屏点击更多可以显示截屏的图片。
2. 拍照点击更多可以显示拍照图片。

实现遇到的问题:
1. 怎么获取到每次拍照图片的路径?
2. 三星手机拍照显示照片会旋转90°。(直接看项demo)
3. 截屏怎么监听广播?(其实截屏现在是没有广播的action的)


那么我们就从第一个问题开始来解释吧。
想到图片路径我们可能会想到内容提供者,为什么那因为我们每次存储图片就相当于对系统的数据库增加或者删除了,系统会记录下来它们的位置(就是路径)那么有人可能会想知道怎么获取该路径啦。

/*** 完整的图片路径和时间** @param context* @return*/public static List<String> getFullImage(Context context) {List<String> result = new ArrayList<String>();Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;String orderBy = MediaStore.Images.Media.DATE_TAKEN + " DESC";ContentResolver contentResolver = context.getContentResolver();/*** desc 到序。*/Cursor cursor = contentResolver.query(uri, null, null, null, orderBy);if (cursor == null || cursor.getCount() <= 0) return null; // 没有图片while (cursor.moveToNext()) {int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);int dataindex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_TAKEN);String path = cursor.getString(index); // 文件地址long data = new Long(cursor.getString(dataindex));long systime = System.currentTimeMillis();flag = compareTime(data, systime);File file = new File(path);if (file.exists()) {result.add(path);}return result;}return result;}

如果各位同学学过sql 就知道 desc 是降序排序。
这个方法可以优化的,最需要获取第一张就可以,因为我(交给你们了)

以上就可以取得图片的路径了。

因为是用popwindows 显示图片的,所以就需要对图片做一些优化。
(就不一一解释了)

/*** 根据View(主要是ImageView)的宽和高来获取图片的缩略图** @param path* @param viewWidth* @param viewHeight* @return*/private Bitmap decodeThumbBitmapForFile(String path, int viewWidth, int viewHeight) {BitmapFactory.Options options = new BitmapFactory.Options();//设置为true,表示解析Bitmap对象,该对象不占内存options.inJustDecodeBounds = true;BitmapFactory.decodeFile(path, options);//设置缩放比例options.inSampleSize = calculateInSampleSize(options, viewWidth, viewHeight);//设置为false,解析Bitmap对象加入到内存中options.inJustDecodeBounds = false;return BitmapFactory.decodeFile(path, options);}/*** 根据View(主要是ImageView)的宽和高来计算Bitmap缩放比例。默认不缩放** @param options*/public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {// Raw height and width of imagefinal int height = options.outHeight;final int width = options.outWidth;int inSampleSize = 1;if (height > reqHeight || width > reqWidth) {final int halfHeight = height / 2;final int halfWidth = width / 2;// Calculate the largest inSampleSize value that is a power of 2 and keeps both// height and width larger than the requested height and width.while ((halfHeight / inSampleSize) > reqHeight&& (halfWidth / inSampleSize) > reqWidth) {inSampleSize *= 2;}}return inSampleSize;}

说起这个截屏都是一把心酸泪
方法1:想通过广播来获取截屏的action 然后在通过内容提供者查询到最后的image path。(行不通,官方目前还没有提供)

方法2:
通过检测Screenshots的文件夹,看时候有没有新的文件写入。
这里接触到一个新的方法( FileObserver)

observer = new FileObserver(pathToWatch) { // set up a file observer to watch this directory on sd card@Overridepublic void onEvent(int event, String file) {/*** FileObserver.MODIFY* 数据被写入到文件中*/if (event==FileObserver.MODIFY) {Log.d("MainActivity", "File created [" + pathToWatch + file + "]");flag = false;oldtime = System.currentTimeMillis();}}};observer.startWatching(); //START OBSERVING

详细的代码在demo中查看。

引用:

screenshot的路径获取

截屏文件夹变化更新

demo源码

附上一张效果图片

更多推荐

模仿QQ快速显示图片效果

本文发布于:2024-02-25 20:43:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1700241.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:效果   快速   图片   QQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!