【问题标题】:Android center Bitmap in ImageView matrixImageView矩阵中的Android中心位图
【发布时间】:2015-09-01 08:06:47
【问题描述】:

我正在开发一个应用程序,用户可以在其中移动他的图像在屏幕内,然后保存它。 问题是在活动开始时将位图定位在 ImageView 中。
这是 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,而是它的矩阵),并且效果很好。

@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;
    }

问题是Bitmap在Activity开始的位置。它在 Top|Left 而不是 Center 上对齐。像这样:

谁能帮我把它集中在 ImageView 开始?

【问题讨论】:

    标签: android matrix bitmap imageview


    【解决方案1】:

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

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

    编辑:

    如果您需要 scaleType "matrix",请使用下一个解决方案:

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

    然后在代码中改变图片的偏移量:

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

    【讨论】:

    • 我需要它是 android:scaleType="matrix" 因为我正在将它移动到触摸屏上。
    • 谢谢,它正在工作。但我需要它在 API 级别 10 上工作,而 removeOnGlobalLayoutListener 在 API 级别 16 上工作。
    • @filipst 比使用 if 语句,如果 Api Level 小于 16 调用 imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    • @filipst 它适用于我的 Galaxy Ace (Android 2.3.7)。您对图像移动执行相同的操作 - view.setImageMatrix(matrix)。尝试编译没有任何额外实现的简单项目,可能在另一个地方出现问题。
    • 我正在使用三星 Galaxy Note 2,它正在工作,但在三星标签 GT-P5110 上没有任何反应。
    【解决方案2】:

    这对我有用。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 2011-08-31
      • 1970-01-01
      • 2016-11-16
      • 2016-05-18
      相关资源
      最近更新 更多