如何修改bitmapmesh示例以包含鱼眼效果?

时间:2011-03-31 20:53:17

标签: android graphics fisheye

我有一些Android附带的示例代码扭曲了位图图像(Bitmapmesh.java)。我想要在我的图像上放一个圆圈,它会产生鱼眼效果。我是android的新手,特别是图形,是否可以在bitmapmesh示例中创建此效果?

我不知道从哪里开始,所以任何指针都会受到赞赏。谁能给我一个关于所涉及内容的高级视图,例如我想首先在图像上放置一个圆圈。我之前将按钮放在图像上似乎浮动,这是通过使用相对布局然后添加子按钮来完成的。我现在要做的是不同的,可能会涉及调用一些onDraw方法?我也有一个算法来做失真,我只是不知道如何将它应用到图像。

下面是bitmapmesh代码。任何人都可以告诉我从哪里开始,即使它只是将圆圈放在图像上,然后我可以解决实现效果。

感谢垫

import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream;

import android.content.Context; import android.graphics。; import android.os.Bundle; import android.os.Environment; import android.view。; import android.util.FloatMath;

公共类BitMapFishEye扩展了GraphicsActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new SampleView(this));
}

private static class SampleView extends View {
    private static final int WIDTH = 20;
    private static final int HEIGHT = 20;
    private static final int COUNT = (WIDTH + 1) * (HEIGHT + 1);

    private final Bitmap mBitmap;
    private final float[] mVerts = new float[COUNT*2];
    private final float[] mOrig = new float[COUNT*2];

    private final Matrix mMatrix = new Matrix();
    private final Matrix mInverse = new Matrix();

    private File tempFile;
    private byte[] imageArray;

    private static void setXY(float[] array, int index, float x, float y) {
        array[index*2 + 0] = x;
        array[index*2 + 1] = y;
    }

    public SampleView(Context context) {
        super(context);
        setFocusable(true);

       /* mBitmap = BitmapFactory.decodeResource(getResources(),
                                                 R.drawable.beach);*/
        tempFile = new File(Environment.getExternalStorageDirectory().
                getAbsolutePath() + "/"+"image.jpg");

        imageArray = new byte[(int)tempFile.length()];

     try{

        InputStream is = new FileInputStream(tempFile);
            BufferedInputStream bis = new BufferedInputStream(is);
            DataInputStream dis = new DataInputStream(bis);


            int i = 0;

            while (dis.available() > 0) {
            imageArray[i] = dis.readByte();
            i++;
            }

            dis.close();

     } catch (Exception e) {

               e.printStackTrace();
            }


        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inSampleSize = 5;
        mBitmap = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length, bfo);

        float w = mBitmap.getWidth();
        float h = mBitmap.getHeight();
        // construct our mesh
        int index = 0;
        for (int y = 0; y <= HEIGHT; y++) {
            float fy = h * y / HEIGHT;
            for (int x = 0; x <= WIDTH; x++) {
                float fx = w * x / WIDTH;                    
                setXY(mVerts, index, fx, fy);
                setXY(mOrig, index, fx, fy);
                index += 1;
            }
        }

        mMatrix.setTranslate(10, 10);
        mMatrix.invert(mInverse);
    }

    @Override protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFCCCCCC);

        canvas.concat(mMatrix);
        canvas.drawBitmapMesh(mBitmap, WIDTH, HEIGHT, mVerts, 0,
                              null, 0, null);
    }

    private void warp(float cx, float cy) {
        final float K = 10000;
        float[] src = mOrig;
        float[] dst = mVerts;
        for (int i = 0; i < COUNT*2; i += 2) {
            float x = src[i+0];
            float y = src[i+1];
            float dx = cx - x;
            float dy = cy - y;
            float dd = dx*dx + dy*dy;
            float d = FloatMath.sqrt(dd);
            float pull = K / (dd + 0.000001f);

            pull /= (d + 0.000001f);
         //   android.util.Log.d("skia", "index " + i + " dist=" + d + " pull=" + pull);

            if (pull >= 1) {
                dst[i+0] = cx;
                dst[i+1] = cy;
            } else {
                dst[i+0] = x + dx * pull;
                dst[i+1] = y + dy * pull;
            }
        }
    }

    private int mLastWarpX = -9999; // don't match a touch coordinate
    private int mLastWarpY;

    @Override public boolean onTouchEvent(MotionEvent event) {
        float[] pt = { event.getX(), event.getY() };
        mInverse.mapPoints(pt);

        int x = (int)pt[0];
        int y = (int)pt[1];
        if (mLastWarpX != x || mLastWarpY != y) {
            mLastWarpX = x;
            mLastWarpY = y;
            warp(pt[0], pt[1]);
            invalidate();
        }
        return true;
    }
}

}

0 个答案:

没有答案