Android PhotoView在方向改变后保持缩放

时间:2015-07-17 18:02:38

标签: android image-processing imageview orientation-changes

我正在利用Chris Banes的PhotoView课程来放大图像并看到它,但我想这样做,以便当我改变方向时,照片仍会在放大后放大改变。

我理解如何执行此操作的基础知识,当检测到方向更改时,将调用onSaveInstanceState,因此我尝试将实例保存在那里,然后在调用onCreate时将其放回到PhotoView中

public class MainActivity extends ActionBarActivity
{
PhotoView mPhotoView;

@Override
protected void onCreate( Bundle aSavedInstanceState )
{
    super.onCreate( aSavedInstanceState );

    mPhotoView = new PhotoView(this);
    mPhotoView.setMaximumScale( 12 );
    setContentView( mPhotoView );
    mPhotoView.setImageResource( R.drawable.vm_app_icon);

    if (aSavedInstanceState != null)
    {
        RectF theRect = aSavedInstanceState.getParcelable( "Rect" );
        if ( theRect != null)
        {
            Matrix theMatrix = new Matrix();
            theMatrix.setScale( theRect.bottom, theRect.left, theRect.right, theRect.top );
            mPhotoView.setDisplayMatrix( theMatrix );

        }
    }
}

@Override
protected void onSaveInstanceState( final Bundle outState )
{
    super.onSaveInstanceState( outState );
    RectF theRect = mPhotoView.getDisplayRect();
    if (theRect != null)
    {
        outState.putParcelable( "Rect", theRect );
    }
}
}

但这不起作用。我应该在捆绑中存储什么才能应用回PhotoView以保持缩放级别?

1 个答案:

答案 0 :(得分:2)

好吧,经过10个小时的尝试,我已经明白了。

要保存缩放级别,我需要在Bundle,Scale(缩放级别)和DisplayRect(RectF类型)中保存两件事。

缩放级别 - MinScale和MaxScale之间的数字,在我的实例中介于1和16之间

RectF 包含四个值,由于某种原因,这些值是当前视图相对于当前屏幕方向的左上角坐标。即使它保持左上角坐标我不想围绕它旋转,我想围绕中心旋转,所以我需要找到矩形的中心,然后将该值除以“ScreenBase”,这是一个将标准化值并使其能够转换为差异平面的值。这就是我保存它的方式:

@Override
    protected void onSaveInstanceState( final Bundle outState )
    {
        super.onSaveInstanceState( outState );

        Matrix theMatrix = mPhotoView.getDisplayMatrix();
        float[] theFloat = new float[9];
        theMatrix.getValues( theFloat );
        RectF theRect = mPhotoView.getDisplayRect();


        if (theRect != null)
        {
            if( theRect.left > ( mViewWidth / 2 ) || ( theRect.left >= 0 ) )
            {
                theRect.left = 0;
            }
            else
            {
                theRect.left = ( theRect.left - ( mViewWidth / 2 ) ) / mScreenBase;
            }

            if( theRect.top > ( mViewHeight / 2 ) || ( theRect.top >= 0 ) )
            {
                theRect.top = 0;
            }
            else
            {
                theRect.top = ( theRect.top - ( mViewHeight / 2 ) ) / mScreenBase;

            }
            outState.putParcelable( "RectF", theRect );

            outState.putFloat( "ZoomLevel", mPhotoView.getScale() );
        }
    }

然后当我们在另一侧拾起它时,我们必须对数字进行大量操作,以使新屏幕空间的左上角居中于同一位置(如果出现边界问题则操纵它) ),这是我如何做到的:

@Override
    protected void onCreate( final Bundle aSavedInstanceState )
    {
        super.onCreate( aSavedInstanceState );

        mPhotoView = new PhotoView( this );
        mPhotoView.setMaximumScale( 16 );
        setContentView( mPhotoView );
        mPhotoView.setImageResource( R.drawable.vm_app_icon );

        mPhotoView.getViewTreeObserver().addOnPreDrawListener( new ViewTreeObserver.OnPreDrawListener()
        {
            public boolean onPreDraw()
            {
                mPhotoView.getViewTreeObserver().removeOnPreDrawListener( this );
                mViewHeight = mPhotoView.getMeasuredHeight();
                mViewWidth = mPhotoView.getMeasuredWidth();
                Matrix theMatrix = mPhotoView.getDisplayMatrix();
                theMatrix.getValues( mBaseMatrixValues );
                mScreenBase = mBaseMatrixValues[ 0 ];
                int theWidth = mPhotoView.getWidth();
                Log.e(TAG, theWidth + "");

                if( aSavedInstanceState != null )
                {
                    float[] theFloats = new float[ 9 ];
                    float theZoom = aSavedInstanceState.getFloat( "ZoomLevel" );
                    RectF theRect = aSavedInstanceState.getParcelable( "RectF" );
                    theFloats[ 0 ] = theZoom;
                    theFloats[ 4 ] = theZoom;
                    theFloats[ 2 ] = ( theRect.left * mScreenBase ) - ( theZoom * mBaseMatrixValues[ 2 ] ) + ( mViewWidth / 2 ); //Left
                    theFloats[ 5 ] = ( theRect.top * mScreenBase ) - ( theZoom * mBaseMatrixValues[ 5 ] ) + ( mViewHeight / 2 ); //Top
                    theFloats[ 8 ] = (float) 1.0;

                    theFloats = CheckBoundaries( theZoom, theFloats, theRect );

                    theMatrix.setValues( theFloats );
                    mPhotoView.setDisplayMatrix( theMatrix ); //Sets the mSuppMatrix in the PhotoViewAttacher

                    Matrix theImageViewMatrix = mPhotoView.getDisplayMatrix(); //Gets the new mDrawMatrix
                    mPhotoView.setImageMatrix( theImageViewMatrix ); //And applies it to the PhotoView (catches out of boundaries problems)
                }
                return true;
            }
        } );
    }

        private float[] CheckBoundaries(final float aZoom, float[] aFloats, final RectF aRect )
        {
            if( aZoom == 1.0 ) //If the zoom is all the way out
            {
                aFloats[ 2 ] = 0;
                aFloats[ 5 ] = 0;
                return aFloats;
            }

            theMaxLeftValue = ( ( mViewHeight * aZoom ) - mViewWidth + ( aZoom * mBaseMatrixValues[ 2 ] ) );
            theMaxTopValue = ( ( mViewWidth * aZoom ) - mViewHeight + ( aZoom * mBaseMatrixValues[ 5 ] ) );
            if( Math.abs( aFloats[ 2 ] ) > ( theMaxLeftValue ) )
            {
                aFloats[ 2 ] = -Math.abs( theMaxLeftValue ) + 10;
            }
            else if( Math.abs( aFloats[ 2 ] ) < ( aZoom * mBaseMatrixValues[ 2 ] ) )
            {
                aFloats[ 2 ] = -( aZoom * mBaseMatrixValues[ 2 ] );
            }

            if( Math.abs( aFloats[ 5 ] ) > ( theMaxTopValue ) )
            {
                aFloats[ 5 ] = -Math.abs( theMaxTopValue ) + 10;
            }
            else if( Math.abs( aFloats[ 5 ] ) < ( aZoom * mBaseMatrixValues[ 5 ] ) )
            {
                aFloats[ 5 ] = -( aZoom * mBaseMatrixValues[ 5 ] );
            }

            if( aFloats[ 2 ] > 0 )
                aFloats[ 2 ] = -( mViewWidth / 2 );
            else if( aFloats[ 5 ] > 0 )
                aFloats[ 5 ] = -( mViewHeight / 2 );

            return aFloats;
        }