Gallery中的ImageView填充

时间:2011-05-11 14:16:27

标签: android android-widget

如果我在图库中为ImageView设置填充:

imageView.setPadding(10, 10, 10, 10);

它会在实际设备像素中设置吗?如何在dip中以编程方式设置填充?

2 个答案:

答案 0 :(得分:8)

试试这个..
     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);

答案 1 :(得分:0)

我使用这个单位转换助手类......

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

公共类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);
}

}