一个是ImageSplitter类,有一个静态方法split,传入的参数是要切割的Bitmap对象,和横向和竖向的切割片数。比如传入的是3、3,则横竖向都切割成3片,最终会将整个图片切割成3X3=9片。
-
import java.util.ArrayList;
-
import java.util.List;
-
import android.graphics.Bitmap;
-
public class ImageSplitter {
-
public static List split(Bitmap bitmap, int xPiece, int yPiece) {
-
List pieces = new ArrayList(xPiece * yPiece);
-
int width = bitmap.getWidth();
-
int height = bitmap.getHeight();
-
int pieceWidth = width / 3;
-
int pieceHeight = height / 3;
-
for (int i = 0; i < yPiece; i++) {
-
for (int j = 0; j < xPiece; j++) {
-
ImagePiece piece = new ImagePiece();
-
piece.index = j + i * xPiece;
-
int xValue = j * pieceWidth;
-
int yValue = i * pieceHeight;
-
piece.bitmap = Bitmap.createBitmap(bitmap, xValue, yValue,
-
pieceWidth, pieceHeight);
-
pieces.add(piece);
-
}
-
}
-
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,