更好的图像视图旋转,而不是在Android中使用位图?
当我要像指南针一样旋转图像视图时,使用位图有问题。虽然使用位图,但它可以完美地旋转 然而,在深入查看内存监视器和logcat之后,我得到了大量的跟踪信息,其中说:更好的图像视图旋转,而不是在Android中使用位图?,android,performance,bitmapimage,Android,Performance,Bitmapimage,当我要像指南针一样旋转图像视图时,使用位图有问题。虽然使用位图,但它可以完美地旋转 然而,在深入查看内存监视器和logcat之后,我得到了大量的跟踪信息,其中说: 01-21 21:05:56.556 22147-22147/.../ dalvikvm﹕ GC_FOR_ALLOC freed 11983K, 21% free 56247K/70808K, paused 15ms, total 15ms 对我来说,这不是一个好兆头,当我使用位图时会发生这种情况,有谁能给我一些避免GC
01-21 21:05:56.556 22147-22147/.../
dalvikvm﹕ GC_FOR_ALLOC freed 11983K, 21% free 56247K/70808K,
paused 15ms, total 15ms
对我来说,这不是一个好兆头,当我使用位图时会发生这种情况,有谁能给我一些避免GC破坏的建议吗?下面的代码是在传感器方法“onSensorChanged()”中调用的(下面的代码,MFFragment是WeakReference,而mBitmapOrg是位图)。如果安卓NDK是最佳选择,我宁愿不使用它
private void rotateImageView(ImageView imageView, int drawable, float rotate) {
DisplayMetrics mDisplayMetrics = new DisplayMetrics();
mFragment.get().getActivity().getWindowManager().getDefaultDisplay().
getMetrics(mDisplayMetrics);
int width = mBitmapOrg.getWidth(), height = mBitmapOrg.getHeight();
Matrix mMatrix = new Matrix();
rotate = rotate % 360;
mMatrix.postRotate( rotate, width, height );
Bitmap rotatedBitmap = Bitmap.createBitmap(mBitmapOrg, 0, 0, width, height, mMatrix, true);
BitmapDrawable bitmapDrawable = new BitmapDrawable(mFragment.get().getResources(), rotatedBitmap);
imageView.setImageDrawable(bitmapDrawable);
imageView.setScaleType( ImageView.ScaleType.CENTER );
}
以下是传感器更改:
@Override
public void onSensorChanged(SensorEvent event)
{
if (mFromLocation == null || mToLocation == null ||
mArrowCompass == null || event == null) return;
switch (event.sensor.getType())
{
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, mValuesAccelerometer, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, mValuesMagneticField, 0, 3);
break;
}
boolean success = SensorManager.getRotationMatrix(mMatrixR, mMatrixI,
mValuesAccelerometer,
mValuesMagneticField);
// calculate a new smoothed azimuth value and store to mAzimuth
if (success)
{
SensorManager.getOrientation(mMatrixR, mMatrixValues);
mAzimuth = (float) Math.toDegrees(mMatrixValues[0]);
mAzimuth -= getGeomagneticField(mFromLocation).getDeclination(); // converts magnetic north into true north
mBearTo = mFromLocation.bearingTo(mToLocation);
// If the bearTo is smaller than 0, add 360 to get the rotation clockwise.
if (mBearTo < 0) {
mBearTo = mBearTo + 360;
}
//This is where we choose to point it
mDirectionToDest = (mBearTo - mAzimuth) % 360;
// If the direction is smaller than 0, add 360 to get the rotation clockwise.
if (mDirectionToDest < 0) {
mDirectionToDest = mDirectionToDest + 360;
}
final float mDirection = mDirectionToDest;
rotateImageView(mArrowCompass.get(), R.drawable.compass_arrow, mDirection);
}
}
@覆盖
传感器更改时的公共无效(传感器事件)
{
如果(mFromLocation==null | | mToLocation==null||
mArrowCompass==null | | event==null)返回;
开关(event.sensor.getType())
{
外壳传感器.U型加速计:
系统阵列复制(event.values,0,mvaluesacceleometer,0,3);
打破
外壳传感器。类型\u磁场:
系统阵列拷贝(event.values,0,mvalues磁场,0,3);
打破
}
布尔成功=SensorManager.getRotationMatrix(mMatrixR,mMatrixI,
M值加速度计,
M值(磁场);
//计算新的平滑方位角值并存储到mAzimuth
如果(成功)
{
SensorManager.getOrientation(mMatrixR、mMatrixValues);
mAzimuth=(float)Math.toDegrees(mmatrixvalue[0]);
mAzimuth-=getGeomagneticField(mFromLocation).getDeclination();//将磁北转换为正北
mBearTo=mFromLocation.bearingTo(mToLocation);
//如果bearTo小于0,则添加360以获得顺时针旋转。
如果(姆贝托<0){
姆贝托=姆贝托+360;
}
//这就是我们选择指出它的地方
mDirectionToDest=(姆贝亚托-马齐茅斯)%360;
//如果方向小于0,则添加360以获得顺时针旋转。
如果(mDirectionToDest<0){
mDirectionToDest=mDirectionToDest+360;
}
最终浮点mDirection=mDirectionToDest;
rotateImageView(mArrowCompass.get(),R.drawable.compass_箭头,mDirection);
}
}
尝试在rotateImageView()函数中旋转ImageView而不是Bitmap.createBitmap()
(以上是新闻局和普金克的评论。)
imageView.setScaleType(imageView.ScaleType.MATRIX);
矩阵mMatrix=新矩阵();
setImageMatrix(mMatrix);
mMatrix=imageView.getImageMatrix();
mMatrix.postRotate(旋转、宽度、高度);
尝试旋转视图而不是位图旋转ImageView的内部矩阵,通过调用ImageView.getImageMatrix()并使用scaleType=“Matrix”来获取它。尝试了,没有成功,你能给我举个例子吗?