更好的ImageView旋转而不是Android中的Bitmap?

时间:2015-01-21 20:08:52

标签: android performance bitmapimage

当我要像指南针一样旋转ImageView时,我遇到使用位图的问题。虽然使用Bitmap,它可以完美旋转。

然而,在深入了解内存监视器和logcat之后,我得到了大量的痕迹:

    01-21 21:05:56.556  22147-22147/.../
dalvikvm﹕ GC_FOR_ALLOC freed 11983K, 21% free 56247K/70808K, 
paused 15ms, total 15ms

对我来说,这不是一个好兆头,当我使用Bitmap时会发生这种情况,任何人都可以给我建议避免GC破坏?下面的代码在传感器方法“onSensorChanged()”中调用(下面的代码,mFragment是WeakReference,而mBitmapOrg是位图)。如果这是最佳选择,我宁愿不使用Android 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 );
}

以下是onSensorChanged:

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

1 个答案:

答案 0 :(得分:0)

尝试在rotateImageView()函数中旋转ImageView而不是Bitmap.createBitmap()。

(来自上面的gio和pskink的评论。)

imageView.setScaleType(ImageView.ScaleType.MATRIX);

Matrix mMatrix = new Matrix();
imageView.setImageMatrix(mMatrix);
mMatrix = imageView.getImageMatrix();
mMatrix.postRotate(rotate, width, height);