1

I have image view in xml and I want to try to set image in center:

My current code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:orientation="vertical"
    >

    <ImageView
        android:id="@+id/imageview_fullscreen"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:scaleType="matrix"
        android:src="@drawable/ic_launcher"

        />

</RelativeLayout>

I have added functionality for zoom in/out on image view -

http://stackoverflow.com/questions/15609845/add-limit-to-zoom-in-and-zoom-out-in-android

When I write above code then the image is not able to set in center with scale type matrix. The image is set in left top side from above code.

How can I set this to center with scale type matrix. ?

user3678528
  • 1,741
  • 2
  • 18
  • 24

7 Answers7

1

You can use

android:layout_gravity="center_vertical"

And set

android:layout_width="wrap_content"
android:layout_height="wrap_content"
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • setting the width and height to wrap_content is not ideal cause you would not want the image view to cover only a portion of the screen. also it doesn't work. Can you think of another way around? – Shreemaan Abhishek Jul 29 '20 at 03:53
0
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/ic_launcher" />

</FrameLayout>

try with this. Edited the answer.

DroidAks
  • 327
  • 2
  • 9
  • This is work but i have applied zoon in / out from this `http://stackoverflow.com/questions/15609845/add-limit-to-zoom-in-and-zoom-out-in-android` . so when i do zoom in/out then it is not work perfect. –  Oct 29 '15 at 06:48
  • 1
    Your question was that you want to set the image in center. You should have described the whole issue. – DroidAks Oct 29 '15 at 06:53
0

For the ImageView, add android:layout_centerInParent="true".

darkprince92
  • 406
  • 4
  • 10
0

Try this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center"
  android:orientation="vertical" >

<ImageView
  android:id="@+id/imageview_fullscreen"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_centerHorizontal="true"
  android:layout_centerVertical="true"
  android:src="@drawable/ic_launcher" />

</RelativeLayout>
0

You mentioned that, you are using view zoom. If you are using multi-touch to zoom the ImageView, please resize and center the drawable int he imageview during onMeasure method of the View.

Follow this example to implement both single touch and multi touch on the image view, the image will auto re-size and fit-center. Also, you can change the parent to FrameLayout to achieve scale many views. http://www.mobile-open.com/2015/66671.html

0
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        zoomImageView.setLayoutParams(params);
        RectF drawableRect = new RectF(0, 0, zoomImageView.getDrawable().getIntrinsicWidth(), zoomImageView.getDrawable().getIntrinsicHeight());
        RectF viewRect = new RectF(0, 0, width, (height) - 100);
        matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
        //matrix.setScale(-1,-1);
        view.setScaleType(ImageView.ScaleType.MATRIX);
        zoomImageView.setImageMatrix(matrix);


    <ImageView
        android:id="@+id/zoomImage"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:src="@mipmap/ic_launcher" />

first in your layout place the image in center, when everr the user touchs the imaageview for first time make the imageview width and height matchparent, and set Scaletype matrix, and apply the matrix.

here width and height are device width and heigth,we canculate using following snippet.

DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
    width = displayMetrics.widthPixels;
    height = displayMetrics.heightPixels;
Ramesh Bhupathi
  • 408
  • 7
  • 10
0

You can use on RelativeLayout

android:layout_gravity="centerl"

And set on ImageView

android:layout_width="wrap_content"
android:layout_height="wrap_content"
Farid Haq
  • 3,728
  • 1
  • 21
  • 15