在Android中的View.PostRotate(角度)之后有效地获取自定义图像视图尺寸

时间:2015-03-04 18:12:45

标签: android rotation scaling

我的自定义图片视图出了问题:

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

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

现在给了我新的尺寸,但问题是需要大量的记忆才能在每次旋转后都无法创建新的位图。

现在可以请你就此提出建议吗?再次汇总:旋转后的后缩放(1)确实显示更大的图像,但在测量时不会改变视图的大小。我想得到新的尺寸。

非常感谢提前。阿米尔

1 个答案:

答案 0 :(得分:0)

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

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进行操作,然后将其传递给函数剪切,然后使用新矩阵设置自定义imageview的矩阵ImageMatrix。这段代码是用xamarin编写的,但非常直接在java中映射它。希望它有所帮助!