我想创建类似于这个问题的东西我可以将图像转换为点网格吗?但我找不到我的问题的任何答案。基本思想是从手机加载一张图片并应用这个点网格。我将不胜感激任何建议。
问问题
1384 次
2 回答
3
正如其他人可能建议的那样,您的问题也可以使用 OpenGL 着色语言 (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
为一个。如果您愿意,可以将结果用作 some 的 src 。或者,您可以将其转换为 a并将其保存到磁盘。Drawable
LayerDrawable
ImageView
Drawable
Bitmap
于 2013-02-01T06:08:16.963 回答
1
于 2013-02-01T03:51:35.587 回答