- 浏览: 320348 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
crxiang:
写得很好啊,最近正好需要这个,谢谢分享了!
Android中ProgressDialog的简单示例 -
shangs2010:
分析的真仔细,多谢!
SharePreference类似于JavaEE中的session -
guochongcan:
fantaxy025025 写道兄弟,你这个不行的。
只能删 ...
MYSQL用一条SQL语句删除重复记录 -
fantaxy025025:
兄弟,你这个不行的。只能删除 重复个数是2的,如果重复个数大于 ...
MYSQL用一条SQL语句删除重复记录 -
wujiandong:
不错,多写点这方面的东西~~,加油~~,哥们~~
布局学习——妙用TabHost
一、相关概念
1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象
2、Canvas画布,绘图的目的区域,用于绘图
3、Bitmap位图,用于图的处理
4、Matrix矩阵
二、Bitmap
1、从资源中获取Bitmap
Java代码
Resources res = getResources(); Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);
2、Bitmap → byte[]
Java代码
public byte[] Bitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); }
3、byte[] → Bitmap
Java代码
public Bitmap Bytes2Bimap(byte[] b) { if (b.length != 0) { return BitmapFactory.decodeByteArray(b, 0, b.length); } else { return null; } }
4、Bitmap缩放
Java代码
public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = ((float) width / w); float scaleHeight = ((float) height / h); matrix.postScale(scaleWidth, scaleHeight); Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); return newbmp; }
5、将Drawable转化为Bitmap
Java代码
public static Bitmap drawableToBitmap(Drawable drawable) { // 取 drawable 的长宽 int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight(); // 取 drawable 的颜色格式 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; // 建立对应 bitmap Bitmap bitmap = Bitmap.createBitmap(w, h, config); // 建立对应 bitmap 的画布 Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, w, h); // 把 drawable 内容画到画布中 drawable.draw(canvas); return bitmap; }
6、获得圆角图片
Java代码
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, w, h); final RectF rectF = new RectF(rect); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
7、获得带倒影的图片
Java代码
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) { final int reflectionGap = 4; int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1); Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false); Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(bitmap, 0, 0, null); Paint deafalutPaint = new Paint(); canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint); canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null); Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); paint.setShader(shader); // Set the Transfer mode to be porter duff and destination in paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; }
三、Drawable
1、Bitmap转换成Drawable
Java代码
Bitmap bm=xxx; //xxx根据你的情况获取 BitmapDrawable bd= new BitmapDrawable(getResource(), bm); 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
2、Drawable缩放
Java代码
public static Drawable zoomDrawable(Drawable drawable, int w, int h) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); // drawable转换成bitmap Bitmap oldbmp = drawableToBitmap(drawable); // 创建操作图片用的Matrix对象 Matrix matrix = new Matrix(); // 计算缩放比例 float sx = ((float) w / width); float sy = ((float) h / height); // 设置缩放比例 matrix.postScale(sx, sy); // 建立新的bitmap,其内容是对原bitmap的缩放后的图 Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true); return new BitmapDrawable(newbmp); }
尊重原创
http://dyh7077063.iteye.com/blog/970672
发表评论
-
使用 SharedPreferences
2013-01-31 21:37 1173SharedPreferences是一种轻量级的数据存储 ... -
android:windowSoftInputMode属性详解
2012-11-01 07:36 799android:windowSoftInputMode ac ... -
自定义ListView中的分割线
2012-11-01 07:12 2266ListView中每个Item项之间都有分割线,设置andro ... -
在EditText/TextView中插入表情图片、样式、下划线等
2012-09-23 20:03 2659EditText: 通常用于显示 ... -
Android开发:在EditText中关闭软键盘
2012-09-23 14:41 17651、EditText有焦点(focusable为true) ... -
Android 字体和颜色
2012-06-23 18:34 939对于能够显示文字的控件(如TextView EditText ... -
解决Eclipse Indigo(3.7)中文字体偏小问题
2012-06-04 20:33 1075===========转============= ... -
Android中Cursor 的一些方法
2012-05-16 17:15 1411close() //关闭游标,释放资源 copyStrin ... -
ContentProvider分析
2012-05-15 22:24 972红色部分较重要的 private static ... -
Java的log 的几种表示颜色
2012-05-09 16:12 0Log下面的日志颜色 E Error 红 ... -
android单元测试时,异常情况解决记录
2012-04-07 20:26 1502异常内容 java.lang.NoClassDefFo ... -
Android_SDK及ADT升级方法
2012-04-05 10:57 25377本文只讲如何 ... -
android 退出程序 对话框提醒
2012-03-17 20:23 0if (keyCode == KeyEvent.KEYCO ... -
DBA应该具有什么样的素质?——转
2012-03-17 20:18 0问题起源于在写一份材 ... -
抓住移动互联网发展最佳时机,开发最好的移动应用程序,为用户提供最佳的软件服务
2012-03-04 20:03 0抓住移动互联网发展最佳时机,开发最好的移动应用程序,为用户提供 ... -
Android WebView 浏览器
2012-01-01 09:38 4160WebView的使用很方便。在学习WebView之前,我 ... -
android ImageView的scaleType属性
2011-12-27 13:07 1157ImageView:Displays an arbitra ... -
程序实现线性布局相关
2011-12-26 15:42 1013程序实现线性布局相关1、 LinearLayout l ... -
Android中Toast的用法简介
2011-12-24 21:20 1377Toast是Android中用来显示显示信息的一种机制,和Di ... -
android 中checkbox的使用
2011-12-22 16:59 1853主要是讲checkbox的使用,其实接触过html都应该知道表 ...
相关推荐
android 画图 bitmap drawable canvas paint
文档中描述了Drawable Bitmap 几种转换方法供初学者使用。方法都是经过实践验证的。
很多开发者表示,不知道Android的Drawable和Bitmap之间如何相关转换。下面Android123给大家两种比较简单高效的方法。 一、Bitmap转Drawable 代码如下: Bitmap bm=xxx; //xxx根据你的情况获取 BitmapDrawable bd...
主要介绍了Android Bitmap和Drawable的对比的相关资料,需要的朋友可以参考下
Android Drawable、Bitmap、byte、灰度 转换
Android Drawable和Bitmap的转换实例详解 通常我们需要通过代码去设置图片,就需要设置图片Bitmap和Drawable的转换,下面整理了几种方式 一、Bitmap转Drawable Bitmap bm=xxx; //xxx根据你的情况获取 ...
android中的drawable集合,包括Bitmap、shape、Layer、Transition等等
Drawable Bitmap之间的转化Drawable Bitmap之间的转化
读取本地drawable中较大的资源图片 从文件得到BitMap 从数组得到Bitmap 从流中得到Bitmap 图片透明度处理 获取源图片的BITMAP,压缩,本地图片 bitmap转byte[] 获取缩略图 保存图片 光晕效果 获取bitmap的字节大小 ...
Android Drawable、Bitmap、byte、灰度 之间的转换
很多网友刚刚开始学习Android平台,对于Drawable、Bitmap、Canvas和Paint它们之间的概念不是很清楚,其实它们除了Drawable外早在Sun的J2ME中就已经出现了,但是在Android平台中,Bitmap、Canvas相关的都有所变化。...
详细的介绍了android_Drawable、Bitmap、byte[]之间的转换 在android开发中非常实用
一般在android程序中把图片文件放在res/drawable目录下就可以通过R.drawable.id来使用,但在存储卡中的图片怎样引用呢?下面通过实现这个功能来介绍Bitmap的用法。 程序如下: import java.io.File; import android...
2、从资源中获取Bitmap Resources res=getResources(); Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic); 3、Bitmap → byte[] private byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos...
本类库可以对ImageView,View Background,Drawable和Bitmap添加风格与设置亮度、对比度
该文档教我们如何在android保持中保持bitmap数据以及读取的过程
android MitMap的放大,缩小,倒影,将drawable转换为BitMap等等