Heim > Fragen und Antworten > Hauptteil
Android图片框架photoview如何记住状态并还原,包括缩放度,缩放后的移动的距离等等,尝试了好多方法都没有作用。
private void generateImages() {
for (int i = 0; i < imagesEntities.size(); i++) {
// PhotoViewAttacher attacher;
final ImagesEntity entity = imagesEntities.get(i);
final float scale = entity.getScale();
final float offsetX = entity.getImageOffsetX() / 100 * screenWidth;
final float offsetY = entity.getImageOffsetY() / 100 * screenWidth;
// final float offsetX = entity.getImageOffsetX();
// final float offsetY = entity.getImageOffsetY();
int w = calculate(entity.getWidth(), screenWidth);
int h = calculate(entity.getHeight(), screenWidth);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(w != 0 ? w
: WIDTH, h != 0 ? h : HEIGHT);
layoutParams.leftMargin = calculateMargin(entity.getCenterX(), entity.getWidth(),
screenWidth);
layoutParams.topMargin = calculateMargin(entity.getCenterY(), entity.getHeight(),
screenWidth);
final PhotoView photoView = new PhotoView(act);
// attacher = new PhotoViewAttacher(photoView);
photoView.setRotation(entity.getDegree());
// photoView.setScaleType(ImageView.ScaleType.CENTER_CROP);
String image = imagesEntities.get(i).getImage();
Bitmap bitmap;
if (image.contains("http") || image.contains("content") || image.contains("storage")) {
BitmapUtils.loadBitmapNoCenterCrop(act, image, photoView);
} else {
bitmap = Base64Util.base64ToBitmap(image);
photoView.setImageBitmap(bitmap);
}
photoView.setTag(R.id.tag_int, i);
photoView.setTag(R.id.tag_int2, entity);
photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
@Override
public void onPhotoTap(View v, float x, float y) {
mPhotoView = photoView;
mPopupWindow.showAsDropDown(v, v.getWidth() / 2, -v.getHeight() / 2);
}
@Override
public void onOutsidePhotoTap() {
showToast("photo_tab_outside");
}
});
// photoView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver
// .OnGlobalLayoutListener() {
// @Override
// public void onGlobalLayout() {
// if (photoView.getDrawable() == null) {
// return;
// }
// photoView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// Matrix matrix = new Matrix();
// matrix.setValues(new float[]{scale, 0.0f, offsetX, 0.0f, scale, offsetY, 0f, 0.0f, 1.0f});
// matrix.postScale(scale, scale);
// matrix.postTranslate(offsetX, offsetY);
// photoView.setDisplayMatrix(matrix);
// }
// });
// photoView.post(new Runnable() {
// @Override
// public void run() {
// photoView.setScale(3, false);
// LogUtils.d("setScale: " + scale);
// }
// });
photoView.setOnScaleChangeListener(new PhotoViewAttacher.OnScaleChangeListener() {
@Override
public void onScaleChange(float scaleFactor, float focusX, float focusY) {
entity.setScale(scaleFactor);
// entity.setImageOffsetX(focusX);
// entity.setImageOffsetY(focusY);
int position = (int) photoView.getTag(R.id.tag_int);
imagesEntities.set(position, entity);
pagesEntity.setImages(imagesEntities);
LogUtils.d(pagesEntity.toString());
}
});
// photoView.setOnMatrixChangeListener(new PhotoViewAttacher.OnMatrixChangedListener() {
// @Override
// public void onMatrixChanged(RectF rect) {
// Matrix matrix = photoView.getDisplayMatrix();
// float[] floats = new float[9];
// matrix.getValues(floats);
//
// float scale = floats[0];
// float offsetX = floats[2] / screenWidth * 100;
// float offsetY = floats[5] / screenWidth * 100;
//
// LogUtils.d(matrix.toString());
// LogUtils.d("scale: " + scale + " x: " + offsetX + " y: " + offsetY);
// entity.setScale(scale);
// entity.setImageOffsetX((int) offsetX);
// entity.setImageOffsetY((int) offsetY);
// int position = (int) photoView.getTag(R.id.tag_int);
// imagesEntities.set(position, entity);
// pagesEntity.setImages(imagesEntities);
// LogUtils.d(pagesEntity.toString());
//
// }
// });
container.addView(photoView, layoutParams);
photoViewContainer.put(i, photoView);
}
}