【问题标题】:Efficiently getting Custom Image View Dimensions after View.PostRotate(angle) in android在Android中的View.PostRotate(角度)之后有效地获取自定义图像视图尺寸
【发布时间】:2015-05-05 21:00:03
【问题描述】:

我的自定义图像视图有问题:

视图设置为矩阵类型。我基本上将地图(室内平面图)加载到此自定义视图中,并希望通过多点触控进行拖动、缩放和旋转。我成功了,但是当我像这样旋转视图时问题发生了:

m_Matrix.PostRotate(mAngle, m_Width/2, m_Height/2);
                        ImageMatrix = m_Matrix;

,然后将其缩放为 1:

m_Matrix.PostScale(1/Scale,1/Scale,m_Width/2,m_Height/2);
ImageMatrix = m_Matrix;

;其中 Scale 是旧比例,m_width 和 m_height 是原始宽度和高度。我发现图像从 0-90 和 180-270 变大,从 90-180 和 270-360 恢复正常。我研究并知道图像在旋转时会变大。美好的!现在我想知道每次旋转后的新尺寸,以便我计算新的比例并应用它,这样图像在旋转后对用户来说看起来很稳定。

现在的问题是,当图像旋转时,view.getwidth 和 getheight 确实返回了原始宽度和高度。我也试过 view.measure() 然后检查测量的宽度和高度,但结果相同。唯一的成功是使用:

Bitmap originalBitmap = ((BitmapDrawable)Drawable).Bitmap;
Bitmap newBitmap = Bitmap.CreateBitmap(originalBitmap, 0, 0,
                            m_Width, m_Height, m_Matrix, true); 

它现在给了我新的尺寸,但问题是它需要大量内存,因此我无法在每次旋转后创建新的位图。

现在您能就此提出建议吗?再次总结:旋转后的 postscale(1) 确实显示了更大的图像,但在测量时不会改变视图的大小。我想获得新的维度。

提前非常感谢。阿米尔

【问题讨论】:

    标签: android rotation scaling


    【解决方案1】:

    在尝试了许多方法来实现我的要求(即在矩阵操作(拖动、缩放和旋转)后限制我的自定义视图)之后,我想与你分享这个,以便它可以帮助别人。

    public void Cutting ()
            {
    
                //this function ensures that a picture is bounded and also center the picture by padding it 
    
                m_Matrix= cutMatrix (m_Matrix);    
                ImageMatrix = m_Matrix;
    
            }
            public Matrix cutMatrix(Matrix matrix)
            {
                RectF rect;
                rect = new RectF (this.Left, this.Top, this.Right, this.Bottom);
                matrix.MapRect (rect);
    
                if (rect.Right < m_Width)
                {
                    matrix.PostTranslate(-rect.Right + m_Width, 0);
                }
                rect = new RectF (this.Left, this.Top, this.Right, this.Bottom);
                matrix.MapRect (rect);
    
                if (rect.Left > 0)
                {
                    matrix.PostTranslate(-rect.Left, 0);
                }
    
                rect = new RectF (this.Left, this.Top, this.Right, this.Bottom);
                matrix.MapRect (rect);
    
                if (rect.Bottom < m_Height)
                {
                    matrix.PostTranslate(0, -rect.Bottom + m_Height);
                }
    
                rect = new RectF (this.Left, this.Top, this.Right, this.Bottom);
                matrix.MapRect (rect);
    
                if (rect.Top > 0)
                {
                    matrix.PostTranslate(0, -rect.Top);
                }
    
                rect = new RectF (this.Left, this.Top, this.Right, this.Bottom);
                matrix.MapRect (rect);
    
                if (rect.Width() < myLayout.Width)
                {
                    matrix.PostTranslate((myLayout.Width - rect.Width()) / 2, 0);
                }
                rect = new RectF (this.Left, this.Top, this.Right, this.Bottom);
                matrix.MapRect (rect);
    
                if (rect.Height() < myLayout.Height)
                {
                    matrix.PostTranslate(0, (myLayout.Height - rect.Height()) / 2);
                }
    
                return matrix;
            }
    

    这段代码在矩阵操作后绑定了您的自定义视图,这样您的视图永远不会超出屏幕。对矩阵 m_Matrix 进行操作,然后将其传递给函数 cut,然后使用新矩阵设置自定义图像视图的矩阵 ImageMatrix。这段代码是用 xamarin 编写的,但在 java 中映射它非常简单。希望对大家有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 2019-12-04
      相关资源
      最近更新 更多