Chinaunix首页 | 论坛 | 博客
  • 博客访问: 376332
  • 博文数量: 214
  • 博客积分: 770
  • 博客等级: 军士长
  • 技术积分: 1969
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-08 01:22
文章分类

全部博文(214)

文章存档

2013年(110)

2012年(104)

我的朋友

分类: Android平台

2013-07-26 11:14:35

 一、setBackgroundXXX的用处,设置这个View背景。

     setBackgroundDrawable 的参数为Drawable对象,

     setBackgroundColor      的参数为Color对象,比如说Color.Red为红色,或Color.rgb(255,0,0) 来制定一个红色

     setBackgroundResource 的参数为资源ID,   比如说R.drawable.icon

 二、对于ImageView类有类似 setImageXXX

  道理同上,setImageBitmap的参数为Bitmap对象,同时ImageView还支持矩阵对象,比如setImageMatrix的参数为Matrix对象。



获取drawable对象
方式一:
已将图片保存到drawable目录下,通过图片id获得Drawable或者Bitmap,此方式最常用。(若只知道图片的名称,还可以通过图片的名称获得图片的id)

(1)通过图片id获得Drawable
    Drawable drawable=getResource().getDrawable(R.drawable.xxx);
(2)通过图片id获得Bitmap
    Resource res=gerResource();
    Bitmap bitmap=BitmapFactory.decodeResource(res, id);
(3)通过图片的名称获得图片的id(两种方法)
    int id =res.getIdentifier(name, defType, defPackage); //name:图片的名,defType:资源类型(drawable,string。。。),defPackage:工程的包名
    Drawable drawable=getResource().getDrawable(id);
方式二:
已将图片保存到assest目录下,知道图片的名称,通过inputstream获得图片Drawabl或者 Bitmap
    AssetManager asm=getAssetMg();
    InputStream is=asm.open(name);//name:图片的名称
    (1)获得Drawable
    Drawable da = Drawable.createFromStream(is, null);
    (2)获得Bitmap
    Bitmap bitmap=BitmapFactory.decodeStream(is);
方式三: 图片保存在sdcard,通过图片的路径h
/图片路径
    String imgFilePath = Environment.getExternalStorageDirectory().toString()+ “/DCIM/device.png”;
(1)文件输入流
    fis = new FileInputStream(new File(imgFilePath));//文件输入流
    Bitmap bmp = BitmapFactory.decodeStream(fis);
(2) ImageView iv = (ImageView) findViewById(R.id.image);   
    Bitmap bit = BitmapFactory.decodeFile("/sdcard/android.bmp");      
    iv.setImageBitmap(bit);
    iv.setImageDrawable(Drawable.createFromPath(new                                                                 File(Environment.getExternalStorageDirectory(),"camera.jpg").getAbsolutePath()));

阅读(1125) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~