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>
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.
try this to your ImageView
android:layout_centerInParent="true"
try below code:
android:scaleType="centerInside"
or
android:scaleType="fitCenter"
or
android:scaleType="centerCrop"
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);
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);
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