This question already has answers here:
Can I convert an image into a grid of dots?
                                
                                    (3个答案)
                                
                        
                                6年前关闭。
            
                    
我想创建与此问题Can I convert an image into a grid of dots?类似的内容,但找不到我的问题的任何答案。基本思想是从手机中加载图片并应用此点网格。我对此表示任何建议。

最佳答案

正如其他人可能暗示的那样,您也可以使用OpenGL Shading Language(GLSL)中的片段着色器解决您的问题。 GLSL可能需要痛苦的设置。

这是我使用Android Renderscript的解决方案(与GLSL非常相似,但是是专门为Android设计的。使用率不高)。首先,从官方的Android SDK示例中设置Renderscript> Hello Compute示例。接下来,将mono.rs替换为以下内容:

#pragma version(1)
#pragma rs java_package_name(com.android.example.hellocompute)

rs_allocation gIn;
rs_allocation gOut;
rs_script gScript;

static int mImageWidth;
const uchar4 *gPixels;

const float4 kBlack = {
    0.0f, 0.0f, 0.0f, 1.0f
};

// There are two radius's for each circle for anti-aliasing reasons.
const static uint32_t radius = 15;
const static uint32_t smallerRadius = 13;

// Used so that we have smooth circle edges
static float smooth_step(float start_threshold, float end_threshold, float value) {
    if (value < start_threshold) {
        return 0;
    }
    if (value > end_threshold) {
        return 1;
    }
    value = (value - start_threshold)/(end_threshold - start_threshold);
    // As defined at http://en.wikipedia.org/wiki/Smoothstep
    return value*value*(3 - 2*value);
}

void root(const uchar4 *v_in, uchar4 *v_out, uint32_t u_x, uint32_t u_y) {
    int32_t diameter = radius * 2;

    // Compute distance from center of the circle
    int32_t x = u_x % diameter - radius;
    int32_t y = u_y % diameter - radius;
    float dist = hypot((float)x, (float)y);

    // Compute center of the circle
    uint32_t center_x = u_x /diameter*diameter + radius;
    uint32_t center_y = u_y /diameter*diameter + radius;

    float4 centerColor = rsUnpackColor8888(gPixels[center_x + center_y*mImageWidth]);
    float amount = smooth_step(smallerRadius, radius, dist);
    *v_out = rsPackColorTo8888(mix(centerColor, kBlack, amount));
}

void filter() {
    mImageWidth = rsAllocationGetDimX(gIn);
    rsForEach(gScript, gIn, gOut); // You may need a forth parameter, depending on your target SDK.
}


在HelloCompute.java中,将createScript()替换为以下内容:

private void createScript() {
      mRS = RenderScript.create(this);

        mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn,
                                                    Allocation.MipmapControl.MIPMAP_NONE,
                                                    Allocation.USAGE_SCRIPT);
        mOutAllocation = Allocation.createTyped(mRS, mInAllocation.getType());

        mScript = new ScriptC_mono(mRS, getResources(), R.raw.mono);

        mScript.bind_gPixels(mInAllocation);

        mScript.set_gIn(mInAllocation);
        mScript.set_gOut(mOutAllocation);
        mScript.set_gScript(mScript);
        mScript.invoke_filter();
        mOutAllocation.copyTo(mBitmapOut);
}


最终结果将如下所示


替代

如果您不希望每个点都是纯色,则可以执行以下操作:

有一个非常简单的方法可以做到这一点。您需要一个BitmapDrawable的图片和一个BitmapDrawable的覆盖图块(我们称它为overlayTile)。在overlayTile上,致电

overlayTile.setTileModeX(Shader.TileMode.REPEAT);
overlayTile.setTileModeY(Shader.TileMode.REPEAT);


接下来,使用LayerDrawable将两个Drawable组合为一个Drawable。如果需要,可以将生成的LayerDrawable用作某些ImageView的src。或者,您可以convert the Drawable to a Bitmap并将其保存到磁盘。

10-08 08:35