Skip to content

Instantly share code, notes, and snippets.

@s-shin
Created September 7, 2014 08:49
Show Gist options
  • Select an option

  • Save s-shin/e02bd59ebbfa340854e0 to your computer and use it in GitHub Desktop.

Select an option

Save s-shin/e02bd59ebbfa340854e0 to your computer and use it in GitHub Desktop.
Hotentorで使ったViewユーティリティから一部抜粋。
package jp.hateblo.shin.hotentor.util;
import android.annotation.TargetApi;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.TypedValue;
import android.view.View;
import jp.hateblo.shin.hotentor.App;
/**
* Created by shin on 2014/08/30.
*/
public class ViewUtil {
public static final int dip2px(int dip) {
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip,
App.getAppContext().getResources().getDisplayMetrics());
}
public static final int getScreenWidth() {
return App.getAppContext().getResources().getDisplayMetrics().widthPixels;
}
public static final int getScreenHeight() {
return App.getAppContext().getResources().getDisplayMetrics().heightPixels;
}
// http://stackoverflow.com/a/11947755
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressWarnings("deprecation")
public static void setViewBackground(View view, Drawable bg) {
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(bg);
} else {
view.setBackground(bg);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment