开发者

Load image in device independent and screen independent fashion into a layout view using 1.6 SDK

开发者 https://www.devze.com 2022-12-24 23:57 出处:网络
I\'m having trouble getting an asset image to scale up when I load it. The new call to BitmapDrawable(Resources, BitmapDrawable) is not available on 1.6 SDK.

I'm having trouble getting an asset image to scale up when I load it. The new call to BitmapDrawable(Resources, BitmapDrawable) is not available on 1.6 SDK. Is there a workaround to load the BitmapDrawable the old way and then someho开发者_C百科w manipulate it? I have tried calling setTargetDensity() to no avail. My code (which doesn't scale properly) is:

    ImageView iv = (ImageView)view.findViewById(R.id.image);
 iv.setImageDrawable(new BitmapDrawable(view.getContext().getAssets().open(path)));


I found a way that worked, after doing some good old RTFM. The following code works:

  ImageView iv = (ImageView)view.findViewById(R.id.image);
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inScaled = true;
    opts.inDensity = DisplayMetrics.DENSITY_MEDIUM;
    Rect padding = new Rect();
    opts.inTargetDensity = view.getResources().getDisplayMetrics().densityDpi;
    Bitmap bm = BitmapFactory.decodeStream(view.getContext().getAssets().open(path), padding, opts);
    iv.setImageBitmap(bm);

For background see http://d.android.com/guide/practices/screens_support.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号