位置: 编程技术 - 正文
推荐整理分享关于Android查询本地视频文件、获取缩略图,并且让缩略图以相同大小显示、给缩略图添加白色边框效果(android查询数据库),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:android查询数据的方法,android query 查询数据库,android对数据库的表进行查询操作,安卓查询语句,android query 查询数据库,android对数据库的表进行查询操作,android实现查询功能,android query 查询数据库,内容如对您有帮助,希望把文章链接给更多的朋友!
最近工作中遇到要查询本地的视频文件,以GridView分页显示视频缩略图,并且对不同视频分辨率的缩略图显示相同的大小,还要对缩略图添加圆角以及白色边框的效果。
用Cursor来查询本地的视频文件,将所有的文件添加到一个list中,代码如下:
public List<Video> getList() { list = null; if (getActivity()!= null) { Cursor cursor = getActivity().getContentResolver().query( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, null); if (cursor != null) { list = new ArrayList<Video>(); while (cursor.moveToNext()) { int id = cursor.getInt(cursor .getColumnIndexOrThrow(MediaStore.Video.Media._ID)); String title = cursor .getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE)); String album = cursor .getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM)); String artist = cursor .getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST)); String displayName = cursor .getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)); String mimeType = cursor .getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE)); String path = cursor .getString(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.DATA)); Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(path, Thumbnails.MINI_KIND); long duration = cursor .getInt(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)); long size = cursor .getLong(cursor .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)); if(thumbnail!=null){ Bitmap output = Bitmap.createBitmap(zoomImage(thumbnail,,).getWidth(), zoomImage(thumbnail,,).getHeight(), Config.ARGB_); //得到画布 Canvas canvas = new Canvas(output); //将画布的四角圆化 final Paint paint = new Paint(); //得到与图像相同大小的区域 由构造的四个决定区域的位置以及大小 final Rect rect = new Rect(0, 0, output.getWidth(), output.getHeight()); final RectF rectF = new RectF(rect); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); //drawRoundRect的第2,3个参数一样则画的是正圆的一角,如果数不同则是椭圆的一角 canvas.drawRoundRect(rectF, ,, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(thumbnail, rect, rect, paint); thumbnail = output; Video video2 = new Video(id, title, album, artist, displayName, mimeType, path, size, duration,thumbnail); list.add(video2); } } cursor.close(); } } return list; }
list中存放Video类,Video是一个视频类,包括视频的各种属性、构造方法、get、set方法,此处忽略。通过Cursor获得video的各种属性,其中Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(path, Thumbnails.MINI_KIND);是获得缩略图的关键,得到的是一个bitmap,path是video的路径,通过cursor可获得。
接下来,if语句中就是对缩略图的处理。其中zoomImage(BItmap,double,double);方法是缩放图片,如下:
public static Bitmap zoomImage(Bitmap bgimage, double newWidth, double newHeight) { // 获取这个图片的宽和高 float width = bgimage.getWidth(); float height = bgimage.getHeight(); // 创建操作图片用的matrix对象 Matrix matrix = new Matrix(); // 计算宽高缩放率 float scaleWidth = ((float) newWidth)/width; float scaleHeight = ((float) newHeight)/height; // 缩放图片动作 matrix.postScale(scaleWidth, scaleHeight); Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width, (int) height, matrix, true); return bitmap;}
通过该方法就可以得到指定大小的图片,再将图片圆角化处理即可。至于缩略图的白色边框,只需给GridView的item加一张中间透明的背景图片即可。当然,你得按照背景图片的大小合理调整缩略图的缩放比例,以保证缩略图和背景图片刚好融合。
在这个项目中,一开始做法是先将缩略图圆角处理,但是由于视频分辨率不同,缩略图的大小不同,要使缩略图显示大小相同,就必须放缩,放缩之后圆角就会变形。所以,关键点就在于先缩放图片,再圆角处理。
ps:由于是新手,很多东西还存在缺陷,欢迎大牛批评指正。另外,有想进行交流的,欢迎留言。
版权声明:本文为博主原创文章,未经博主允许不得转载。
Android通过HttpURLConnection获取JSON并进行UI更新 本例子中使用的是:HttpURLConnectionThreadHandler的组合,在newThread中通过HttpURLConnection获取JSON数据后并在Handler里对UI界面进行更新。也可以用过HttpClient,Asyn
AnalogClock 网上看过很多人写的AnalogClock,但是很多都不完整或无法运行,于是自己结合DeskClock源码,写了一个小的demo,步骤大致入如下:1、自定义View,通过Analog
屏蔽系统锁屏的正确方法,亲测有效! 先给大家看段对码:mKeyguard=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);mKeylock=mKeyguard.newKeyguardLock(quot;quot;);mKeylock.disableKeyguard();做过系统锁屏服务程
标签: android查询数据库
本文链接地址:https://www.jiuchutong.com/biancheng/378076.html 转载请保留说明!友情链接: 武汉网站建设