当前位置:主页 > 移动开发 > Android代码 >

Android 加载asset文件夹下边的图片

时间:2021-04-27 09:07:45 | 栏目:Android代码 | 点击:

将asset中的图片文件加载到ImageView中

// load image  
    try {  
      // get input stream  
      InputStream ims = getAssets().open("avatar.jpg");  
      // load image as Drawable  
      Drawable d = Drawable.createFromStream(ims, null);  
      // set image to ImageView  
      mImage.setImageDrawable(d);  
    }  
    catch(IOException ex) {  
      return;  
    }  

将asset中的图片文件绘制到自定义View中。

Bitmap bitmap; 
    try {  
      InputStream ims = this.getContext().getAssets().open("fl.jpg");  
      // 读入图片并将其强转为 BitmapDrawable类型 
      BitmapDrawable bd = (BitmapDrawable) Drawable.createFromStream(ims, null); 
      bitmap = bd.getBitmap(); 
      ims.close(); 
    }  
    catch(IOException ex) {  
      return;  
    }  
    //canvas.drawBitmap(bitmap, -200, -200, new Paint()); 
    canvas.drawBitmap(bitmap, null, new Rect(-30,-40,30,40), new Paint());//null表示原图尺寸,第二个rect表示显示区域(位图会拉伸填充该区域) 

您可能感兴趣的文章:

相关文章