如何在android中像素化位图

Dev*_*ice 1 android pixel effects

我一直在搜索,但我没有找到关于如何在android中像素化位图的问题

一个

请注意,我不是说模糊

And*_*onu 6

您可以尝试将该图像调整为较小的版本(如果由于某种原因需要与原始图像相同的尺寸,则备份)

{
    Bitmap original = ...;
    //Calculate proportional size, or make the method accept just a factor of scale.
    Bitmap small = getResigetResizedBitmap(original, smallWidth, smallHeight);
    Bitmap pixelated = getResigetResizedBitmap(small, normalWidth, normalHeight);
   //Recycle small, recycle original if no longer needed.
}

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(
        bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}
Run Code Online (Sandbox Code Playgroud)

代码来自这里