图片的加载

来源:互联网 发布:好压 mac 编辑:程序博客网 时间:2024/06/05 22:51
public class MainActivity extends Activity {private int screenWidth;private int screenHeight;private ImageView iv_image;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv_image = (ImageView) findViewById(R.id.iv_image);// 获取窗体管理器WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);Display display = wm.getDefaultDisplay();// 1. 获取手机的分辨率screenWidth = display.getWidth();screenHeight = display.getHeight();System.out.println("screenWidth: " + screenWidth + " screenHeight: "+ screenHeight);}public void loadImage(View v) {// 2. 获取图片的分辨率Options opts = new Options();// 标记为只加载图片信息模式opts.inJustDecodeBounds = true;// 解析文件// BitmapFactory.decodeFile("/mnt/sdcard/bg.jpg", opts);BitmapFactory.decodeFile("/mnt/sdcard/318068-106.jpg", opts);int bitmapWidth = opts.outWidth;int bitmapHeight = opts.outHeight;System.out.println("bitmapWidth: " + bitmapWidth + " bitmapHeight: "+ bitmapHeight);// 3. 根据比例进行缩放显示// 计算缩放比例int inSampleSize = 1;// 3000 x 3000, 320x480, 9x6int wSize = bitmapWidth / screenWidth;int hSize = bitmapHeight / screenHeight;// 求出两者最大值inSampleSize = Math.max(wSize, hSize);System.out.println("wSize: " + wSize + " hSize: " + hSize+ " inSampleSize: " + inSampleSize);// 设置缩放比例opts.inSampleSize = inSampleSize;// 标记为加载全部内容opts.inJustDecodeBounds = false;//Bitmap bm = BitmapFactory.decodeFile("/mnt/sdcard/bg.jpg", opts);Bitmap bm = BitmapFactory.decodeFile("/mnt/sdcard/318068-106.jpg", opts);   iv_image.setImageBitmap(bm);}}


0 0
原创粉丝点击