开发者

ImageView padding inside Gallery

开发者 https://www.devze.com 2023-03-05 16:04 出处:网络
If I set padding for ImageView inside gallery like this: imageView.setPadding(开发者_C百科10, 10, 10, 10);

If I set padding for ImageView inside gallery like this:

imageView.setPadding(开发者_C百科10, 10, 10, 10);

it would be set in actual device pixels? How do I set padding programmatically in dip?


Try this..
private int mPaddingInPixels;
final float scale = mContext.getResources().getDisplayMetrics().density;
mPaddingInPixels = (int) (PADDING_IN_DP * scale + 0.5f);
mPaddingInPixels = mPaddingInPixels + 5;
imageView.setPadding(mPaddingInPixels, mPaddingInPixels, mPaddingInPixels, mPaddingInPixels);


I use this unit conversion helper class...

import android.content.Context; import android.util.DisplayMetrics;

public class UnitHelper {

/**
 * Convert dp value to px value
 * @param context The context of operation
 * @param dpValue The dp value to be converted to px
 * @return The px value
 */
public static int dpToPx(Context context, int dpValue) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    return (int)((dpValue * displayMetrics.density) + 0.5);
}

/**
 * Convert px value to dp value
 * @param context The context of operation
 * @param pxValue The px value to be converted to dp
 * @return The dp value
 */
public static int pxToDp(Context context, int pxValue) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    return (int) ((pxValue/displayMetrics.density)+0.5);
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消