public class MainActivity extends Activity {
private int screenWidth;
private int screenHeight;
private ImageView iv_image;
@Override
protected 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, 9x6
int 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);
}
}
图片的加载
最新推荐文章于 2025-03-07 00:10:11 发布