【问题标题】:Trying to mirror a picture with java android canvas.drawImage尝试使用 java android canvas.drawImage 镜像图片
【发布时间】:2013-08-26 19:41:12
【问题描述】:

我在尝试使用 java android canvas.drawImage 旋转图片时遇到问题。我正在做一个小游戏,我正在使用我的 drawImage 函数在屏幕上绘制不同的图片。但是现在我想旋转一些小图像,为此我创建了一个名为 drawMirroredImage 的函数。但是现在这些小图像不会出现在同一个地方。

这是我的代码:

public void drawImage(Image Image, int x, int y) {
    canvas.drawBitmap(((AndroidImage) Image).bitmap, x, y, null);
}

public void drawMirroredImage(Image Image, int x, int y) {
    canvas.save();
    canvas.scale(-1.0f, 1.0f);
    canvas.drawBitmap(((AndroidImage) Image).bitmap, x - canvas.getWidth(), y, null);
    canvas.restore();   
}

谁知道我做错了什么?

非常感谢您的帮助

【问题讨论】:

  • 您是否要显示该图像的反射...?
  • 是的普鲁,这就是我想要的>_

标签: java android android-canvas drawimage mirror


【解决方案1】:

以下内容对您有用。 我在 SO 本身的某个地方找到了它,但不记得在哪里。

public static Bitmap getReflectionedBitmap(Context context,int resourceId,Bitmap originalImage,int reflectionGap) {

        if(originalImage==null)
        originalImage = BitmapFactory.decodeResource(context.getResources(),resourceId);

        int width = originalImage.getWidth();
        int height = originalImage.getHeight();

        // This will not scale but will flip on the Y axis
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);
        // Create a Bitmap with the flip matix applied to it.
        // We only want the bottom half of the image
        Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
                height / 2, width, height / 2, matrix, false);

        // Create a new bitmap with same width but taller to fit reflection

        Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
                (height + height / 2), Config.ARGB_8888);
        // Create a new Canvas with the bitmap that's big enough for
        // the image plus gap plus reflection

        Canvas canvas = new Canvas(bitmapWithReflection);
        // Draw in the original image
        canvas.drawBitmap(originalImage, 0, 0, null);
        // Draw in the gap
        Paint deafaultPaint = new Paint();
        deafaultPaint.setColor(0xffffffff);
        canvas.drawRect(0, height, width, height + reflectionGap , deafaultPaint);

        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0,
                originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
                        + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
        // Set the paint to use this shader (linear gradient)
        paint.setShader(shader);
        // Set the Transfer mode to be porter duff and destination in
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
                + reflectionGap, paint);

        return bitmapWithReflection;
    }

我稍微调整了 sn-p 以将其与资源图像一起使用。

如果您想为资源中的图像创建反射,请传入null 作为参数来代替Bitmap

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多