ImageView Center in position with ScaleType Matrix

2020-07-27 02:57发布

I am using Zoom effect with ImageView so I need to use scaletype=matrix. Now, I want to set Center position to ImageView but I am not able to set it.

Please help me regarding this.

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>

6条回答
聊天终结者
2楼-- · 2020-07-27 03:05

try below code:

android:scaleType="centerInside"

or

android:scaleType="fitCenter"

or

android:scaleType="centerCrop"
查看更多
该账号已被封号
3楼-- · 2020-07-27 03:07

I had the sameproblem. Eventually I Gave up for using Matrix and replaced it with this:

android.view.ViewGroup.LayoutParams params = imageView.getLayoutParams();
params.height = desiredHeight;
params.width = desiredWid;
imageView.setLayoutParams(params);
查看更多
太酷不给撩
4楼-- · 2020-07-27 03:09

Under android:scaleType="matrix" circumstance, the only way I found is to use Matrix to set ImageView position, use the following code:

Matrix matrix = new Matrix();
matrix.postTranslate(screen_width/2, screen_height/2);
imageView.setImageMatrix(matrix);
查看更多
Summer. ? 凉城
5楼-- · 2020-07-27 03:13

Put the following lines of code:

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

Hope this will help you.

查看更多
你好瞎i
6楼-- · 2020-07-27 03:23

try this to your ImageView

  android:layout_centerInParent="true"
查看更多
Deceive 欺骗
7楼-- · 2020-07-27 03:26

try this,

<?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>

this may help you

查看更多
登录 后发表回答