android图片处理,使用指南

  1. conn.setDoInput(true);

  2. conn.connect();

  3. InputStream is = conn.getInputStream();

  4. bitmap = BitmapFactory.decodeStream(is);

  5. is.close();

  6. } catch (IOException e) {

  7. e.printStackTrace();

  8. }

  9. Log.v(tag, bitmap.toString());

  10. return bitmap;

  11. }

  12. //通过传入位图,新的宽.高比进行位图的缩放操作

  13. public static Drawable resizeImage(Bitmap bitmap, int w, int h) {

  14. // load the origial Bitmap

  15. Bitmap BitmapOrg = bitmap;

  16. int width = BitmapOrg.getWidth();

  17. int height = BitmapOrg.getHeight();

  18. int newWidth = w;

  19. int newHeight = h;

  20. Log.v(tag, String.valueOf(width));

  21. Log.v(tag, String.valueOf(height));

  22. Log.v(tag, String.valueOf(newWidth));

  23. Log.v(tag, String.valueOf(newHeight));

  24. // calculate the scale

  25. float scaleWidth = ((float) newWidth) / width;

  26. float scaleHeight = ((float) newHeight) / height;

  27. // create a matrix for the manipulation

  28. Matrix matrix = new Matrix();

  29. // resize the Bitmap

  30. matrix.postScale(scaleWidth, scaleHeight);

  31. // if you want to rotate the Bitmap

  32. // matrix.postRotate(45);

  33. // recreate the new Bitmap

  34. Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,

  35. height, matrix, true);

  36. // make a Drawable from Bitmap to allow to set the Bitmap

  37. // to the ImageView, ImageButton or what ever

  38. return new BitmapDrawable(resizedBitmap);

  39. }

Java代码   收藏代码

  1. 1.图片加载方法,方便用户加载图片

  2. /***

  3. * 加载本地图片

  4. * @param context:主运行函数实例

  5. * @param bitAdress:图片地址,一般指向R下的drawable目录

  6. * @return

  7. */

  8. public final Bitmap CreatImage(Context context, int bitAdress) {

  9. Bitmap bitmaptemp = null;

  10. bitmaptemp = BitmapFactory.decodeResource(context.getResources(),

  11. bitAdress);

  12. return bitmaptemp;

  13. }

  14. 2.图片平均分割方法,将大图平均分割为N行N列,方便用户使用

  15. /***

  16. * 图片分割

  17. *

  18. * @param g

  19. * :画布

  20. * @param paint

  21. * :画笔

  22. * @param imgBit

  23. * :图片

  24. * @param x

  25. * :X轴起点坐标

  26. * @param y

  27. * :Y轴起点坐标

  28. * @param w

  29. * :单一图片的宽度

  30. * @param h

  31. * :单一图片的高度

  32. * @param line

  33. * :第几列

  34. * @param row

  35. * :第几行

  36. */

  37. public final void cuteImage(Canvas g, Paint paint, Bitmap imgBit, int x,

  38. int y, int w, int h, int line, int row) {

  39. g.clipRect(x, y, x + w, h + y);

  40. g.drawBitmap(imgBit, x – line * w, y – row * h, paint);

  41. g.restore();

  42. }

  43. 3.图片缩放,对当前图片进行缩放处理

  44. /***

  45. * 图片的缩放方法

  46. *

  47. * @param bgimage

  48. * :源图片资源

  49. * @param newWidth

  50. * :缩放后宽度

  51. * @param newHeight

  52. * :缩放后高度

  53. * @return

  54. */

  55. public Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) {

  56. // 获取这个图片的宽和高

  57. int width = bgimage.getWidth();

  58. int height = bgimage.getHeight();

  59. // 创建操作图片用的matrix对象

  60. Matrix matrix = new Matrix();

  61. // 计算缩放率,新尺寸除原始尺寸

  62. float scaleWidth = ((float) newWidth) / width;

  63. float scaleHeight = ((float) newHeight) / height;

  64. // 缩放图片动作

  65. matrix.postScale(scaleWidth, scaleHeight);

  66. Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,

  67. matrix, true);

  68. return bitmap;

  69. }

  70. 4.绘制带有边框的文字,一般在游戏中起文字的美化作用

  71. /***

  72. * 绘制带有边框的文字

  73. *

  74. * @param strMsg

  75. * :绘制内容

  76. * @param g

  77. * :画布

  78. * @param paint

  79. * :画笔

  80. * @param setx

  81. * ::X轴起始坐标

  82. * @param sety

  83. * :Y轴的起始坐标

  84. * @param fg

  85. * :前景色

  86. * @param bg

  87. * :背景色

  88. */

  89. public void drawText(String strMsg, Canvas g, Paint paint, int setx,

  90. int sety, int fg, int bg) {

  91. paint.setColor(bg);

  92. g.drawText(strMsg, setx + 1, sety, paint);

  93. g.drawText(strMsg, setx, sety – 1, paint);

  94. g.drawText(strMsg, setx, sety + 1, paint);

  95. g.drawText(strMsg, setx – 1, sety, paint);

  96. paint.setColor(fg);

  97. g.drawText(strMsg, setx, sety, paint);

  98. g.restore();

  99. }

  100. 5.Android 图片透明度处理代码

  101. /**

  102. * 图片透明度处理

  103. *

  104. * @param sourceImg

  105. *            原始图片

  106. * @param number

  107. *            透明度

  108. * @return

  109. */

  110. public static Bitmap setAlpha(Bitmap sourceImg, int number) {

  111. int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];

  112. sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0,sourceImg.getWidth(), sourceImg.getHeight());// 获得图片的ARGB值

  113. number = number * 255 / 100;

  114. for (int i = 0; i < argb.length; i++) {

  115. argb = (number << 24) | (argb & 0×00FFFFFF);// 修改最高2位的值

  116. }

  117. sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888);

  118. return sourceImg;

  119. }

  120. 6.图片翻转

  121. Resources res = this.getContext().getResources();

  122. img = BitmapFactory.decodeResource(res, R.drawable.slogo);

  123. Matrix matrix = new Matrix();

  124. matrix.postRotate(90);        /*翻转90度*/

  125. int width = img.getWidth();

  126. int height = img.getHeight();

  127. r_img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);

Java代码   收藏代码

  1. import android.graphics.Bitmap;

  2. import android.graphics.Canvas;

  3. import android.graphics.LinearGradient;

  4. import android.graphics.Matrix;

  5. import android.graphics.Paint;

  6. import android.graphics.PixelFormat;

  7. import android.graphics.PorterDuffXfermode;

  8. import android.graphics.Rect;

  9. import android.graphics.RectF;

  10. import android.graphics.Bitmap.Config;

  11. import android.graphics.PorterDuff.Mode;

  12. import android.graphics.Shader.TileMode;

  13. import android.graphics.drawable.Drawable;

  14. /**

  15. *

  16. * @author superdev

  17. * @version 1.0

  18. *

  19. */

  20. public class ImageUtil {

  21. /**

  22. * 放大缩小图片

  23. */

  24. public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {

  25. int width = bitmap.getWidth();

  26. int height = bitmap.getHeight();

  27. Matrix matrix = new Matrix();

  28. float scaleWidht = ((float) w / width);

  29. float scaleHeight = ((float) h / height);

  30. matrix.postScale(scaleWidht, scaleHeight);

  31. Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

  32. return newbmp;

  33. }

  34. /**

  35. * 将Drawable转化为Bitmap

  36. */

  37. public static Bitmap drawableToBitmap(Drawable drawable) {

  38. int width = drawable.getIntrinsicWidth();

  39. int height = drawable.getIntrinsicHeight();

  40. Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);

  41. Canvas canvas = new Canvas(bitmap);

  42. drawable.setBounds(0, 0, width, height);

  43. drawable.draw(canvas);

  44. return bitmap;

  45. }

  46. /**

  47. * 获得圆角图片的方法

  48. */

  49. public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {

  50. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

  51. Canvas canvas = new Canvas(output);

  52. final int color = 0xff424242;

  53. final Paint paint = new Paint();

  54. final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

  55. final RectF rectF = new RectF(rect);

  56. paint.setAntiAlias(true);

  57. canvas.drawARGB(0, 0, 0, 0);

  58. paint.setColor(color);

  59. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

  60. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

  61. canvas.drawBitmap(bitmap, rect, rect, paint);

  62. return output;

  63. }

  64. /**

  65. * 获得带倒影的图片方法

  66. */

  67. public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {

  68. final int reflectionGap = 4;

  69. int width = bitmap.getWidth();

  70. int height = bitmap.getHeight();

  71. Matrix matrix = new Matrix();

  72. matrix.preScale(1, -1);

  73. Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

  74. Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

  75. Canvas canvas = new Canvas(bitmapWithReflection);

  76. canvas.drawBitmap(bitmap, 0, 0, null);

  77. Paint deafalutPaint = new Paint();

  78. canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

  79. canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

  80. Paint paint = new Paint();

  81. LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);

  82. paint.setShader(shader);

  83. // Set the Transfer mode to be porter duff and destination in

  84. paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

  85. // Draw a rectangle using the paint with our linear gradient

  86. canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

  87. return bitmapWithReflection;

  88. }

  89. }

Java代码   收藏代码

  1. private byte[] Bitmap2Bytes(Bitmap bm){

  2. ByteArrayOutputStream baos = new ByteArrayOutputStream();

  3. bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

  4. return baos.toByteArray();

  5. }

  6. private Bitmap Bytes2Bimap(byte[] b){

  7. if(b.length!=0){

  8. return BitmapFactory.decodeByteArray(b, 0, b.length);

  9. }

  10. else {

  11. return null;

  12. }

  13. }

  14. /**

  15. * create the bitmap from a byte array

  16. *生成水印图片

  17. * @param src the bitmap object you want proecss

  18. * @param watermark the water mark above the src

  19. * @return return a bitmap object ,if paramter’s length is 0,return null

  20. */

  21. private Bitmap createBitmap( Bitmap src, Bitmap watermark )

  22. {

  23. String tag = “createBitmap”;

  24. Log.d( tag, “create a new bitmap” );

  25. if( src == null )

  26. {

  27. return null;

  28. }

  29. int w = src.getWidth();

  30. int h = src.getHeight();

  31. int ww = watermark.getWidth();

  32. int wh = watermark.getHeight();

  33. //create the new blank bitmap

  34. Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图

  35. Canvas cv = new Canvas( newb );

  36. //draw src into

  37. cv.drawBitmap( src, 0, 0, null );//在 0,0坐标开始画入src

  38. //draw watermark into

  39. cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印

  40. //save all clip

  41. cv.save( Canvas.ALL_SAVE_FLAG );//保存

  42. //store

  43. cv.restore();//存储

  44. return newb;

  45. }

  46. /** 重新编码Bitmap

  47. *

  48. * @param src

  49. *          需要重新编码的Bitmap

  50. *

  51. * @param format

  52. *          编码后的格式(目前只支持png和jpeg这两种格式)

  53. *

  54. * @param quality

  55. *          重新生成后的bitmap的质量

  56. *

  57. * @return

  58. *          返回重新生成后的bitmap

  59. */

  60. private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,

  61. int quality) {

  62. ByteArrayOutputStream os = new ByteArrayOutputStream();

  63. src.compress(format, quality, os);

  64. byte[] array = os.toByteArray();

  65. return BitmapFactory.decodeByteArray(array, 0, array.length);

  66. }

  67. //Stream转换成Byte

  68. static byte[] streamToBytes(InputStream is) {

  69. ByteArrayOutputStream os = new ByteArrayOutputStream(1024);

  70. byte[] buffer = new byte[1024];

  71. int len;

  72. try {

  73. while ((len = is.read(buffer)) >= 0) {

  74. os.write(buffer, 0, len);

  75. }

  76. } catch (java.io.IOException e) {

  77. }

  78. return os.toByteArray();

  79. }

  80. //把View转换成Bitmap

  81. /**

  82. * 把一个View的对象转换成bitmap

  83. */

  84. static Bitmap getViewBitmap(View v) {

  85. v.clearFocus();

  86. v.setPressed(false);

  87. //能画缓存就返回false

  88. boolean willNotCache = v.willNotCacheDrawing();

  89. v.setWillNotCacheDrawing(false);

  90. int color = v.getDrawingCacheBackgroundColor();

  91. v.setDrawingCacheBackgroundColor(0);

  92. if (color != 0) {

  93. v.destroyDrawingCache();

  94. }

  95. v.buildDrawingCache();

  96. Bitmap cacheBitmap = v.getDrawingCache();

  97. if (cacheBitmap == null) {

  98. Log.e(TAG, “failed getViewBitmap(” + v + “)”, new RuntimeException());

  99. return null;

  100. }

  101. Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

  102. // Restore the view

  103. v.destroyDrawingCache();

  104. v.setWillNotCacheDrawing(willNotCache);

  105. v.setDrawingCacheBackgroundColor(color);

  106. return bitmap;

  107. }

Java代码   收藏代码

  1. 读取raw资源文件中的mp3文件,然后通过音乐播放器播放:

  2. /**

  3. * 把mp3文件写入卡

  4. *

  5. * @param fileName

  6. *             输出的文件名(全路径)

  7. * @param context

  8. *             context对象

  9. */

  10. private void writeMP3ToSDcard(String fileName, Context context) {

  11. byte[] buffer = new byte[1024 * 8];

  12. int read;

  13. BufferedInputStream bin = new BufferedInputStream(context.getResources().openRawResource(R.raw.ring));

  14. try {

  15. BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(fileName));

  16. while ((read = bin.read(buffer)) > -1) {

  17. bout.write(buffer, 0, read);

  18. }

  19. bout.flush();

  20. bout.close();

  21. bin.close();

  22. } catch (FileNotFoundException e) {

  23. e.printStackTrace();

  24. } catch (IOException e) {

  25. e.printStackTrace();

  26. }

  27. }

  28. Intent intent = new Intent();

  29. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  30. intent.setAction(android.content.Intent.ACTION_VIEW);

  31. intent.setDataAndType(Uri.fromFile(newFile(“XXXXmp3的文件全路径”)),“audio/*”);

  32. startActivity(intent);

绘制图像倒影

Java代码   收藏代码

  1. private void

  2. _Init()

  3. {

  4. m_paint = new Paint(Paint.ANTI_ALIAS_FLAG);

  5. LinearGradient lg = new LinearGradient(

  6. 0, 0, 0, m_nShadowH,

  7. 0xB0FFFFFF, 0x00000000,

  8. Shader.TileMode.CLAMP);

  9. m_paint.setShader(lg);

  10. m_paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

最后

最后这里放上我这段时间复习的资料,这个资料也是偶然一位朋友分享给我的,里面包含了腾讯、字节跳动、阿里、百度2020-2021面试真题解析,并且把每个技术点整理成了视频和PDF(知识脉络 + 诸多细节)。

还有 高级架构技术进阶脑图、高级进阶架构资料 帮助大家学习提升进阶,这里我也免费分享给大家也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

有需要的朋友可以点击:**Android面试资料**免费领取~

一起互勉~

mode(PorterDuff.Mode.MULTIPLY));

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-egL2kDsV-1711166249143)]
[外链图片转存中…(img-31hCLJnp-1711166249143)]
[外链图片转存中…(img-KYQKDuuw-1711166249144)]
[外链图片转存中…(img-JiQfUBvS-1711166249144)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-SOlFveLm-1711166249144)]

最后

最后这里放上我这段时间复习的资料,这个资料也是偶然一位朋友分享给我的,里面包含了腾讯、字节跳动、阿里、百度2020-2021面试真题解析,并且把每个技术点整理成了视频和PDF(知识脉络 + 诸多细节)。

还有 高级架构技术进阶脑图、高级进阶架构资料 帮助大家学习提升进阶,这里我也免费分享给大家也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

有需要的朋友可以点击:**Android面试资料**免费领取~

[外链图片转存中…(img-34LIliAt-1711166249145)]

[外链图片转存中…(img-LcSUKHYW-1711166249145)]

一起互勉~

  • 27
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值