How to toggle flip Imageview horizontally in android

孤人 提交于 2020-01-06 19:29:39

问题


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.

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.setImageBitmap(flipImage(BitmapFactory.decodeResource(getResources(), R.drawable.doom01),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.




回答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



来源:https://stackoverflow.com/questions/30193353/how-to-toggle-flip-imageview-horizontally-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!