【问题标题】:draw bitmap on canvas with original dimension在具有原始尺寸的画布上绘制位图
【发布时间】:2014-06-14 07:31:49
【问题描述】:

我在我的画布上绘制了一个位图,但它没有以原始大小显示

http://upload.dinhosting.fr/A/e/3/Sans_titre.png

这是我的代码:

ImageView image = (ImageView) findViewById(R.id.imageView);
Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Bitmap bmp = Bitmap.createBitmap(320, 382, Bitmap.Config.ARGB_8888);
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.carte);
Canvas canv = new Canvas(bmp);
canv.drawBitmap(bmp1,10,10,null);
Drawable[] layers = new Drawable[2]; 
layers[0] = svg.createPictureDrawable(); 
BitmapDrawable drawable = new BitmapDrawable(getResources(), bmp); 
layers[1] = drawable; 
LayerDrawable layerDrawable = new LayerDrawable(layers);
image.setImageDrawable(layerDrawable);

thnx

【问题讨论】:

    标签: android canvas bitmap


    【解决方案1】:

    Bitmap

    如果你使用这样的东西:

    Bitmap resizedbitmap = Bitmap.createScaledBitmap(bmp1, image.getWidth(), image.getHeight(), true);
    

    您可以通过以下方式获取高度和宽度:
    https://stackoverflow.com/a/9655670/1748764

    或:

    getIntrinsicWidth

    getIntrinsicHeight

    【讨论】:

    • 只是一个问题.. image.getWidth() -> 返回像素大小或dp ??
    • getHeigh 这里说:返回视图的高度,以像素为单位。
    【解决方案2】:

    图像的大小会根据代码运行的设备进行调整。当您打开图像时,Android 会检查设备的分辨率和大小(您使用 BitmapDrawable(getResources(), bmp),因此资源会考虑设备。

    如果您的图片为 100x100,并且您在分辨率为 2.0 的设备上运行代码,则您的图片将为 200x200。

    您需要自行将图像大小调整为所需的宽度和高度。

    【讨论】:

    • thnx ,那我怎样才能自己调整我的图像大小??
    【解决方案3】:

    位图图像; image=BitmapFactory.decodeResource(getResources(),R.mipmap.wallpaper);

    Bitmap originalsize= Bitmap.createScaledBitmap(image, image.getWidth(), image.getHeight(), true);

    【讨论】:

      猜你喜欢
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多