如何在安卓系统中翻转ImageView?

8

我正在开发一个应用程序,需要在触摸时翻转ImageView并将控制权传递给第二个活动。

请帮助我。

我已经尝试了很多次,但都没有成功。

提前感谢大家。


2
尝试了很多次,你尝试了什么?在这里发布。 - Paresh Mayani
3个回答

7

你不需要使用任何库,可以尝试以下简单的函数来水平或垂直翻转imageview:

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

您需要传递与要翻转的ImageView相关联的位图和翻转类型。例如,
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));

7

5
您可以使用适用于Android 3.0及以上版本的动画API。
如果需要在蜂窝之前使用,可以使用名为NineOldAndroids的库。
请查看this answer以获取要使用的确切代码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接