android图片处理,保准看明白

  1. return pieces;

  2. }

  3. }

1、图标加灰色过滤;

2、android的图片资源默认是静态的,单实例;如果两个IM好友的头像一样,最简单的都是用的软件自带头像,有一个在线,一个离线,直接改变头像的灰度,则两个用户的头像都会变灰或者在线,答案是:Drawable.mutate()。

Java代码   收藏代码

  1. Drawable mDrawable = context.getResources().getDrawable(R.drawable.face_icon);

  2. //Make this drawable mutable.

  3. //A mutable drawable is guaranteed to not share its state with any other drawable.

  4. mDrawable.mutate();

  5. ColorMatrix cm = new ColorMatrix();

  6. cm.setSaturation(0);

  7. ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);

  8. mDrawable.setColorFilter(cf);

生成缩略图,抠自android launcher源码:

Java代码   收藏代码

  1. /*

  2. * Copyright © 2008 The Android Open Source Project

  3. *

  4. * Licensed under the Apache License, Version 2.0 (the “License”);

  5. * you may not use this file except in compliance with the License.

  6. * You may obtain a copy of the License at

  7. *

  8. *      http://www.apache.org/licenses/LICENSE-2.0

  9. *

  10. * Unless required by applicable law or agreed to in writing, software

  11. * distributed under the License is distributed on an “AS IS” BASIS,

  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  13. * See the License for the specific language governing permissions and

  14. * limitations under the License.

  15. */

  16. package com.android.launcher;

  17. import android.graphics.drawable.BitmapDrawable;

  18. import android.graphics.drawable.Drawable;

  19. import android.graphics.drawable.PaintDrawable;

  20. import android.graphics.Bitmap;

  21. import android.graphics.PixelFormat;

  22. import android.graphics.Canvas;

  23. import android.graphics.PaintFlagsDrawFilter;

  24. import android.graphics.Paint;

  25. import android.graphics.Rect;

  26. import android.content.res.Resources;

  27. import android.content.Context;

  28. /**

  29. * Various utilities shared amongst the Launcher’s classes.

  30. */

  31. final class Utilities {

  32. private static int sIconWidth = -1;

  33. private static int sIconHeight = -1;

  34. private static final Paint sPaint = new Paint();

  35. private static final Rect sBounds = new Rect();

  36. private static final Rect sOldBounds = new Rect();

  37. private static Canvas sCanvas = new Canvas();

  38. static {

  39. sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,

  40. Paint.FILTER_BITMAP_FLAG));

  41. }

  42. /**

  43. * Returns a Drawable representing the thumbnail of the specified Drawable.

  44. * The size of the thumbnail is defined by the dimension

  45. * android.R.dimen.launcher_application_icon_size.

  46. *

  47. * This method is not thread-safe and should be invoked on the UI thread only.

  48. *

  49. * @param icon The icon to get a thumbnail of.

  50. * @param context The application’s context.

  51. *

  52. * @return A thumbnail for the specified icon or the icon itself if the

  53. *         thumbnail could not be created.

  54. */

  55. static Drawable createIconThumbnail(Drawable icon, Context context) {

  56. if (sIconWidth == -1) {

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

  58. sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);

  59. }

  60. int width = sIconWidth;

  61. int height = sIconHeight;

  62. float scale = 1.0f;

  63. if (icon instanceof PaintDrawable) {

  64. PaintDrawable painter = (PaintDrawable) icon;

  65. painter.setIntrinsicWidth(width);

  66. painter.setIntrinsicHeight(height);

  67. } else if (icon instanceof BitmapDrawable) {

  68. // Ensure the bitmap has a density.

  69. BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;

  70. Bitmap bitmap = bitmapDrawable.getBitmap();

  71. if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {

  72. bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());

  73. }

  74. }

  75. int iconWidth = icon.getIntrinsicWidth();

  76. int iconHeight = icon.getIntrinsicHeight();

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

  78. if (width < iconWidth || height < iconHeight || scale != 1.0f) {

  79. final float ratio = (float) iconWidth / iconHeight;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值