Drawble高效创建缩略图方法

代码是从android源码代中抽出来的,感觉不错。。。如果大家有什么建议,一起交流,谢谢!!!

public Drawable createIconThumbnail(Drawable icon,int MIconWidth,int mIconHeight) {
		int width = mIconWidth;
		int height = mIconHeight;

		final int iconWidth = icon.getIntrinsicWidth();
		final int iconHeight = icon.getIntrinsicHeight();

		if (icon instanceof PaintDrawable) {
			PaintDrawable painter = (PaintDrawable) icon;
			painter.setIntrinsicWidth(width);
			painter.setIntrinsicHeight(height);
		}

		if (width > 0 && height > 0) {
			if (width < iconWidth || height < iconHeight) {
				final float ratio = (float) iconWidth / iconHeight;

				if (iconWidth > iconHeight) {
					height = (int) (width / ratio);
				} else if (iconHeight > iconWidth) {
					width = (int) (height * ratio);
				}

				final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
						: Bitmap.Config.RGB_565;
				final Bitmap thumb = Bitmap.createBitmap(mIconWidth,
						mIconHeight, c);
				final Canvas canvas = mCanvas;
				canvas.setBitmap(thumb);
				// Copy the old bounds to restore them later
				// If we were to do oldBounds = icon.getBounds(),
				// the call to setBounds() that follows would
				// change the same instance and we would lose the
				// old bounds
				mOldBounds.set(icon.getBounds());
				final int x = (mIconWidth - width) / 2;
				final int y = (mIconHeight - height) / 2;
				icon.setBounds(x, y, x + width, y + height);
				icon.draw(canvas);
				icon.setBounds(mOldBounds);
				icon = new BitmapDrawable(getResources(), thumb);
			} else if (iconWidth < width && iconHeight < height) {
				final Bitmap.Config c = Bitmap.Config.ARGB_8888;
				final Bitmap thumb = Bitmap.createBitmap(mIconWidth,
						mIconHeight, c);
				final Canvas canvas = mCanvas;
				canvas.setBitmap(thumb);
				mOldBounds.set(icon.getBounds());
				final int x = (width - iconWidth) / 2;
				final int y = (height - iconHeight) / 2;
				icon.setBounds(x, y, x + iconWidth, y + iconHeight);
				icon.draw(canvas);
				icon.setBounds(mOldBounds);
				icon = new BitmapDrawable(getResources(), thumb);
			}
		}

		return icon;
	}
时间: 2024-08-03 09:18:29

Drawble高效创建缩略图方法的相关文章

织梦CMS批量给PJPEG格式图片创建缩略图

DEDECMS只能给gif|jpg|bmp|png四种图片格式创建缩略图,而目前来说图片格式相当多,比如PJPEGE格式的图片, 默认是不能创建的,你可以打开DEDE/inc|inc_archives_functions.php文件,查找到function GetDDImgFromBody(&$body) 下面的第一个  代码如下 复制代码 preg_match_all("/(src)=["|'| ]{0,}([^>]*.(gif|jpg|bmp|png)) 修改为 pr

腾讯电脑管家使用已创建保险柜方法

  现在,很多时候,我们都需要一些安全保护软件来保护自己的电脑.很多用户都在问,腾讯电脑管家怎么使用已创建保险柜?今天,小编就来跟大家分享腾讯电脑管家使用已创建保险柜方法. 新建后点击"打开保险柜"或在保险柜主程序中点击"打开"进行密码验证后都会弹出类似于资源管理器界面,这个就是保险柜,可以像操作普通文件那样将需要放入保险柜中的文件通过剪切/复制放入保险柜. 腾讯电脑管家使用已创建保险柜方法 腾讯电脑管家使用已创建保险柜方法 关闭资源管理器界面并没有关闭保险柜,你可

javascript元素动态创建实现方法

  本文实例讲述了javascript元素动态创建实现方法.分享给大家供大家参考.具体分析如下: document.write只能在页面加载过程中才能动态创建 可以调用document的createElement方法来创建具有指定标签的DOM对象,然后通过调用元素的appendChild方法将 新创建元素添加到相应的元素下 举例如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <html xmlns="http://www.w

移动产品设计:高效的缩略图浏览方式 旋转木马模式

移动产品设计中,图片传达的信息比文字更直白.美观.容易吸引用户注意,所以在产品中引入大量图片资源也成为设计师喜爱的方式之一. 今天我们在文中要介绍一种高效的缩略图浏览方式--旋转木马模式(Carousel). 1.什么是旋转木马(Carousel)模式? 旋转木马由来已久,在西方的游乐场中经常可以见到,早期的胶片电影灵感即来源于此,将一张张静止的画面快速转动投射到荧幕上,在中国古代也有类似的形式--"跑马灯",在节日供百姓观赏娱乐. 在移动产品交互设计中,旋转木马模式可以在一条目内同时

ASP保存远程图片到本地 同时取得第一张图片并创建缩略图的代码_应用技巧

采集中 或者 在线添加文章中 都可以用到此功能 俺自己在baidu上搜索的保存远程图片到本地的代码 感觉比较难用点 而且没有现成的比较全的代码 俺也看不懂 俺从 SNA新闻采集系统 For 3.62 (程序制作:ansir)里提取了点函数 用下 比较简单好用 以下是函数 程序代码  复制代码 代码如下: <% '================================================== '函数名:CheckDir2 '作 用:检查文件夹是否存在 '参 数:FolderP

android studio如何去掉自己创建的方法名下面的小波浪线?

问题描述 android studio如何去掉自己创建的方法名下面的小波浪线? android studio如何去掉自己创建的方法名下面的小波浪线?怎么去掉小波浪线??看着好不舒服 解决方案 File-->Settings,在左侧面板选择Editor-->Inspections,右侧找到Spelling选项,将其后面复选框的对勾去掉,然后保存即可. 解决方案二: 因为定义了这个方法却没有用到...? 解决方案三: 在这里解决

image-android中存储图像会创建缩略图

问题描述 android中存储图像会创建缩略图 我使用以下代码在相册中插入图像,但是插入图像时也会创建图像的缩略图,如何阻止缩略图的创建呢? 我在相册中加入图像,当我从相册中选择图像时,返回的是缩略图的路径,这个不是我所要的结果. 插入图像的相关代码: scr The content resolver to use source The stream to use for the image title The name of the image description The descript

js动态创建的方法传递参数

问题描述 js动态创建的方法传递参数 用ajax获取了 json数据 存进数组 动态创建a标签 现在想点击a时 触发onclick事件时传递参数 但一直失败 无奈 想知道怎么才能把参数传递出去上代码: mdId_arr.push('<li><a href=""#"">'+info.lists[i]+'</a>'+ '<a href=""#"" data-icon=""

javascript元素动态创建实现方法_javascript技巧

本文实例讲述了javascript元素动态创建实现方法.分享给大家供大家参考.具体分析如下: document.write只能在页面加载过程中才能动态创建 可以调用document的createElement方法来创建具有指定标签的DOM对象,然后通过调用元素的appendChild方法将 新创建元素添加到相应的元素下 举例如下: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-e