展会信息港展会大全

android读取图片 android开发教程
来源:互联网   发布日期:2016-01-19 12:31:16   浏览:1633次  

导读:一:读取res raw中的图片 读取本地res中的图片public static Drawable readBitmap(Context con, int resid){BitmapFactory Options opt = new BitmapFactory Options();opt inPreferredConfig = Bi ...

一:读取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编码这个字符串

赞助本站

人工智能实验室

相关热词: 读取图片 android开发

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港