点击图片使图片放大和缩小 的主要源码如下; //想让图片宽是屏幕的宽度
//测量
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//只测量
float height = bitmap.getHeight();
float width = bitmap.getWidth();
//再拿到屏幕的宽
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
float screenWidth = display.getWidth();
//计算如果让照片是屏幕的宽,选要乘以多少?
scale = screenWidth / width;
if (scale == 0) {
scale = 1;
}
//这个时候。只需让图片的宽是屏幕的宽,高乘以比例
int displayHeight = (int) (height * scale);//要显示的高,这样避免失真
//最终让图片按照宽是屏幕 高是等比例缩放的大小
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int) screenWidth, displayHeight);
photoView.setLayoutParams(layoutParams);
PhotoViewAttacher attacher = new PhotoViewAttacher(photoView);
最后这句话最重要的
//想让图片宽是屏幕的宽度
//测量
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//只测量
float height = bitmap.getHeight();
float width = bitmap.getWidth();
//再拿到屏幕的宽
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
float screenWidth = display.getWidth();
//计算如果让照片是屏幕的宽,选要乘以多少?
scale = screenWidth / width;
if (scale == 0) {
scale = 1;
}
//这个时候。只需让图片的宽是屏幕的宽,高乘以比例
int displayHeight = (int) (height * scale);//要显示的高,这样避免失真
//最终让图片按照宽是屏幕 高是等比例缩放的大小
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int) screenWidth, displayHeight);
photoView.setLayoutParams(layoutParams);PhotoViewAttacher attacher = new PhotoViewAttacher(photoView);
最后这句话最重要的
本文详细介绍了如何通过计算屏幕宽度与图片原始宽度的比例来实现图片的等比例缩放,确保图片展示时既不会失真又能充分利用屏幕空间。文章提供了一段关键代码示例,展示了从获取屏幕尺寸到设置图片视图参数的全过程。
1117

被折叠的 条评论
为什么被折叠?



