How to flip ImageView in Android?

后端 未结 3 386
清歌不尽
清歌不尽 2021-01-12 06:40

I am working on an application I need to flip ImageView on touch and transfer control to the second activity.

Please help me.

I tried a lot b

3条回答
  •  萌比男神i
    2021-01-12 07:10

    You dont need to use any library, you can try following simple function to flip imageview either horizontally or vertically,

        final static int FLIP_VERTICAL = 1;
        final static int FLIP_HORIZONTAL = 2;
        public static Bitmap flip(Bitmap src, int type) {
                // create new matrix for transformation
                Matrix matrix = new Matrix();
                // if vertical
                if(type == FLIP_VERTICAL) {
                    matrix.preScale(1.0f, -1.0f);
                }
                // if horizonal
                else if(type == FLIP_HORIZONTAL) {
                    matrix.preScale(-1.0f, 1.0f);
                // unknown type
                } else {
                    return null;
                }
    
                // return transformed image
                return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
            }
    

    You will have to pass bitmap associated with imageview to be flipped and flipping type. For example,

    ImageView myImageView = (ImageView) findViewById(R.id.myImageView);
    Bitmap bitmap = ((BitmapDrawable)myImageView.getDrawable()).getBitmap(); // get bitmap associated with your imageview
    myImageView .setImageBitmap(flip(bitmap ,FLIP_HORIZONTAL));
    

提交回复
热议问题