问题
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>
回答1:
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.
回答2:
try this to your ImageView
android:layout_centerInParent="true"
回答3:
try below code:
android:scaleType="centerInside"
or
android:scaleType="fitCenter"
or
android:scaleType="centerCrop"
回答4:
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);
回答5:
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);
回答6:
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
来源:https://stackoverflow.com/questions/23755102/imageview-center-in-position-with-scaletype-matrix