【问题标题】:ImageView Center in position with ScaleType MatrixImageView 中心在 ScaleType 矩阵的位置
【发布时间】:2017-01-18 21:32:45
【问题描述】:

我在 ImageView 中使用缩放效果,所以我需要使用 scaletype=matrix。现在,我想将中心位置设置为 ImageView,但我无法设置它。

请帮助我解决这个问题。

layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imgImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="@string/imgdesc"
        android:scaleType="matrix"
        android:src="@drawable/ic_answer" />

</RelativeLayout>

【问题讨论】:

  • 您有什么解决方案吗?请与我们分享

标签: android xml android-layout scale


【解决方案1】:

放入以下代码行:

RectF drawableRect = new RectF(0, 0, image_width, image_height);
RectF viewRect = new RectF(0, 0, screen_width, screen_height);
matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);

希望这会对你有所帮助。

【讨论】:

    【解决方案2】:

    试试这个ImageView

      android:layout_centerInParent="true"
    

    【讨论】:

    • @JeetenParmar 更改 android:scaleType="centerCrop" 并尝试
    • 我正在缩放图像,所以我不能使用任何期望矩阵。
    【解决方案3】:

    试试下面的代码:

    android:scaleType="centerInside"
    

    android:scaleType="fitCenter"
    

    android:scaleType="centerCrop"
    

    【讨论】:

    • 我正在缩放图像,所以所有这些都不能通过用矩阵替换它来工作。
    【解决方案4】:

    android:scaleType="matrix"的情况下,我发现唯一的办法就是使用Matrix来设置ImageView的位置,使用如下代码:

    Matrix matrix = new Matrix();
    matrix.postTranslate(screen_width/2, screen_height/2);
    imageView.setImageMatrix(matrix);
    

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题。最终我放弃了使用 Matrix 并将其替换为:

      android.view.ViewGroup.LayoutParams params = imageView.getLayoutParams();
      params.height = desiredHeight;
      params.width = desiredWid;
      imageView.setLayoutParams(params);
      

      【讨论】:

        【解决方案6】:

        试试这个,

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        
            <ImageView
                android:id="@+id/imgImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:scaleType="matrix"
                android:src="@drawable/ic_launcher" />
        
        </RelativeLayout>
        

        这可能对你有帮助

        【讨论】:

        • 这肯定会起作用,但随后ImageView 设置了它的边界,因此它不允许用户将图像缩放到其边界之外。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-13
        • 1970-01-01
        • 2011-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-29
        相关资源
        最近更新 更多