如何将扩散效果制作到位图

时间:2015-05-21 11:08:12

标签: android bitmap convolution

在我的应用程序中,我有文本,我正在转换为位图,然后我需要它顺利地变成云,然后消失。我正在使用卷积矩阵来模糊位图并使其消失。

我不知道如何模糊扩展位图。通过模糊扩展我的意思是使位图中的图片更大更平滑。它像飞机一样。首先它有锋利的边缘,然后它扩散然后它消失。 有关传播图片的想法吗?

blur-dissarear的初步代码(不理想,需要改进)

           new Runnable() {
                @Override
                public void run() {
                    if (k < 150) {
                        k++;
                        ((TextView)view.findViewById(R.id.textView)).setText(k + "");

                        bitmapEffect = createBitmap_convolve(bitmapEffect, matrix_blur, 0.98f);

                        image1.setImageBitmap(bitmapEffect);
                        new Handler().postDelayed(this, 16);
                    }
                }
            });

private Bitmap createBitmap_convolve(Bitmap src, float[] coefficients, float factor) {

    Bitmap result = Bitmap.createBitmap(src.getWidth(),
            src.getHeight(), src.getConfig());

    RenderScript renderScript = RenderScript.create(activity);

    Allocation input = Allocation.createFromBitmap(renderScript, src);
    Allocation output = Allocation.createFromBitmap(renderScript, result);

    ScriptIntrinsicConvolve3x3 convolution = ScriptIntrinsicConvolve3x3
            .create(renderScript, Element.U8_4(renderScript));
    convolution.setInput(input);

    normalize(coefficients, factor);

    convolution.setCoefficients(coefficients);
    convolution.forEach(output);

    output.copyTo(result);
    renderScript.destroy();
    return result;
}

private void normalize(float[] coefficients, float normalizeFactor) {
        float sum = 0;
        for (int i = 0; i < coefficients.length; i++) {
            sum += coefficients[i];
        }
        if(sum == 0){
            sum = 1;
        }
        for (int i = 0; i < coefficients.length; i++) {
            coefficients[i] = coefficients[i] / sum * normalizeFactor;
        }
    }

float[] matrix_blur =
        { 1.0f, 1.0f, 1.0f,
          1.0f, 1.0f, 1.0f,
          1.0f, 1.0f, 1.0f};

1 个答案:

答案 0 :(得分:0)

您想调整/拉伸您的位图吗?!

Bitmap类有一个Bitmap.createScaledBitmap方法(位图,新宽度,新高度,true);