`

android开发的一些常用不常用的公共方法

阅读更多
//将 R.drawable里面的图片资源转成换Bitmap型

Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);
Bitmap newb = Bitmap.createBitmap( 300, 300, Config.ARGB_8888 );
Canvas canvasTemp = new Canvas( newb ); 
canvasTemp.drawBitmap(bmp, 50, 50, p);

    InputStream is = getResources().openRawResource(R.drawable.icon); 
    Bitmap mBitmap = BitmapFactory.decodeStream(is);


/**
* 根据日期“2013-12-18”得到星期几
* @param pTime
* @return
[size=large][/size]

*/
public String getWeek(String pTime) {
String Week = "";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
c.setTime(format.parse(pTime));
} catch (ParseException e) {
e.printStackTrace();
}
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
Week += "日";
}
if (c.get(Calendar.DAY_OF_WEEK) == 2) {
Week += "一";
}
if (c.get(Calendar.DAY_OF_WEEK) == 3) {
Week += "二";
}
if (c.get(Calendar.DAY_OF_WEEK) == 4) {
Week += "三";
}
if (c.get(Calendar.DAY_OF_WEEK) == 5) {
Week += "四";
}
if (c.get(Calendar.DAY_OF_WEEK) == 6) {
Week += "五";
}
if (c.get(Calendar.DAY_OF_WEEK) == 7) {
Week += "六";
}
return Week;
}

public DisplayImageOptions picCircleOptions(Context context) {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.displayer(
new RoundedBitmapDisplayer(new DPIUtil().dip2px(
context, 35.0f))).build();
return options;
}
/**
* 判断字符串是否是数字(0.0)
* @param str
* @return
*/
public boolean isNumeric(String str) {
if(str == null || str.equals("")) {
return false;
}
char[] p = str.toCharArray();
for (int i = 0; i < p.length; i++) {
if(!isNum(""+p[i])) {
return false;
}
}
return true;
}

private boolean isNum(String str) {
Pattern pattern = Pattern.compile("[0-9.]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}

/**
* 隐藏软键盘
* @param context
*/
public void hideSoftKeyboard(Context context) {
((InputMethodManager) context.getSystemService("input_method")).
hideSoftInputFromWindow(((Activity) context).getCurrentFocus().
getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

//判断当前ImageView是显示的哪一个本地图片
if(ivStartTest.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.ic_start_test).getConstantState())
{
}

// 代码里实现对控件的属性设置

RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
//1是控件的Id:rlTopView.setId(1);
params.addRule(RelativeLayout.BELOW, 1);

button.setLayoutParams(params); //使layout更新
//添加权重:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
height, 1);
llNews.setWeightSum(3.0f);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics