간단하게 랜덤한 문자열을 생성해야 할때 사용할 수 있는 문자열 생성 함수 있습니다.
[code]/**
* 랜덤한 문자열을 원하는 길이만큼 반환합니다.
*
* @param length 문자열 길이
* @return 랜덤문자열
*/
private static String getRandomString(int length)
{
StringBuffer buffer = new StringBuffer();
Random random = new Random();
String chars[] =
“a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z”.split(“,”);
for (int i=0 ; i<length ; i++)
{
buffer.append(chars[random.nextInt(chars.length)]);
}
return buffer.toString();
}[/code]
Tag Archives: java
[Java/Android] DIP to Pixel, Pixel to DIP 변환 유틸리티
안드로이드에서는 기본적으로 화면 해상도에 의존적이지 않는 화면 구성을 하여야만 합니다. 하지만 동적으로 UI를 구축한다거나 또는 복잡한 레이아웃에서는 그것이 쉽지가 않죠. 그경우엔 픽셀 단위의 글을 자유자재로 DIP으로 혹은 그 반대로 변환할 수 있어야 합니다. 다음은 간단히 사용할 수 있는 예제입니다.
[code]public class DisplayUtil
{
private static final float DEFAULT_HDIP_DENSITY_SCALE = 1.5f;
/**
* 픽셀단위를 현재 디스플레이 화면에 비례한 크기로 반환합니다.
*
* @param pixel 픽셀
* @return 변환된 값 (DP)
*/
public static int DPFromPixel(int pixel)
{
Context context = BaseApplication.getContext();
float scale = context.getResources().getDisplayMetrics().density;
return (int)(pixel / DEFAULT_HDIP_DENSITY_SCALE * scale);
}
/**
* 현재 디스플레이 화면에 비례한 DP단위를 픽셀 크기로 반환합니다.
*
* @param DP 픽셀
* @return 변환된 값 (pixel)
*/
public static int PixelFromDP(int DP)
{
Context context = BaseApplication.getContext();
float scale = context.getResources().getDisplayMetrics().density;
return (int)(DP / scale * DEFAULT_HDIP_DENSITY_SCALE);
}
}[/code]