简体   繁体   English

尝试使用java android canvas.drawImage镜像图片

[英]Trying to mirror a picture with java android canvas.drawImage

I'm having problems while trying to rotate a picture with java android canvas.drawImage. 我试图用java android canvas.drawImage旋转图片时遇到问题。 I'm doing a little game, and I'm painting different pictures on the screen using my drawImage function. 我正在做一个小游戏,我正在使用我的drawImage函数在屏幕上绘制不同的图片。 However now I want to rotate some little images, I have created a function called drawMirroredImage for this. 但是现在我想旋转一些小图像,我为此创建了一个名为drawMirroredImage的函数。 However now this little images don't appear on the same place. 但是现在这些小图像不会出现在同一个地方。

Here is my code: 这是我的代码:

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();   
}

Anyone knows what I'm doing wrong? 谁知道我做错了什么?

Lot of thanks for helping 非常感谢帮助

Following will work for you. 以下将为您服务。 I found it somewhere on SO itself but don't remember where. 我发现它本身就在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;
    }

I tweaked the snippet a little bit to use it with resource images as well. 我稍微调整了片段,以便将它与资源图像一起使用。

Pass in null as parameter in place of Bitmap if you want to create reflection for images in resource. 如果要为资源中的图像创建反射,则将null作为参数代替Bitmap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM