使用到BaseAdapte类的派生类GalleryAdapter,存放Gallery要显示的图片。Gallery每次切换图片,GalleryAdapter类的getView()都会被调用一次。GalleryAdapter引用的图片来源于SD卡pictures文件夹里的数据。getView()每次每次被调用时,根据position读取文件,对页面进行布局。
使用ImageView控件的派生类BasaImageView。BasaImageView类具有调整图片居中显示和对图片进行放大、缩小功能。
GalleryViewActivity浏览图片界面布局使用MyGallery类,通过findViewById找到控件MyGallery,调用setAdapter函数,设置适配器GalleryAdapter,并把图片地址传入适配器中。当通过点击软件功能界面的图册,跳转到GalleryViewActivity活动界面,显示图片。
(1) ImageView缩放
得到图片缩放比例,把图片转换成Matrix对象,对Matrix对象进行缩放和移动。更新UI。
protected void zoomTo(float scale, float centerX, float centerY) {
if (scale > mMaxZoom) {
scale = mMaxZoom;
} else if (scale < mMinZoom) {
scale = mMinZoom;
}
float oldScale = getScale();
float deltaScale = scale / oldScale;
mSuppMatrix.postScale(deltaScale, deltaScale, centerX, centerY);
setImageMatrix(getImageViewMatrix());
center(true, true);
}
protected void zoomTo(final float scale, final float centerX, final float centerY, final float durationMs) {
final float incrementPerMs = (scale - getScale()) / durationMs;
final float oldScale = getScale();
final long startTime = System.currentTimeMillis();
mHandler.post(new Runnable() {
public void run() {
long now = System.currentTimeMillis();
float currentMs = Math.min(durationMs, now - startTime);
float target = oldScale + (incrementPerMs * currentMs);
zoomTo(target, centerX, centerY);
if (currentMs < durationMs) {
mHandler.post(this);
}
}
});
}
protected void zoomTo(float scale) {
float cx = getWidth() / 2F;
float cy = getHeight() / 2F;
zoomTo(scale, cx, cy);
}
protected void zoomToPoint(float scale, float pointX, float pointY) {
float cx = getWidth() / 2F;
float cy = getHeight() / 2F;
panBy(cx - pointX, cy - pointY);
zoomTo(scale, cx, cy);
}
(2) 双击缩放图片 Android平台手机图片浏览软件的开发实现(12):http://www.751com.cn/jisuanji/lunwen_2019.html