android 从一个方向缩放,Android PhotoView在方向改变后保持缩放

好吧,经过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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值