本文用来记录一些android 操作图片的方法,方便查看。

1.将Drawable转化为Bitmap

public 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;  }


2.获得圆角图片的方法

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;  }


3.获得带倒影的图片方法

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, , 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(); 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); //importantpaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; }


更多相关文章

  1. android 图片文字轮播效果(图片和文字自动滚动)
  2. Android studio编译时出现aapt.exe 崩溃的解决方法
  3. listView中item 图文并存的两种方法
  4. Android获取本机Mac地址及IP地址的方法
  5. Android 远程链接 daemon not running 解决方法
  6. android > Android实现计时与倒计时的几种方法
  7. Android 复习笔记之图解TextView类及其XML相关属性和方法
  8. android.support library找不到的解决方法

随机推荐

  1. Android(安卓)NDK 开发
  2. 【Android】16.4 IntentService类
  3. IT蓝豹强烈推荐:符合1-2年工作经验,开发中
  4. Android(安卓)Kotlin入门-控制流
  5. 扣丁学堂——SQLite
  6. Android(安卓)Studio报错:unable to find
  7. # Android中Activity四种启动模式和taskA
  8. Android使用selector点击按钮文字变色
  9. Android(安卓)打包aar给unity3d使用 项目
  10. Android(安卓)实现微信,QQ文件在其他应用