调整一个ImageView后无限的GC_FOR_ALLOC

时间:2015-07-01 04:35:02

标签: android android-layout android-imageview infinite-loop

我的log cat中有无尽的GC_FOR_ALLOC,如下所示:

32ms, total 32ms
07-01 12:23:28.946  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1994K, 32% free 18349K/26896K, paused 15ms, total 15ms
07-01 12:23:29.116  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 3006K, 32% free 18381K/26896K, paused 20ms, total 20ms
07-01 12:23:29.376  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 4671K, 33% free 18284K/26896K, paused 17ms, total 17ms
07-01 12:23:29.496  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1898K, 32% free 18379K/26896K, paused 18ms, total 18ms
07-01 12:23:29.606  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1994K, 32% free 18349K/26896K, paused 17ms, total 17ms
07-01 12:23:29.806  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 3006K, 32% free 18381K/26896K, paused 15ms, total 16ms
07-01 12:23:30.086  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 4671K, 33% free 18283K/26896K, paused 17ms, total 19ms
07-01 12:23:30.206  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 2035K, 33% free 18242K/26896K, paused 14ms, total 15ms
07-01 12:23:30.316  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1994K, 33% free 18212K/26896K, paused 11ms, total 12ms
07-01 12:23:30.486  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 3007K, 33% free 18245K/26896K, paused 17ms, total 17ms
07-01 12:23:30.736  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 4359K, 33% free 18146K/26896K, paused 17ms, total 18ms
07-01 12:23:30.856  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1898K, 33% free 18242K/26896K, paused 16ms, total 17ms
07-01 12:23:30.966  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1994K, 33% free 18212K/26896K, paused 15ms, total 15ms
07-01 12:23:31.136  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 3006K, 33% free 18245K/26896K, paused 20ms, total 21ms
07-01 12:23:31.396  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 4359K, 33% free 18146K/26896K, paused 17ms, total 17ms
07-01 12:23:31.516  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1898K, 33% free 18242K/26896K, paused 17ms, total 17ms
07-01 12:23:31.636  14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1994K, 33% free 18212K/26896K, paused 19ms, total 20ms
07-01 12:23:31.816  14905-14905/com.dodosocial.lowon


在追踪错误之后,我找到了它,因为我已经以编程方式修改了ImageView的高度 这是我使用的代码:

MyActivity.java

private ImageView mImageUserProfile;
private Bitmap mBitmapIcon;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mImageUserProfile = (ImageView)findViewById(R.id.login_icon);
    mImageUserProfile.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mBitmapIcon = CommUtils.getUserProfileBitmap();
            if(mBitmapIcon!=null) {
                setupIcon();
            }
        }
    });
}

private void setupIcon() {
    int bmHeight = mBitmapIcon.getHeight();
    int bmWidth  = mBitmapIcon.getWidth();

    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)mImageUserProfile.getLayoutParams();
    layoutParams.height = layoutParams.width*bmHeight/bmWidth;
    mImageUserProfile.setLayoutParams(layoutParams);

    mImageUserProfile.setImageBitmap(mBitmapIcon);
}

我删除了如下所示的高度变化代码后,GC_FOR_ALLOC就消失了

private void setupIcon() {
  int bmHeight = mBitmapIcon.getHeight();
  int bmWidth  = mBitmapIcon.getWidth();

  /*
  RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)mImageUserProfile.getLayoutParams();
  layoutParams.height = layoutParams.width*bmHeight/bmWidth;
  mImageUserProfile.setLayoutParams(layoutParams);
  */

  mImageUserProfile.setImageBitmap(mBitmapIcon);
}

1 个答案:

答案 0 :(得分:1)

正如@GabeSechan在对你的问题的评论中所说,你导致无限循环,因为你没有删除布局监听器。

通过更改public class MultiConverter : IMultiValueConverter { public object Convert( object[] values, Type targetType, object parameter, CultureInfo culture) { if (values[0] is MyColorType && values[1] is MyColorType && // if ComboBox.SelectedColor == CurrentDataGridRow.ColorColumn (MyColorType)values[0] == (MyColorType)values[1]) return true; return false; } public object[] ConvertBack( object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } 的高度,您的测量无效,从而导致布局通过。系统将重新布局,然后重新触发ImageView,然后重新触发代码以更改高度...这会导致另一个布局......等。

如果您只是记得在更改布局之前删除侦听器,那么您应该没问题。

使用您的代码看起来像这样:

GlobalLayoutListener