-
return pieces;
-
}
-
}
1、图标加灰色过滤;
2、android的图片资源默认是静态的,单实例;如果两个IM好友的头像一样,最简单的都是用的软件自带头像,有一个在线,一个离线,直接改变头像的灰度,则两个用户的头像都会变灰或者在线,答案是:Drawable.mutate()。
-
Drawable mDrawable = context.getResources().getDrawable(R.drawable.face_icon);
-
//Make this drawable mutable.
-
//A mutable drawable is guaranteed to not share its state with any other drawable.
-
mDrawable.mutate();
-
ColorMatrix cm = new ColorMatrix();
-
cm.setSaturation(0);
-
ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);
-
mDrawable.setColorFilter(cf);
生成缩略图,抠自android launcher源码:
-
/*
-
* Copyright © 2008 The Android Open Source Project
-
*
-
* Licensed under the Apache License, Version 2.0 (the “License”);
-
* you may not use this file except in compliance with the License.
-
* You may obtain a copy of the License at
-
*
-
* http://www.apache.org/licenses/LICENSE-2.0
-
*
-
* Unless required by applicable law or agreed to in writing, software
-
* distributed under the License is distributed on an “AS IS” BASIS,
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-
* See the License for the specific language governing permissions and
-
* limitations under the License.
-
*/
-
package com.android.launcher;
-
import android.graphics.drawable.BitmapDrawable;
-
import android.graphics.drawable.Drawable;
-
import android.graphics.drawable.PaintDrawable;
-
import android.graphics.Bitmap;
-
import android.graphics.PixelFormat;
-
import android.graphics.Canvas;
-
import android.graphics.PaintFlagsDrawFilter;
-
import android.graphics.Paint;
-
import android.graphics.Rect;
-
import android.content.res.Resources;
-
import android.content.Context;
-
/**
-
* Various utilities shared amongst the Launcher’s classes.
-
*/
-
final class Utilities {
-
private static int sIconWidth = -1;
-
private static int sIconHeight = -1;
-
private static final Paint sPaint = new Paint();
-
private static final Rect sBounds = new Rect();
-
private static final Rect sOldBounds = new Rect();
-
private static Canvas sCanvas = new Canvas();
-
static {
-
sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
-
Paint.FILTER_BITMAP_FLAG));
-
}
-
/**
-
* Returns a Drawable representing the thumbnail of the specified Drawable.
-
* The size of the thumbnail is defined by the dimension
-
* android.R.dimen.launcher_application_icon_size.
-
*
-
* This method is not thread-safe and should be invoked on the UI thread only.
-
*
-
* @param icon The icon to get a thumbnail of.
-
* @param context The application’s context.
-
*
-
* @return A thumbnail for the specified icon or the icon itself if the
-
* thumbnail could not be created.
-
*/
-
static Drawable createIconThumbnail(Drawable icon, Context context) {
-
if (sIconWidth == -1) {
-
final Resources resources = context.getResources();
-
sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
-
}
-
int width = sIconWidth;
-
int height = sIconHeight;
-
float scale = 1.0f;
-
if (icon instanceof PaintDrawable) {
-
PaintDrawable painter = (PaintDrawable) icon;
-
painter.setIntrinsicWidth(width);
-
painter.setIntrinsicHeight(height);
-
} else if (icon instanceof BitmapDrawable) {
-
// Ensure the bitmap has a density.
-
BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
-
Bitmap bitmap = bitmapDrawable.getBitmap();
-
if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
-
bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
-
}
-
}
-
int iconWidth = icon.getIntrinsicWidth();
-
int iconHeight = icon.getIntrinsicHeight();
-
if (width > 0 && height > 0) {
-
if (width < iconWidth || height < iconHeight || scale != 1.0f) {
-
final float ratio = (float) iconWidth / iconHeight;