所以我正在做一个像“Spot The Difference”这样的游戏,虽然我可以找到差异的中心,但我无法正确地在它上面放置一个形状。使用“Paint Code”我生成了一个类,它可以帮助我创建形状但是仍然存在放置问题。我需要在找到的差异的中心添加许多圆形形状。这是形状类和我到目前为止所尝试的:
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Path;
public class StyleKit {
// Resizing Behavior
public enum ResizingBehavior {
AspectFit, //!< The content is proportionally resized to fit into the target rectangle.
AspectFill, //!< The content is proportionally resized to completely fill the target rectangle.
Stretch, //!< The content is stretched to match the entire target rectangle.
Center, //!< The content is centered in the target rectangle, but it is NOT resized.
}
// Canvas Drawings
// Tab
private static class CacheForRoundImage {
private static Paint paint = new Paint();
private static RectF ovalRect = new RectF();
private static Path ovalPath = new Path();
}
private static void drawRoundImage(Canvas canvas, RectF frame, int borderColor) {
// General Declarations
Paint paint = CacheForRoundImage.paint;
// Oval
RectF ovalRect = CacheForRoundImage.ovalRect;
ovalRect.set(frame.left + 2f,
frame.top + 2f,
frame.right - 2f,
frame.bottom - 2f);
Path ovalPath = CacheForRoundImage.ovalPath;
ovalPath.reset();
ovalPath.addOval(ovalRect, Path.Direction.CW);
paint.reset();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(3.5f);
paint.setStrokeMiter(10f);
canvas.save();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(borderColor);
canvas.drawPath(ovalPath, paint);
canvas.restore();
}
// Canvas Images
// Tab
public static Bitmap imageOfRoundImage(PointF imageSize, int borderColor) {
Bitmap imageOfRoundImage = Bitmap.createBitmap((int) imageSize.x, (int) imageSize.y, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(imageOfRoundImage);
StyleKit.drawRoundImage(canvas, new RectF(0f, 0f, imageSize.x/3, imageSize.y/3), borderColor);
return imageOfRoundImage;
}
// Resizing Behavior
public static void resizingBehaviorApply(ResizingBehavior behavior, RectF rect, RectF target, RectF result) {
if (rect.equals(target) || target == null) {
result.set(rect);
return;
}
if (behavior == ResizingBehavior.Stretch) {
result.set(target);
return;
}
float xRatio = Math.abs(target.width() / rect.width());
float yRatio = Math.abs(target.height() / rect.height());
float scale = 0f;
switch (behavior) {
case AspectFit: {
scale = Math.min(xRatio, yRatio);
break;
}
case AspectFill: {
scale = Math.max(xRatio, yRatio);
break;
}
case Center: {
scale = 1f;
break;
}
}
float newWidth = Math.abs(rect.width() * scale);
float newHeight = Math.abs(rect.height() * scale);
result.set(target.centerX() - newWidth / 2,
target.centerY() - newHeight / 2,
target.centerX() + newWidth / 2,
target.centerY() + newHeight / 2);
}
}
这些是显示形状的方法:
private void paintOverImage(int[] centerOfShape) {
Bitmap firstImage = BitmapFactory.decodeResource(getResources(), R.drawable.space1);
PointF pointF = new PointF(125, 125);
Bitmap overlayImage = StyleKit.imageOfRoundImage(pointF, Color.BLACK);
Bitmap mergedImages = mergeImages(firstImage, overlayImage, centerOfShape);
changedImageImv.setImageBitmap(mergedImages);
}
private Bitmap mergeImages(Bitmap firstImage, Bitmap secondImage, int[] centerOfShape) {
Bitmap result = Bitmap.createBitmap(firstImage.getWidth(), firstImage.getHeight(), firstImage.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(firstImage, 0f, 0f, null);
canvas.drawBitmap(secondImage, 10, 10, null);
return result;
}
在所有这些代码行之后没有任何反应。 StyleKit类是正确的,我在以下方法中弄乱了一些东西。 centerOfShape表还包含值,这些值是形状应该出现的坐标。
答案 0 :(得分:0)
显然我一直在加载原始图像。这是解决方案:
Bitmap firstImage = ((BitmapDrawable) changedImageImv.getDrawable()).getBitmap();