开发者

Load image in device independent and screen independent fashion into a layout view using 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜