一:读取res/raw中的图片
//读取本地res中的图片
public static Drawable readBitmap(Context con, int resid){
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
//获取资源图片
Bitmap bitmap = null;
InputStream input = con.getResources().openRawResource(resid);
try {
bitmap = BitmapFactory.decodeStream(input,null,opt);
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new BitmapDrawable(bitmap);
}
//如果返回的是bitmap对象的时候可以将其回收掉,根据需求,自己设定
public static void recycle(Bitmap bitmap){
if(bitmap!=null && !bitmap.isRecycle()){
bitmap.recycle();
System.gc();//提醒系统及时回收
}
}
static public Drawable getRawDrawable(Context con,int resid){
Bitmap bitmap = null;
try {
InputStream is = con.getResources().openRawResource(resid);
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new BitmapDrawable(bitmap);
}
二:放在assets中的图片(只读)
InputStream is = context.getResources().getAssets().open("icon.png");
Bitmap bitmap = BitmapFactory.decodeStream(is);
static public Drawable getImageFromAssetsFile(Context con, String fileName)
{
Bitmap image = null;
AssetManager am = con.getResources().getAssets();
try {
InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return new BitmapDrawable(image);
}
三:读取sd卡中的图片
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/icon.png");
四:读取网络图片
public static Bitmap loadImageFromUrl(String urlStr){
URL url;
InputStream is = null;
try{
url = new URL(urlStr);
is = m.getContent();
}catch(Exception e){
e.printStaceTrace();
}
return BitmapFactory.decodeStream(i);
}
为view设置背景图,根据view不同的状态设置不同的背景图
实现view的状态的切换效果有三种:
public void setPressedBg(View view, Drawable normal,Drawable focused, Drawable
pressed) {
StateListDrawable bg = new StateListDrawable();
int[][] states = new int[6][];
states[0] = new int[] {android.R.attr.state_pressed,android.R.attr.state_enabled };
states[1] = new int[]
{android.R.attr.state_enabled, android.R.attr.state_focused };
states[2] = new int[] {android.R.attr.state_enabled };
states[3] = new int[]
{android.R.attr.state_focused ,android.R.attr.state_window_focused};
states[4] = new int[]{ android.R.attr.state_window_focused };
bg.addState(states[0], pressed);
bg.addState(states[3], focused);
bg.addState(states[2], normal);
view.setBackgroundDrawable(bg);
}
public void setBackgroundStateListFileName(String press, String normal){
StateListDrawable listDrawable = new StateListDrawable();
int pressed = android.R.attr.state_pressed;
int selected = android.R.attr.state_selected;
listDrawable.addState(new int[]{pressed}, getImageFromAssetsFile(mContext,
press));
listDrawable.addState(new int[]{selected}, getImageFromAssetsFile(mContext,
press));
listDrawable.addState(new int[]{}, getImageFromAssetsFile(mContext, normal));
this.setBackgroundDrawable(listDrawable);
}
public void setBackgroundStateListFileName(String press, String normal) {
StateListDrawable listDrawable = new StateListDrawable();
if (!TextUtils.isEmpty(press)) {
listDrawable.addStat(android.view.View.PRESSED_ENABLED_STATE_SET,
getImageFromAssetsFile(mContext, press));
}
if (!TextUtils.isEmpty(normal)) {
listDrawable.addState(android.view.View.EMPTY_STATE_SET,
getImageFromAssetsFile(mContext, normal))
}
this.setBackgroundDrawable(listDrawable);
}
Android字符串处理类-TextUtils类
处理比较简单的内容不用去思考正则表达式不妨试试这个在android.text.TextUtils的类,主要的功
能如下:
static boolean isEmpty(CharSequence str)
功能:是否为空字符
TextUtils.getCapsMode(CharSequence cs, int off, int reqModes)
功能:确定大小写是否有效在当前位置的文本
static String TextUtils.htmlEncode(String s)
功能:使用HTML编码这个字符串