View関連のUtilメモ

#View関連もよく使うもの
個人的メモ

- Viewのメモリ解放促進(他に使うWidgetがあれば足して使う)
- setBackgroundとsetBackgroundDrawableのSDKバージョンによる切り替え(SDK v11以前との切り分け）
- dpとpxの変換
- generateViewIdのAndroid4.2以前の下位互換実装

```ViewUtil.java
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageButton;
import android.widget.SeekBar;


public class ViewUtil {
    public static final void cleanupView(View view) {
        if (view instanceof ImageButton) {
            ImageButton imageButton = (ImageButton) view;
            imageButton.setImageDrawable(null);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                imageButton.setBackground(null);
            } else {
                imageButton.setBackgroundDrawable(null);
            }
        } else if (view instanceof ImageView) {
            ImageView imageView = (ImageView) view;
            imageView.setImageDrawable(null);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                imageView.setBackground(null);
            } else {
                imageView.setBackgroundDrawable(null);
            }
        } else if (view instanceof SeekBar) {
            SeekBar seekBar = (SeekBar) view;
            seekBar.setProgressDrawable(null);
            seekBar.setThumb(null);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                seekBar.setBackground(null);
            } else {
                seekBar.setBackgroundDrawable(null);
            }
        }

        if (view instanceof ViewGroup) {
            ViewGroup viewGroupg = (ViewGroup) view;
            int size = viewGroupg.getChildCount();
            for (int i = 0; i < size; i++) {
                cleanupView(viewGroupg.getChildAt(i));
            }
        }
    }

    public static final void setBackgroundDrawable(View view, Drawable drawable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(drawable);
        } else {
            view.setBackgroundDrawable(drawable);
        }
    }

    public static final int dpToPx(Context context, int dip) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }

    public static final int pxToDp(Context context, int px) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale);
    }

    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);

    public static int generateViewId() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            for (;;) {
                final int result = sNextGeneratedId.get();
                int newValue = result + 1;
                if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
                if (sNextGeneratedId.compareAndSet(result, newValue)) {
                    return result;
                }
            }
        } else {

           return View.generateViewId();

        }
    }
}
```

##参考
viewの解放
http://dorodoro.info/tip/%E3%83%A1%E3%83%A2%E3%83%AA%E3%83%AA%E3%83%BC%E3%82%AF%E3%81%AE%E5%9B%9E%E9%81%BF%E3%82%92%E3%81%99%E3%82%8B/