Android center Bitmap in ImageView matrix

旧时模样 提交于 2019-12-06 12:28:45

问题


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:

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


回答1:


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



回答2:


This worked for me.

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


来源:https://stackoverflow.com/questions/32326573/android-center-bitmap-in-imageview-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!