android Bitmap用法总结

合集下载

Android的Bitmap的修改方法

Android的Bitmap的修改方法
这篇文章主要介绍了android持久化保存cookie的方法在解析网页信息的时候需要登录后才能访问所以使用httpclient模拟登录然后把cookie保存下来以供下一次访问使用感兴趣的小伙伴们可以参考一下
Android的 Bitmap的修改方法
Android的Bitmap和J2ME的Image比较类似。 如果我想从resource里读入一个图片,然后在这个图片上draw一点自己的信息,比如文字。 再画到屏幕上怎么做呢? J2ME里只要Image.getGraphic() 拿到 Graphic的对象就可以想draw什么就draw什么了。 那么Android里怎么实现呢? Java 代码 复制代码 代码如下:
以上代码后பைடு நூலகம்可以用canvas的draw函数在Bitmap上作修改了。 之后只要在onDraw里,用onDraw的参数canvas来drawBitmap就可以了。
Bitmap img = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(); canvas.setBitmap(img); Bitmap img = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(); canvas.setBitmap(img);

Android中Glide获取图片Path、Bitmap用法详解

Android中Glide获取图片Path、Bitmap用法详解

Android中Glide获取图⽚Path、Bitmap⽤法详解在此之前给⼤家介绍过,⼤家可以先参考⼀下,本篇内容更加深⼊的分析了Glide获取图⽚Path、Bitmap⽤法,以及实现的代码分析。

1. 获取Bitmap:1)在图⽚下载缓存好之后获取Glide.with(mContext).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {@Overridepublic void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {image.setImageBitmap(resource);}}); //⽅法中设置<span style="font-family: Arial, Helvetica, sans-serif;">asBitmap可以设置回调类型</span>上⾯是简单⽅法,下⾯有全⾯的⽅法,可以完美控制:Glide.with(mContext).load(url).asBitmap().into(new Target<Bitmap>() {@Overridepublic void onLoadStarted(Drawable placeholder) {}@Overridepublic void onLoadFailed(Exception e, Drawable errorDrawable) {}@Overridepublic void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {//TODO set bitmap}@Overridepublic void onLoadCleared(Drawable placeholder) {}@Overridepublic void getSize(SizeReadyCallback cb) {}@Overridepublic void setRequest(Request request) {}@Overridepublic Request getRequest() {return null;}@Overridepublic void onStart() {}@Overridepublic void onStop() {}@Overridepublic void onDestroy() {}});2)通过url获取Bitmap myBitmap = Glide.with(applicationContext).load(yourUrl).asBitmap() //必须.centerCrop().into(500, 500).get()2. 获取图⽚缓存路径FutureTarget<File> future = Glide.with(mContext).load("url").downloadOnly(500, 500);try {File cacheFile = future.get();String path = cacheFile.getAbsolutePath();} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}注意:这段代码需要在线程中执⾏,否则会保存,⽬前我还没整理出统⼀的⽅法回调,回头再研究研究。

android通过BitmapFactory.decodeFile获取图片bitmap报内。。。

android通过BitmapFactory.decodeFile获取图片bitmap报内。。。

android通过BitmapFactory.decodeFile获取图⽚bitmap报内。

android通过BitmapFactory.decodeFile获取图⽚bitmap报内存溢出的解决办法 原⽅法:public static Bitmap getSmallBitmap(String filePath, int reqWidth, int reqHeight) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(filePath, options);// Calculate inSampleSizeoptions.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);// Decode bitmap with inSampleSize setoptions.inJustDecodeBounds = false;return BitmapFactory.decodeFile(filePath, options);} 异常:06-23 11:41:04.817 24959-24959/com.test.tax E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.test.tax, PID: 24959ng.OutOfMemoryErrorat android.graphics.BitmapFactory.nativeDecodeStream(Native Method)at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:623)at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:599)at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:378)at com.test.tax.utils.PictureUtils.getSmallBitmap(PictureUtils.java:138)at com.test.tax.utils.PictureUtils.bitmapToFile(PictureUtils.java:58)at com.test.tax.utils.localalbum.ui.LocalAlbum.onActivityResult(LocalAlbum.java:141)at android.app.Activity.dispatchActivityResult(Activity.java:5441)at android.app.ActivityThread.deliverResults(ActivityThread.java:3353)at android.app.ActivityThread.handleSendResult(ActivityThread.java:3400)at android.app.ActivityThread.access$1300(ActivityThread.java:141)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1250)at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:136)at android.app.ActivityThread.main(ActivityThread.java:5047)at ng.reflect.Method.invokeNative(Native Method)at ng.reflect.Method.invoke(Method.java:515)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)at dalvik.system.NativeStart.main(Native Method) 解决办法: 通过设置BitmapFactory.Options属性解决options.inPreferredConfig = Bitmap.Config.RGB_565;options.inDither = true; 解决后的⽅法:public static Bitmap getSmallBitmap(String filePath, int reqWidth, int reqHeight) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(filePath, options);// Calculate inSampleSizeoptions.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);// Decode bitmap with inSampleSize setoptions.inJustDecodeBounds = false;//避免出现内存溢出的情况,进⾏相应的属性设置。

bitmap的常用方法

bitmap的常用方法

bitmap的常用方法Bitmap是一种常用的图像文件格式,被广泛应用于计算机图形处理和图像表示方面。

本文将介绍Bitmap的常用方法,包括创建Bitmap、读取Bitmap、修改Bitmap、保存Bitmap以及一些Bitmap操作的技巧和注意事项。

一、创建Bitmap在开始使用Bitmap之前,我们需要了解如何创建一个Bitmap对象。

创建Bitmap对象有以下几种常见的方法:1. 通过文件路径创建Bitmap:使用指定的文件路径可以创建一个Bitmap对象,例如:Bitmap bitmap = BitmapFactory.decodeFile("path/to/file.jpg");这里的文件路径可以是本地文件路径或者网络文件路径。

2. 通过资源ID创建Bitmap:使用资源ID可以创建一个Bitmap对象,例如:Bitmap bitmap =BitmapFactory.decodeResource(context.getResources(), R.drawable.image);这里的资源ID可以是应用程序中的图片资源ID。

3. 通过字节数组创建Bitmap:使用字节数组可以创建一个Bitmap对象,例如:Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);这里的字节数组可以是图片的二进制数据。

二、读取Bitmap创建了Bitmap对象之后,我们可以通过一些方法读取Bitmap的信息:1. 获取Bitmap的宽度和高度:int width = bitmap.getWidth();int height = bitmap.getHeight();这样我们可以得到Bitmap的尺寸信息。

2. 获取Bitmap的像素颜色:int color = bitmap.getPixel(x, y);这里的x和y是坐标值,表示要获取像素颜色的位置。

bitmap释放流程

bitmap释放流程

bitmap释放流程Bitmap释放流程介绍Bitmap是Android开发中经常用到的一种图片格式,但是如果不正确地释放Bitmap,会导致内存泄漏和OOM(Out of Memory)问题。

本文将详细说明Bitmap的释放流程,以帮助开发者正确处理Bitmap对象。

Bitmap内存管理1.Bitmap对象占用的内存是非常重要的资源,需谨慎管理。

2.Android提供了一种垃圾回收机制,但它对Bitmap的回收并不是即时的。

Bitmap释放流程1. 创建Bitmap•使用BitmapFactory类的decodeResource()或decodeStream()方法,根据资源文件或文件流创建Bitmap对象。

2. 使用Bitmap•在使用Bitmap对象之前,首先要确保它不为空。

3. 回收Bitmap•当Bitmap不再使用时,需要手动回收,避免造成内存泄漏。

•调用Bitmap类的recycle()方法释放Bitmap占用的内存。

•释放后的Bitmap对象将变为无效状态,不能再被使用。

4. 内存释放策略•在Activity或Fragment的生命周期方法中释放Bitmap,例如onDestroy()或onDetach()方法。

•在列表或滑动控件的回收方法中释放Bitmap,例如onViewRecycled()方法。

5. 避免Bitmap内存泄漏•不要将Bitmap对象存储在静态成员变量中。

•不要在循环中创建大量的Bitmap对象而不释放。

6. 控制Bitmap大小•根据需求,可以通过调用BitmapFactory类的Options参数的inSampleSize字段,控制Bitmap的大小。

•通过减小Bitmap的尺寸,可以减少占用的内存空间。

7. 检测内存泄漏•使用内存分析工具,例如Android Studio的内存监视器,检测内存泄漏情况。

•检查是否有未释放的Bitmap对象。

总结正确地释放Bitmap对象对于Android开发非常重要,可以有效地避免内存泄漏和OOM问题。

android中取得bitmap对象缩略图的方法

android中取得bitmap对象缩略图的方法

Android中取得Bitmap对象缩略图的方法| 方法2
4
方法2:不浪费内存的方法
Bitmap.createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter),该方 法可以将一个Bitmap生成指定大小的BItmap,该方法可以放大图片也可以缩小图片。
Android中取得Bitmap对象缩略图的方法| 方法1

方法1:来自开发文档
• BitmapFactory#decodeFile (String pathName, BitmapFactory.Options opts) • opts可以指定inJustDecodeBounds和inSampleSize两个参数。 • 当指定inJustDecodeBounds时候,只解析图片的长度和宽度,不载入图片。 • 当指定inSampleSize的时候,会根据inSampleSize载入一个缩略图。 • 比如inSampleSize=4,载入的缩略图是原图大小的1/4。
缩略图加载过程: • 1. 使用inJustDecodeBounds,读bitmap的长和宽。 • 2. 根据bitmap的长款和目标缩略图的长和宽,计算出inSampleSize的大小。 • 3. 使用inSampleSize,载入一个大一点的缩略图A • 4. 使用createScaseBitmap,将缩略图A,生成我们需要的缩略图B。 • 5. 回收缩略图A。
Android中取得Bitmap对象 缩略图的方法
北京信息职业技术学院 | 范美英
Android中取得Bitmap对象缩略图的方法|一个问题
2
如何获取缩略图?
• 在Android应用开发过程中,有时候不需要展示原图,只需展示 图片的缩略图,可以节省内存。

Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类

Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类

Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类[java] n package com.soai.imdemo; n nn import java.io.ByteArrayInputStream; n importjava.io.ByteArrayOutputStream; n import java.io.InputStream; n nn import android.graphics.Bitmap; n import android.graphics.BitmapFactory; n import android.graphics.Canvas; n importandroid.graphics.PixelFormat; n import android.graphics.drawable.BitmapDrawable; n import android.graphics.drawable.Drawable; n nn /**n n* Bitmap与DrawAble与byte[]与InputStream之间的转换工具类n n* @author Administratorn n*n n*/ n public class FormatTools { n n n private static FormatTools tools = new FormatTools(); n nn n n public static FormatTools getInstance() { n n n n n if (tools == null) { n n n n n n n tools = new FormatTools(); n n n n n n n return tools; n n n n n } n n n n n return tools; n n n } n nn n n // 将byte[]转换成InputStream n n n public InputStream Byte2InputStream(byte[] b) { n n n n n ByteArrayInputStream bais = new ByteArrayInputStream(b); n n n n n return bais; n n n } n nn n n // 将InputStream转换成byte[] n n n public byte[] InputStream2Bytes(InputStream is) { n n n n n String str = ""; n n n n n byte[] readByte = new byte[1024]; n n n n n int readCount = -1; n n n n n try { n n n n n n n while ((readCount = is.read(readByte, 0, 1024)) != -1) { n n n n n n n n n str += new String (readByte).trim(); n n n n n n n } n n n n n n n return str.getBytes(); n n n n n } catch (Exception e) { n n n n n n n e.printStackTrace(); n n n n n } n n n n n return null; n n n } n nn n n // 将Bitmap转换成InputStream n n n public InputStream Bitmap2InputStream(Bitmap bm) { n n n n n ByteArrayOutputStream baos = new ByteArrayOutputStream(); n n n n n press(pressFormat.JPEG, 100, baos); n n n n n InputStream is = new ByteArrayInputStream (baos.toByteArray()); n n n n n return is; n n n } n nn n n // 将Bitmap转换成InputStream n n n public InputStream Bitmap2InputStream(Bitmap bm, int quality) { n n n n n ByteArrayOutputStream baos = new ByteArrayOutputStream(); n n n n n press(pressFormat.PNG, quality, baos); n n n n n InputStream is = new ByteArrayInputStream(baos.toByteArray()); n n n n n return is; n n n } n nn n n // 将InputStream转换成Bitmap n n n public Bitmap InputStream2Bitmap (InputStream is) { n n n n n return BitmapFactory.decodeStream(is); n n n } n nn n n // Drawable 转换成InputStream n n n public InputStream Drawable2InputStream(Drawable d) { n n n n n Bitmap bitmap = this.drawable2Bitmap(d); n n n n n return this.Bitmap2InputStream(bitmap); n n n } n nn n n // InputStream转换成Drawable n n n public Drawable InputStream2Drawable(InputStream is) { n n n n n Bitmap bitmap = this.InputStream2Bitmap(is); n n n n n return this.bitmap2Drawable(bitmap); n n n } n nn n n // Drawable转换成byte[] n n n public byte[] Drawable2Bytes(Drawable d) { n n n n n Bitmap bitmap = this.drawable2Bitmap(d); n n n n n return this.Bitmap2Bytes(bitmap); n n n } n nn n n // byte[]转换成Drawable n n n public Drawable Bytes2Drawable(byte[] b) { n n n n n Bitmap bitmap = this.Bytes2Bitmap(b); n n n n n return this.bitmap2Drawable(bitmap); n n n } n nn n n // Bitmap转换成byte[] n n n public byte[] Bitmap2Bytes(Bitmap bm) { n n n n n ByteArrayOutputStream baos = new ByteArrayOutputStream(); n n n n n press(pressFormat.PNG, 100, baos); n n n n n return baos.toByteArray(); n n n } n nn n n // byte[]转换成Bitmap n n n public Bitmap Bytes2Bitmap(byte[] b) { n n n n n if (b.length != 0) { n n n n n n n returnBitmapFactory.decodeByteArray(b, 0, b.length); n n n n n } n n n n n return null; n n n } n nn n n // Drawable转换成Bitmap n n n public Bitmap drawable2Bitmap(Drawable drawable) { n n n n n Bitmap bitmap = Bitmap n n n n n n n n n .createBitmap( n n n n n n n n n n n n ndrawable.getIntrinsicWidth(), n n n n n n n n n n n n n drawable.getIntrinsicHeight(), n n n n n n n n n n n n n drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 n n n n n n n n n n n n n n n n n : Bitmap.Config.RGB_565); n n n n n Canvas canvas = new Canvas(bitmap); n n n n n drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), n n n n n n n n ndrawable.getIntrinsicHeight()); n n n n n drawable.draw(canvas); n n n n n return bitmap; n n n } n nn n n // Bitmap转换成Drawable n n n public Drawable bitmap2Drawable(Bitmap bitmap) { n n n n n BitmapDrawable bd = new BitmapDrawable(bitmap); n n n n n Drawable d = (Drawable) bd; n n n n n return d; n n n } n } n n。

androidBitmapFactory.Options使用方法详解

androidBitmapFactory.Options使用方法详解

androidBitmapFactory.Options使⽤⽅法详解BitmapFactory.Options的使⽤是在加载图⽚时,就从图⽚的加载和使⽤说起怎样获取图⽚的⼤⼩?⾸先我们把这个图⽚转成Bitmap,然后再利⽤Bitmap的getWidth()和getHeight()⽅法就可以取到图⽚的宽⾼了。

新问题⼜来了,在通过BitmapFactory.decodeFile(String path)⽅法将突破转成Bitmap时,遇到⼤⼀些的图⽚,我们经常会遇到OOM(Out Of Memory)的问题。

怎么避免它呢?这就⽤到了我们上⾯提到的BitmapFactory.Options这个类。

BitmapFactory.Options这个类,有⼀个字段叫做 inJustDecodeBounds 。

SDK中对这个成员的说明是这样的:If set to true, the decoder will return null (no bitmap), but the out…也就是说,如果我们把它设为true,那么BitmapFactory.decodeFile(String path, Options opt)并不会真的返回⼀个Bitmap给你,它仅仅会把它的宽,⾼取回来给你,这样就不会占⽤太多的内存,也就不会那么频繁的发⽣OOM了。

⽰例代码如下:BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;Bitmap bmp = BitmapFactory.decodeFile(path, options);/* 这⾥返回的bmp是null */这段代码之后,options.outWidth 和 options.outHeight就是我们想要的宽和⾼了。

有了宽,⾼的信息,我们怎样在图⽚不变形的情况下获取到图⽚指定⼤⼩的缩略图呢?⽐如我们需要在图⽚不变形的前提下得到宽度为200的缩略图。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

android Bitmap用法总结Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer41、Drawable → Bitmappublic static Bitmap drawableToBitmap(Drawable drawable) {Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);Canvas canvas = new Canvas(bitmap);// canvas.setBitmap(bitmap);drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());drawable.draw(canvas);return bitmap;}2、从资源中获取BitmapResources res=getResources();Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);3、Bitmap → byte[]private byte[] Bitmap2Bytes(Bitmap bm){ByteArrayOutputStream baos = new ByteArrayOutputStream();press(pressFormat.PNG, 100, baos);return baos.toByteArray();}4、byte[] → Bitmapprivate Bitmap Bytes2Bimap(byte[] b){if(b.length!=0){return BitmapFactory.decodeByteArray(b, 0, b.length);}else {return null;}}5、保存bitmapstatic boolean saveBitmap2file(Bitmap bmp,String filename){ CompressFormat format= pressFormat.JPEG;int quality = 100;OutputStream stream = null;try {stream = new FileOutputStream("/sdcard/" + filename);} catch (FileNotFoundException e) {// TODO Auto-generated catch blockGenerated by Foxit PDF Creator © Foxit Software For evaluation only.e.printStackTrace();}return press(format, quality, stream);}6、将图片按自己的要求缩放// 图片源Bitmap bm = BitmapFactory.decodeStream(getResources() .openRawResource(R.drawable.dog));// 获得图片的宽高int width = bm.getWidth();int height = bm.getHeight();// 设置想要的大小int newWidth = 320;int newHeight = 480;// 计算缩放比例float scaleWidth = ((float) newWidth) / width;float scaleHeight = ((float) newHeight) / height;// 取得想要缩放的matrix参数Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);// 得到新的图片Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,true);// 放在画布上canvas.drawBitmap(newbm, 0, 0, paint);相关知识链接:/thread-3162-1-1.html7、bitmap的用法小结BitmapFactory.Options option = new BitmapFactory.Options();option.inSampleSize = 2; //将图片设为原来宽高的1/2,防止内存溢出Bitmap bm = BitmapFactory.decodeFile("",option);//文件流URL url = new URL("");InputStream is = url.openStream();Bitmap bm = BitmapFactory.decodeStream(is);android:scaleType:android:scaleType是控制图片如何resized/moved来匹对ImageView的size。

ImageView.ScaleType / android:scaleType值的意义区别:CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽Generated by Foxit PDF Creator © Foxit Software For evaluation only.FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置FIT_XY / fitXY 把图片不按比例扩大/缩小到View的大小显示MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。

//放大缩小图片public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){int width = bitmap.getWidth();int height = bitmap.getHeight();Matrix matrix = new Matrix();float scaleWidht = ((float)w / width);float scaleHeight = ((float)h / height);matrix.postScale(scaleWidht, scaleHeight);Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);return newbmp;}//将Drawable转化为Bitmappublic static Bitmap drawableToBitmap(Drawable drawable){int width = drawable.getIntrinsicWidth();int height = drawable.getIntrinsicHeight();Bitmap bitmap = Bitmap.createBitmap(width, height,drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);Canvas canvas = new Canvas(bitmap);drawable.setBounds(0,0,width,height);drawable.draw(canvas);return bitmap;Generated by Foxit PDF Creator © Foxit Software For evaluation only.}//获得圆角图片的方法public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){ Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);Canvas canvas = new Canvas(output);final int color = 0xff424242;final Paint paint = new Paint();final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());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;}//获得带倒影的图片方法public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){final int reflectionGap = 4;int width = bitmap.getWidth();int height = bitmap.getHeight();Matrix matrix = new Matrix();matrix.preScale(1, -1);Bitmap reflectionImage = Bitmap.createBitmap(bitmap,0, height/2, width, height/2, matrix, false);Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888);Canvas canvas = new Canvas(bitmapWithReflection);canvas.drawBitmap(bitmap, 0, 0, null);Paint deafalutPaint = new Paint();Generated by Foxit PDF Creator © Foxit Software For evaluation only.canvas.drawRect(0, height,width,height + reflectionGap,deafalutPaint);canvas.drawBitmap(reflectionImage, 0, height + 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, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);return bitmapWithReflection;}}。

相关文档
最新文档