如何在android中水平切换翻转Imageview

时间:2021-05-12 17:11:04

I have an Imageview and I want to flip picture horizontally whenever user click on the Button named "Flip Picture" . and when User click this button second time it should return to orignal state in other words flip back.

我有一个Imageview,每当用户点击名为“翻转图片”的按钮时,我想要水平翻转图片。当用户第二次点击此按钮时,它应该返回到原始状态,换句话说就是向后翻转。

So it should repeat this behavior. I found this useful code to flip the image view with out using external library , but do not know how to flip back:

所以它应该重复这种行为。我找到了这个有用的代码来翻转图像视图而不使用外部库,但不知道如何翻转:

Here is the Code :

这是代码:

 public Bitmap flipImage(Bitmap src, int type) {
    // create new matrix for transformation
    Matrix matrix = new Matrix();
    // if vertical
    if(type == FLIP_VERTICAL) {
        // y = y * -1
        matrix.preScale(1.0f, -1.0f);
    }
    // if horizonal
    else if(type == FLIP_HORIZONTAL) {
        // x = x * -1


        // unknown type
    } else {
        return null;
    }

    // return transformed image
    return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}

and here is how I am trying to apply it on my Image view named FlipImage

以下是我试图将其应用于名为FlipImage的Image视图

Flipimage.setImageBitmap(flipImage(BitmapFactory.decodeResource(getResources(), R.drawable.doom01),2));

2 个解决方案

#1


I found Answer My self while testing and trying : Just flip the values it will flip the image view back : Here is the magic in this line :

我在测试和尝试时找到了自己的答案:只需翻转它将翻转图像视图的值:这是这一行中的魔力:

On first click :

点击第一次:

matrix.preScale(-1.0f, 1.0f);

On Second Click :

二次点击:

matrix.preScale(1.0f, -1.0f);

So you can initialize the counter and also can use Toggle button of android.

所以你可以初始化计数器,也可以使用android的Toggle按钮。

#2


This is a good code that will help you solve your problem. Pass a bitmap image to the function and the function returns a bitmap data type. ... or download source code

这是一个很好的代码,可以帮助您解决问题。将位图图像传递给函数,函数返回位图数据类型。 ...或下载源代码

public Bitmap FlipHorizontally(Bitmap originalImage) {
        // The gap we want between the flipped image and the original image
        final int flipGap = 4;


        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 matrix applied to it.
        // We only want the bottom half of the image
        Bitmap flipImage = Bitmap.createBitmap(originalImage, 0,0 , width, height, matrix, true);

        // Create a new bitmap with same width but taller to fit reflection
        Bitmap bitmapWithFlip = Bitmap.createBitmap((width + width + flipGap), height, Bitmap.Config.ARGB_8888);

        // Create a new Canvas with the bitmap that's big enough for
        Canvas canvas = new Canvas(bitmapWithFlip);

        //Draw original image
        canvas.drawBitmap(originalImage, 0, 0, null);

       //Draw the Flipped Image
        canvas.drawBitmap(flipImage, width+flipGap, 0, null);


        return bitmapWithFlip;
    }

Learn more

#1


I found Answer My self while testing and trying : Just flip the values it will flip the image view back : Here is the magic in this line :

我在测试和尝试时找到了自己的答案:只需翻转它将翻转图像视图的值:这是这一行中的魔力:

On first click :

点击第一次:

matrix.preScale(-1.0f, 1.0f);

On Second Click :

二次点击:

matrix.preScale(1.0f, -1.0f);

So you can initialize the counter and also can use Toggle button of android.

所以你可以初始化计数器,也可以使用android的Toggle按钮。

#2


This is a good code that will help you solve your problem. Pass a bitmap image to the function and the function returns a bitmap data type. ... or download source code

这是一个很好的代码,可以帮助您解决问题。将位图图像传递给函数,函数返回位图数据类型。 ...或下载源代码

public Bitmap FlipHorizontally(Bitmap originalImage) {
        // The gap we want between the flipped image and the original image
        final int flipGap = 4;


        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 matrix applied to it.
        // We only want the bottom half of the image
        Bitmap flipImage = Bitmap.createBitmap(originalImage, 0,0 , width, height, matrix, true);

        // Create a new bitmap with same width but taller to fit reflection
        Bitmap bitmapWithFlip = Bitmap.createBitmap((width + width + flipGap), height, Bitmap.Config.ARGB_8888);

        // Create a new Canvas with the bitmap that's big enough for
        Canvas canvas = new Canvas(bitmapWithFlip);

        //Draw original image
        canvas.drawBitmap(originalImage, 0, 0, null);

       //Draw the Flipped Image
        canvas.drawBitmap(flipImage, width+flipGap, 0, null);


        return bitmapWithFlip;
    }

Learn more