加载asset文件夹下边的图片

这篇博客介绍了如何将Android应用的Asset文件夹中的图片加载到ImageView和自定义View中进行显示,特别提到了在onDraw()方法中绘制位图的源码实现。

将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表示显示区域(位图会拉伸填充该区域)

在onDraw()中绘制位图需要用到的方法(源码):


    /**
     * Draw the specified bitmap, scaling/translating automatically to fill
     * the destination rectangle. If the source rectangle is not null, it
     * specifies the subset of the bitmap to draw.
     *
     * <p>Note: if the paint contains a maskfilter that generates a mask which
     * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter),
     * then the bitmap will be drawn as if it were in a Shader with CLAMP mode.
     * Thus the color outside of the original width/height will be the edge
     * color replicated.
     *
     * <p>This function <em>ignores the density associated with the bitmap</em>.
     * This is because the source and destination rectangle coordinate
     * spaces are in their respective densities, so must already have the
     * appropriate scaling factor applied.
     *
     * @param bitmap The bitmap to be drawn
     * @param src    May be null. The subset of the bitmap to be drawn
     * @param dst    The rectangle that the bitmap will be scaled/translated
     *               to fit into
     * @param paint  May be null. The paint used to draw the bitmap
     */
    public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull RectF dst,
            @Nullable Paint paint) {
      if (dst == null) {
          throw new NullPointerException();
      }
      throwIfCannotDraw(bitmap);
      final long nativePaint = paint == null ? 0 : paint.getNativeInstance();

      float left, top, right, bottom;
      if (src == null) {
          left = top = 0;
          right = bitmap.getWidth();
          bottom = bitmap.getHeight();
      } else {
          left = src.left;
          right = src.right;
          top = src.top;
          bottom = src.bottom;
      }

      native_drawBitmap(mNativeCanvasWrapper, bitmap, left, top, right, bottom,
              dst.left, dst.top, dst.right, dst.bottom, nativePaint, mScreenDensity,
              bitmap.mDensity);
  }


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值