3

我正在尝试创建一个像这里找到的那​​样的实时万花筒应用程序:http: //www.windowsphone.com/en-gb/store/app/live-kaleidoscope/32e41ccf-5a25-42ea-84ff-07688f3e6aa5

为此,我已经能够获得与相机预览相对应的位图。我正在尝试从三角形路径创建一个多边形,并在使用矩阵变换操作位图后用位图填充路径。我还没有进行这些转换,但是现在我什至看不到多边形和其中的图像。我的代码中没有任何错误。当我运行应用程序时,我只看到相机正在运行。我被困住了!请帮忙!这是我的代码:

公共类万花筒扩展视图

{

Bitmap imageData;
Canvas canvas1;
int picWidth, picHeight, ang, displayWidth, displayHeight, numOfsides;
float triangleSide, triangleHeight;

public Kaleidoscope(Context context) {
    super(context);
}

public Kaleidoscope(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void init(Bitmap image, int sides, int w, int h) {

    numOfsides = sides;
    triangleSide = (w - 100) / 2;
    // triangleHeight=(float)Math.tan(Math.PI/3)*triangleSide/2;
    displayWidth = w;
    displayHeight = h;
    imageData = image;
    draw();
}

public void onDraw(Canvas canvas) {
    canvas1 = canvas;
}

public void draw() {

    float angle = 360 / numOfsides;
    float totalAngle = 0;
    float centerX = displayWidth / 2;
    float centerY = displayHeight / 2;
    float cornerX = centerX + triangleSide;
    float cornerY = centerY / 2;
    Matrix mat = new Matrix();

    mat.postTranslate(picWidth / 2, 0);

    // Matrix mat2=mat;
    //
    // float concatArray[]={-1,0,0,1,0,0};
    // Matrix concatMat=new Matrix();
    // concatMat.setValues(concatArray);
    // mat2.postConcat(concatMat);

    for (int i = 0; i < numOfsides; i++) {

        float verticalDistance = (triangleSide * (float) (Math.sin(Math
                .toRadians(totalAngle))));
        float horizontalDistance = (triangleSide * (float) (Math.cos(Math
                .toRadians(totalAngle))));

        float x = (displayWidth / 2) + horizontalDistance - (picWidth / 2);
        float y = (displayHeight / 2) + verticalDistance - (picHeight / 2);

        Path triangle = new Path();
        triangle.lineTo(cornerX, cornerY);
        triangle.lineTo(x, y);
        triangle.lineTo(centerX, centerY);
        cornerX = x;
        cornerY = y;
        totalAngle = totalAngle + angle;
        Bitmap matBitmap;

        if (i % 2 == 0) {
            matBitmap = Bitmap.createBitmap(imageData, 0, 0,
                    imageData.getWidth(), imageData.getHeight(), mat, true);
        } else {
            matBitmap = Bitmap.createBitmap(imageData, 0, 0,
                    imageData.getWidth(), imageData.getHeight(), mat, true);
        }

        BitmapShader fillBMPshader = new BitmapShader(matBitmap,
                Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
        Paint fill = new Paint();
        fill.setColor(0xFFFFFFFF);
        fill.setStyle(Paint.Style.FILL);
        fill.setShader(fillBMPshader);

        canvas1.drawPath(triangle, fill);

    }
}

}

4

1 回答 1

0

首先,对不起我的英文写作。(因为我是韩国人)

如果你在 android 中使用摄像头,则名为的类preview将扩展SurfaceView并实现SurfaceHolder.callback.

然后mainActivity调用这个类。正确的?

mainActivity调用该类后preview,您必须创建另一个扩展类的View类。

make 方法命名onDraw为覆盖:onDraw(Canvas canvas)并在此方法中创建源,如下所示:

mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
mPaint.setTextSize(40);
canvas.drawText("X", canvas.getWidth() / 2, canvas.getHeight() / 2, mPaint);

然后回去mainActivityaddView

此源中“视图类”的名称是“测试”。

final FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
if (camview == null) {
    camview = new Preview(this);
    frame.addView(camview);
    test = new InvalidateTest(context);
    frame.addView(test, new LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}

总之,只需创建新类并将此视图添加到(框架)布局中。

于 2013-02-26T11:17:03.397 回答