ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android图片处理,flutter跳转动画

2021-12-29 14:33:26  阅读:245  来源: 互联网

标签:int 转动 height width bitmap new android Bitmap flutter


  1. final Resources resources = context.getResources();

  2. sIconWidth = sIconHeight = (int) resources.getDimension(

  3. android.R.dimen.app_icon_size);

  4. }

  5. int width = sIconWidth;

  6. int height = sIconHeight;

  7. final int bitmapWidth = bitmap.getWidth();

  8. final int bitmapHeight = bitmap.getHeight();

  9. if (width > 0 && height > 0) {

  10. if (width < bitmapWidth || height < bitmapHeight) {

  11. final float ratio = (float) bitmapWidth / bitmapHeight;

  12. if (bitmapWidth > bitmapHeight) {

  13. height = (int) (width / ratio);

  14. } else if (bitmapHeight > bitmapWidth) {

  15. width = (int) (height * ratio);

  16. }

  17. final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ?

  18. bitmap.getConfig() : Bitmap.Config.ARGB_8888;

  19. final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);

  20. final Canvas canvas = sCanvas;

  21. final Paint paint = sPaint;

  22. canvas.setBitmap(thumb);

  23. paint.setDither(false);

  24. paint.setFilterBitmap(true);

  25. sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);

  26. sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);

  27. canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);

  28. return thumb;

  29. } else if (bitmapWidth < width || bitmapHeight < height) {

  30. final Bitmap.Config c = Bitmap.Config.ARGB_8888;

  31. final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);

  32. final Canvas canvas = sCanvas;

  33. final Paint paint = sPaint;

  34. canvas.setBitmap(thumb);

  35. paint.setDither(false);

  36. paint.setFilterBitmap(true);

  37. canvas.drawBitmap(bitmap, (sIconWidth - bitmapWidth) / 2,

  38. (sIconHeight - bitmapHeight) / 2, paint);

  39. return thumb;

  40. }

  41. }

  42. return bitmap;

  43. }

  44. }

Java代码   收藏代码

  1. //Android Matrix类实现镜像方法

  2. public void drawRegion(Image image_src,

  3. int x_src, int y_src,

  4. int width, int height,

  5. int transform,

  6. int x_dest, int y_dest,

  7. int anchor){

  8. if((anchor&VCENTER) != 0){

  9. y_dest -= height/2;

  10. }else if((anchor&BOTTOM) != 0){

  11. y_dest -= height;

  12. }

  13. if((anchor&RIGHT) != 0){

  14. x_dest -= width;

  15. }else if((anchor&HCENTER) != 0){

  16. x_dest -= width/2;

  17. }

  18. Bitmap newMap = Bitmap.createBitmap(image_src.getBitmap(), x_src, y_src, width, height);

  19. Matrix mMatrix = new Matrix();

  20. Matrix temp = new Matrix();

  21. Matrix temp2 = new Matrix();

  22. float[] mirrorY = {

  23. -1, 0, 0,

  24. 0, 1, 0,

  25. 0, 0, 1

  26. };

  27. temp.setValues(mirrorY);

  28. switch(transform){

  29. case Sprite.TRANS_NONE:

  30. break;

  31. case Sprite.TRANS_ROT90:

  32. mMatrix.setRotate(90,width/2, height/2);

  33. break;

  34. case Sprite.TRANS_ROT180:

  35. mMatrix.setRotate(180,width/2, height/2);

  36. break;

  37. case Sprite.TRANS_ROT270:

  38. mMatrix.setRotate(270,width/2, height/2);

  39. break;

  40. case Sprite.TRANS_MIRROR:

  41. mMatrix.postConcat(temp);

  42. break;

  43. case Sprite.TRANS_MIRROR_ROT90:

  44. mMatrix.postConcat(temp);

  45. mMatrix.setRotate(90,width/2, height/2);

  46. break;

  47. case Sprite.TRANS_MIRROR_ROT180:

  48. mMatrix.postConcat(temp);

  49. mMatrix.setRotate(180,width/2, height/2);

  50. break;

  51. case Sprite.TRANS_MIRROR_ROT270:

  52. mMatrix.postConcat(temp);

  53. mMatrix.setRotate(270,width/2, height/2);

  54. break;

  55. }

  56. mMatrix.setTranslate(x_dest, y_dest);

  57. canvas.drawBitmap(newMap, mMatrix, mPaint);

  58. }

Java代码   收藏代码

  1. //图片Url保存为位图并进行缩放操作

  2. //通过传入图片url获取位图方法

  3. public Bitmap returnBitMap(String url) {

  4. URL myFileUrl = null;

  5. Bitmap bitmap = null;

  6. try {

  7. myFileUrl = new URL(url);

  8. } catch (MalformedURLException e) {

  9. e.printStackTrace();

  10. }

  11. try {

  12. HttpURLConnection conn = (HttpURLConnection) myFileUrl

  13. .openConnection();

  14. conn.setDoInput(true);

  15. conn.connect();

  16. InputStream is = conn.getInputStream();

  17. bitmap = BitmapFactory.decodeStream(is);

  18. is.close();

  19. } catch (IOException e) {

  20. e.printStackTrace();

  21. }

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

  23. return bitmap;

  24. }

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

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

  27. // load the origial Bitmap

  28. Bitmap BitmapOrg = bitmap;

  29. int width = BitmapOrg.getWidth();

  30. int height = BitmapOrg.getHeight();

  31. int newWidth = w;

  32. int newHeight = h;

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

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

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

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

  37. // calculate the scale

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

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

  40. // create a matrix for the manipulation

  41. Matrix matrix = new Matrix();

  42. // resize the Bitmap

  43. matrix.postScale(scaleWidth, scaleHeight);

  44. // if you want to rotate the Bitmap

  45. // matrix.postRotate(45);

  46. // recreate the new Bitmap

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

  48. height, matrix, true);

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

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

  51. return new BitmapDrawable(resizedBitmap);

  52. }

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 ba

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整资料开源分享

os.toByteArray();

  1. }

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

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

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

  5. }

  6. else {

  7. return null;

  8. }

  9. }

  10. /**

  11. * create the bitmap from a byte array

  12. *生成水印图片

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

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

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

  16. */

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

  18. {

  19. String tag = “createBitmap”;

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

  21. if( src == null )

  22. {

  23. return null;

  24. }

  25. int w = src.getWidth();

  26. int h = src.getHeight();

  27. int ww = watermark.getWidth();

  28. int wh = watermark.getHeight();

  29. //create the new blank bitmap

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

  31. Canvas cv = new Canvas( newb );

  32. //draw src into

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

  34. //draw watermark into

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

  36. //save all clip

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

  38. //store

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

  40. return newb;

  41. }

  42. /** 重新编码Bitmap

  43. *

  44. * @param src

  45. *          需要重新编码的Bitmap

  46. *

  47. * @param format

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

  49. *

  50. * @param quality

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

  52. *

  53. * @return

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

  55. */

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

  57. int quality) {

  58. ByteArrayOutputStream os = new ByteArrayOutputStream();

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

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

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

  62. }

  63. //Stream转换成Byte

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

  65. ByteArrayOutputStream os = new ByteArrayOutputStream(1024);

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

  67. int len;

  68. try {

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

总结

最后对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

这里附上上述的技术体系图相关的几十套腾讯、头条、阿里、美团等公司20年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。

  1. int quality) {

  2. ByteArrayOutputStream os = new ByteArrayOutputStream();

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

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

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

  6. }

  7. //Stream转换成Byte

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

  9. ByteArrayOutputStream os = new ByteArrayOutputStream(1024);

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

  11. int len;

  12. try {

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

总结

最后对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

这里附上上述的技术体系图相关的几十套腾讯、头条、阿里、美团等公司20年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:
[外链图片转存中…(img-Z6p2Xb7r-1640759041380)]

[外链图片转存中…(img-ASNgZajU-1640759041381)]

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

标签:int,转动,height,width,bitmap,new,android,Bitmap,flutter
来源: https://blog.csdn.net/m0_65686805/article/details/122214998

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有