Android image on tablets and phones

2019-01-20 19:50发布

I have problem,l make application and l want images take same part of screen(10inch tablet and 5inch phone).For example l want image take half width on tablet and on phone...I know for ldpi hdpi xhdpi,but this not work.Can you help me?

1条回答
成全新的幸福
2楼-- · 2019-01-20 19:56

My standard size images are 320 by 200 (for phones) and I store them in the mdpi directory. For tablets I resize them to 480 by 300 like this:

        if (isTablet(getActivity())){  // tablets only
           debugLog( "display tablet image="+imagename);
           int resID = getResources().getIdentifier(imagename,"drawable", getActivity().getPackageName());                       // the corresponding resource id
           if (resID != 0) {
              Bitmap bmp=BitmapFactory.decodeResource(getResources(), resID);
              int width=480;
              int height=300;
              Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
              ImageView imageView = (ImageView) getActivity().findViewById(R.id.tablet_image);  // the imageview to change
              //imageView.setImageResource(resID);
              imageView.setImageBitmap(resizedbitmap);
           }
        }

I do it this way because I have many images and I do not want to store and maintain two copies of each.

查看更多
登录 后发表回答