我正在尝试创建一个像这里找到的那样的实时万花筒应用程序: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);
}
}
}