调整图像和setCompoundDrawablesWithIntrinsicBounds [英] Resize images and setCompoundDrawablesWithIntrinsicBounds

查看:142
本文介绍了调整图像和setCompoundDrawablesWithIntrinsicBounds的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用包含在他们的图像按钮,使用的 setCompoundDrawablesWithIntrinsicBounds 的设置。我用图片来自应用程序的可绘制的文件夹,但也可以使用从网上下载并存储在SD卡上的图像。我发现我需要高档的SD卡图像,以便他们会为同样大小的的可绘制的图像渲染。我这样做是使用:

My app contains buttons with images on them, set by using setCompoundDrawablesWithIntrinsicBounds. I use images from the app's drawables folder, but also use images downloaded from the web and stored on SD card. I found that I needed to upscale the SD card images so they'd render as the same size as the images in drawables. I did this using:

    Options opts = new BitmapFactory.Options();

    opts.inDensity = 160;
    Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() +
      context.getResources().getString(R.string.savefolder) + iconfile, opts);

    myIcon = new BitmapDrawable(context.getResources(), bm);
    btn.setCompoundDrawablesWithIntrinsicBounds(myIcon, null, null, null );

这已经没有任何问题的工作,直到我更新了我的手机到Android 4.1.1,发现下载的图片现在都出现在比那些来自绘制文件夹更小的尺寸。

This has worked with no problems, until I updated my phone to Android 4.1.1 and noticed that the downloaded images were now appearing at a much smaller size than those from the drawable folder.

我搞砸周围的强度气体的价值影响不大,但随着缩放基础上的 btnheight 的值(只是按钮的图像高度位图有更多的成功坐落于):

I messed around with the inDensity value to little effect, but had more success with scaling the bitmap based on a btnheight value (just the height of the button the image sits on):

    int intoffset=bm.getHeight() - bm.getWidth();                   
    myIcon = new  BitmapDrawable(context.getResources(), 
      Bitmap.createScaledBitmap(bm, btnheight - (((btnheight/100)*10) +
      intoffset) , btnheight - ((btnheight/100)*10), true));

此排序的作品,但是图像仍然比它位于(这不应该是的情况下,根据上述,因为它应该缩放图像高度的按钮高度的90%以上的按钮更大一点)。我这样做是为测试。作为按钮高度按照上的按钮显示的字体大小改变我不能在我的应用程序使用此方法,用户可以在应用程式preferences改变这种字体大小

This sort of works, but the image is still a little bigger than the button it sits on (which shouldn't be the case, based on the above, as it should scale the image height to 90% of the button height.) I did this as a test. I can't use this method in my app as the button height changes in accordance with the font size displayed on the buttons, and the user can change this font size in the app preferences.

顺便说一句,我发现,奇怪的(?),通过缩放位图两次是用原来的高度

As an aside, I found that, oddly(?), by scaling the bitmap to twice it's original height using

   Bitmap.createScaledBitmap(bm, bm.getWidth() * 2
       , bm.getHeight() * 2, true));

它正确呈现,在4.0.3和4.1.1(当然,它在相同大小绘制图标被显示),但表现为你所期望的(呈现比它坐落在按钮更大)的2.1

It rendered correctly, (well, it was displayed at the same size as the drawable icons) in 4.0.3 and 4.1.1, but behaved as you'd expect (rendered bigger than the button it sits on) in 2.1.

如果任何人有任何见解,为什么这发生在4.1.1,和我能做些什么,所以我去codeFILE位图呈现在相同大小作为我绘制位图,而不必code为4.1单独.1,这将是更AP preciated!

If anyone has any insights as to why this happens in 4.1.1, and what I can do so my decodeFile bitmaps render at the same size as my drawable bitmaps, without having to code for 4.1.1 separately, it would be much appreciated!

推荐答案

修改我原来的code到下面是对4.1.1工程,以及我测试了它在previous版本...

Modifying my original code to be as below works on 4.1.1 as well as the previous versions I tested it on...

   Options opts = new BitmapFactory.Options();

   DisplayMetrics dm = new DisplayMetrics();
   context.getWindowManager().getDefaultDisplay().getMetrics(dm);

    int dpiClassification = dm.densityDpi;

    opts.inDensity = dm.DENSITY_MEDIUM;

    opts.inTargetDensity = dpiClassification;
    opts.inScaled =true;

    Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() +
     context.getResources().getString(R.string.savefolder) + iconfile, opts);

    myIcon = new BitmapDrawable(context.getResources(), bm);
    btn.setCompoundDrawablesWithIntrinsicBounds(myIcon, null, null, null ); 

这篇关于调整图像和setCompoundDrawablesWithIntrinsicBounds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆