在ImageView的矩阵Android的位图中心 [英] Android center Bitmap in ImageView matrix

查看:192
本文介绍了在ImageView的矩阵Android的位图中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,其中用户可以在屏幕内移动他的形象,然后将其保存。
问题是在ImageView的上活动的开始定位的位图。结果
下面是XML:

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>

我用的 onTouch 的移动ImageView的(当然,不是ImageView的,但它的矩阵),它工作得很好。

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:

任何人都可以请帮我中心,它在启动的ImageView的?

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

推荐答案

如果要对齐位图到中心,那么你ImageView的布局应该是:

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"/>

编辑:

在情况下,如果你需要scaleType黑客帝国,然后使用下一个解决方案:

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"
    />

然后在code修改图像偏移:

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

这篇关于在ImageView的矩阵Android的位图中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆