PhotoViewAttacher clears imageView bounds and zoom level - android

I'm using PhotoView library for the implementation of a zooming Android ImageView. The zooming works fine.
I'm setting an image with low-quality in ImageView and then start to download the new image with hight-quality which will replace the low-quality image.
If the user zooms in the low-quality image - the high-quality image replaces the existing image and the zoom level clears :(
How can I save the zoom level after loading the high-quality image?
I've tried to get the image Matrix from PhotoViewAttacher in low-quality and set it to the high-quality image, but it doesn't work - the image zoom level and bounds aren't the same as they were before. The high-quality image replaced the low-quality image in ImageView.
Matrix matrix;
imageView.setImageBitmap(imageBitmap);
...
PhotoViewAttacher mAttacher = new PhotoViewAttacher(imageView);
// save the matrix before any modifications
matrix = mAttacher.getDisplayMatrix();
mAttacher.setOnMatrixChangeListener(new OnMatrixChangedListener() {
#Override
public void onMatrixChanged(RectF rect) {
// update the matrix
matrix = mAttacher.getDisplayMatrix();
}
});
imageProvider.load(getActivity(), imageView, imageUrl, progressBarView, imageConfig, new ImageCallback() {
#Override
public void onSuccess(ImageView imageView) {
// update image in ImageView
// probably the problem is in this .update() call
// but I don't get what's the exact problem
mAttacher.update();
// restore high-quality image matrix
mAttacher.setDisplayMatrix(matrix);
}
#Override
public void onError(ImageView imageView) {
}
});
Edit: Images have different dimensions.

After looking at the code of PhotoViewAttacher.java - it seems that calling update() does two things:
Ensure the scale type for ImageView is set to ScaleType.MATRIX.
Invalidate the current matrix and recalculate it according to the dimensions of the new Drawable.
So, assuming your first and second image both have exactly the same dimensions, you can just skip the update() call, which should retain the current zoom and pan values.

Related

Facebook fresco library: ZoomableDraweeView check image is zoomed or in original state

I am using ZoomableDraweeView from arlindiDev/FrescoDoubleTapZoom in my app it is working perfectly for pinch and double tap zoom effect but problem is that I want a zoom checking listener in my Mainactivity.class that check whether image is zoom state or in original state.
ChechZoom.java
interface public interface CheckZoom {
void isZoomed(Boolean zoom);
}
implemented in mainactivity.java to observe zoom state
and called in AnimatedZoomableController.java 's method
public void zoom(PointF viewPoint) {
PointF imagePoint = mapViewToImage(viewPoint);
if(getScaleFactor() < getMaxScaleFactor()) {
**checkZoom.isZoomed(true);**
zoomToPoint(getMaxScaleFactor(), imagePoint, viewPoint, LIMIT_ALL, 400, null);
} else {
**checkZoom.isZoomed(false);**
zoomToPoint(getMinScaleFactor(), imagePoint, viewPoint, LIMIT_ALL, 400, null);
}
}
it work fine for double tap zoom effect notify image is zoomed or not in main activity.
but problem is where should i put this line of code checkZoom.isZoomed(true or false); to detect pinch zoom effect.

Android - ImageView flickering by changing the Image (Bitmap) on it

I have got a Image (Bitmap) on a ImageView, without flickering. When I change something with setPixel(x, y, COLOR_VALUE), so some Pixels are changed on the ImageView, it begins to flicker, where I changed the Pixels.
public class Drawer extends ImageView {
private Bitmap someBitmap;
public void doSomeDrawing() {
for (int i = 0; i < 100; i = i + 2) {
someBitmap.setPixel(x, y, COLOR_VALUE);
}
setOnDraw();
}
public void setOnDraw() {
this.setImageBitmap(someBitmap);
}
Try getting a copy of your bitmap and draw on it. Then recycle your old bitmap.
The problem here might also be that setting the pixel takes time and if you do this on the UI Thread, it will slow down your app and may cause flickering too. How much time does doSomething take ?

How to load multiple image of specified size?

I want to load multiple image(list view or gridview) of specified size (i need to change height and width of image at run time) .Can anyone suggest me for any library which support this functionality ?
imageloader.loadimage(imageview,"imageurl",height,width);
Check out UniversalImageLoader. I think this is the best library for loading images. In its loading listener you can resize the loaded image before display it.
Try by this one : and check this for refrence https://github.com/nostra13/Android-Universal-Image-Loader
ImageSize targetSize = new ImageSize(120, 80); // result Bitmap will be fit to this size
imageLoader.loadImage(imageUri, targetSize, displayOptions, new SimpleImageLoadingListener() {
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
}
});

Load part of bitmap in scrollable imageview and load the rest when scrolling?

I'm trying to load in part of a tall image into a scrollable imageview (imageview inside a scrollview).
It works with smaller images but if the images is bigger than 2048x2048 the app will crash due to open gl out of memory.
I can load part of the image with this:
private Bitmap decodeBitmapRegion(InputStream in, Rect region) {
BitmapRegionDecoder decoder = null;
try {
decoder = BitmapRegionDecoder.newInstance(in, false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
region.right = decoder.getWidth();
Bitmap reg = decoder.decodeRegion(region, null);
return reg;
}
Where the rectangle can be something like (0,0,DisplayWidth,800).
That works in most cases since i'm using the imageview to let the user crop a part of the image and save it as a coveriamge.The imageview has the same width as the display and the height of 400 dp.
So the image the user saves will be the same width as the orginal but only 400 dp's tall.
Is there anyway to make it possible for the user to scroll all the way to the bottom of the orginal image?
How does webview load images that are bigger than 2048x2048?
Here's one thought.
Imagine your image broken into rows of bands. Use a ListView to display the bands. A ListView uses an adapter. In adapter.getView(), you use the decoder to load the appropriate band.
The ListView already does "recycling". So, it more-or-less only holds enough rows to fill the screen.
This technique should work well if your images aren't too wide. If they are, and it's not tall enough to occupy more than one screen height, you'll still have the same memory problems. A more complicated solution would be to use some sort of grid view, but then you'll have to do your own view recycling. Not too difficult, but harder than using ListView.
I managed to get the functionality I wanted. But i don't think this is the best solution?
I loaded the image into a webview and then i took a screenshot of the visible part of the webview. After that i can save that bitmap and do whatever i want to it.
But there must be a better way to do this than to use a webview to show the original image?
Anyway here is what I did:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
imgView = (ImageView) findViewById(R.id.imageview);
webView = (WebView) findViewById(R.id.webview);
webView.loadUrl("file:///android_asset/test.jpg");
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imgView.setImageBitmap(getBitmapForVisibleRegion(webView));
}
});
}
public static Bitmap getBitmapForVisibleRegion(WebView webview) {
Bitmap returnedBitmap = null;
webview.setDrawingCacheEnabled(true);
returnedBitmap = Bitmap.createBitmap(webview.getDrawingCache());
webview.setDrawingCacheEnabled(false);
return returnedBitmap;
}

Android: how to change a low quality image to the hi quality one when the animation stops in gallery?

I want to do an image gallery like in iphone. I want to show low quality (pre-resized) images and when the image is active I want to process the big image and show the result in the gallery.
I have two questions. How to attach a listener on the animation stop in gallery? And how to access an image after this action?
You can set an AnimationListener on your animation, and override the onAnimationEnd method.
From http://www.roosmaa.net/animation-ended-callback/ :
ImageView viewN = ..;
Animation animN = ...;
Drawable myNewDrawable = ...;
animN.setAnimationListener(new AnimationListener() {
// ...
void onAnimationEnd(Animation anim)
{
//Do your work here.
viewN.setDrawable(myNewDrawable);
return;
}
// ...
});
viewN.startAnimation(animN);

Categories

Resources