filipst
filipst

Reputation: 1573

Android center Bitmap in ImageView matrix

I am working on an app where user can move his image within the screen, and then save it. The problem is positioning Bitmap in ImageView on start of the activity.
Here is the XML:

<RelativeLayout
        android:id="@+id/image_content_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      <ImageView
           android:id="@+id/top_image"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_centerInParent="true"
           android:scaleType="matrix"
           android:gravity="center|center_vertical"
           android:layout_gravity="center|center_vertical"/>
 </RelativeLayout>

I am moving the ImageView (well, not ImageView, but its matrix) with onTouch, and it works well.

@Override
    public boolean onTouch(View v, MotionEvent event)
    {
        ImageView view = (ImageView) v;
        switch (event.getAction() & MotionEvent.ACTION_MASK)
        {

            case MotionEvent.ACTION_DOWN:
                savedMatrix.set(matrix);
                mode = DRAG;
                start.set((int) event.getX(), (int) event.getY());
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                break;
            case MotionEvent.ACTION_MOVE:

                if (mode == DRAG)
                {
                    matrix.set(savedMatrix);
                    matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
                }
                break;
        }
        view.setImageMatrix(matrix);
        return true;
    }

The problem is the position of Bitmap on the start of Activity. It is aligned on Top|Left instead of Center. Like this:
enter image description here

Can anyone please help me center it on start in ImageView?

Upvotes: 1

Views: 5748

Answers (2)

Lexo
Lexo

Reputation: 437

This worked for me.

<ImageView
    android:id="@+id/top_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="matrix"
    />

Upvotes: 0

Oleksandr
Oleksandr

Reputation: 6356

If you want to align bitmap to center then you ImageView layout should be:

<ImageView
    android:id="@+id/top_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="center"/>

EDIT:

In case if you need scaleType "matrix" then use next solution:

<ImageView
    android:id="@+id/top_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="matrix"
    />

And then in code change offset of image:

    final ImageView imageView = (ImageView) findViewById(R.id.top_image);

    imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= 16) {
                imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

            Drawable drawable = imageView.getDrawable();
            Rect rectDrawable = drawable.getBounds();
            float leftOffset = (imageView.getMeasuredWidth() - rectDrawable.width()) / 2f;
            float topOffset = (imageView.getMeasuredHeight() - rectDrawable.height()) / 2f;

            Matrix matrix = imageView.getImageMatrix();
            matrix.postTranslate(leftOffset, topOffset);
            imageView.setImageMatrix(matrix);
            imageView.invalidate();
        }
    });

Upvotes: 9

Related Questions