【问题标题】:Android Flip ImageView VerticallyAndroid 垂直翻转 ImageView
【发布时间】:2015-05-17 15:56:15
【问题描述】:

我正在尝试垂直翻转和ImageView,但它不起作用。

Java:

public static void flipImageVertically(final Bitmap bmp, final ImageView imageView) {
    final Matrix matrix = new Matrix();

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

    imageView.setImageBitmap(Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true));
}

XML:

<LinearLayout                
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/red" />

</LinearLayout>

ImageView 根本没有翻转。

有人知道为什么吗?

【问题讨论】:

    标签: android imageview flip


    【解决方案1】:

    检查this answer。您可以使用 xml 参数非常轻松地执行翻转

    android:scaleY="-1"
    

    请注意,这在预览中不起作用,只有在您运行应用程序时才起作用。
    从 Android Studio 2 开始,这也适用于预览版。

    或者,您可以在代码中调用ImageView 上的setScaleY(-1f)

    【讨论】:

    • 这对我不起作用。我试过了,还是不会翻。
    • 它在预览中不起作用你试过运行它吗?
    【解决方案2】:

    在您的小部件上使用 rotationY 属性,

      android:rotationY="180"
    

    例如:

    <androidx.appcompat.widget.AppCompatImageView
       android:layout_width="@dimen/button_height_small"
       android:layout_height="@dimen/button_height_small"
       android:layout_gravity="center"
       android:padding="@dimen/space_medium"
       android:rotationY="180"/>
    

    【讨论】:

      【解决方案3】:

      我用过

      <ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="YOUR_DRAWABLE_HERE"
          android:rotation="180"/>  // USE THIS TO ROTATE THE IMAGE
      

      这会将图像旋转 180°,这可能看起来像翻转,具体取决于您的图像。

      希望这会有所帮助:)

      【讨论】:

      • 这里的答案很好
      • 旋转不是翻转,澄清了答案。
      【解决方案4】:

      我用过

       imageView.setScaleY(-1);
      

      翻转图像视图(围绕枢轴点的缩放相对于未缩放的宽度,比较documentation

      【讨论】:

      • android:scaleX="-1" 有效
      【解决方案5】:

      我尝试使用旋转动画,但最好的方法是

          image.setRotationX(180); for vertical 
          image.setRotationY(180); for horizontal 
      

      唯一的问题是重置它。

      编辑: 翻转就像镜像视图旋转。我的 ans 是用于旋转而不是翻转旋转。澄清它。

      【讨论】:

      • 旋转不代表翻转
      • ohh haan ...翻转就像镜子视图vala旋转。
      【解决方案6】:

      如果您传递给 flipImageVertically 方法的位图是相反的并且您每次都传递相同的位图,则可能会发生这种情况。 发布更多详细信息可以帮助缩小范围,xml 和代码。

      【讨论】:

      • 我已经添加了 XML 代码。我只传入原始位图。我根本不会提前改变它。
      • 你传递的位图;它是来自资源还是?试试这个私有的静态布尔值翻转; public static void flipImageVertically(Bitmap bmp, final ImageView imageView) { final Matrix matrix = new Matrix(); if (flipped) { matrix.preScale(1.0f, -1.0f); } else { matrix.preScale(-1.0f, 1.0f); } 翻转 = !flipped; imageView.setImageBitmap(Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true)); }
      • 它会交替将图像上下翻转。
      • 是的,它来自资源。我已经尝试过该代码,但它仍然无法正常工作。
      • 如果它来自资源,则翻转的图像将始终相同。例如,如果您在翻转时有一个向上箭头的图像,如果每次都从资源加载,它将是一个向下箭头的图像。不要从资源中加载它,而是使用 Bitmap bit = ((BitmapDrawable)imageView.getDrawable()).getBitmap() 来获取当前在图像视图中的图像并在 flipImage 方法中使用它。您还应该在 xml 中使用 android:src="@drawable/your_image" 因为 imageView 最初不会有图像
      【解决方案7】:

      只是为了通知我已经开发了一个新库FlipView,其中包含并扩展了这个特定的动画(翻转)。我的意思是一个完全可定制的库,您可以在其中将任何类型的视图和布局与您想要的任何类型的动画和形状交换,包括 Gmail 图像翻转。

      对于您的具体情况,我随库提供的示例也具有垂直翻转功能。

      请看一下。

      【讨论】:

        【解决方案8】:

        从资源中获取drawable

        Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.index);
        

        然后

        public static Bitmap flip(Bitmap src, Direction type) {
            Matrix matrix = new Matrix();
        
            if(type == Direction.VERTICAL) {
                matrix.preScale(1.0f, -1.0f);
            }
            else if(type == Direction.HORIZONTAL) {
                matrix.preScale(-1.0f, 1.0f);
            } else {
                return src;
            }
        
            return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
        }
        

        设置ImageView.setImageBitmap()

        【讨论】:

          猜你喜欢
          • 2012-03-25
          • 1970-01-01
          • 1970-01-01
          • 2017-07-06
          • 1970-01-01
          • 2011-07-05
          • 1970-01-01
          • 1970-01-01
          • 2016-10-15
          相关资源
          最近更新 更多