Android ImageView scale and zoom replace image - java

I'm trying to build an simple image-zoom-replace functionality based on the Image-Zoom-Library "PhotoView":
https://github.com/chrisbanes/PhotoView
I just want to load a small image into a imageView and zoom/scale it. This code works perfectly:
ImageView galleryPictureNew = (ImageView)findViewById(R.id.galleryImage);
PhotoViewAttacher mAttacher = new PhotoViewAttacher(view);
mAttacher.update();
The problem is, i want to replace the small image with a large (hd-image) if i zoom in.
There is function called "setOnScaleChangeListener" like this:
mAttacher.setOnScaleChangeListener(new PhotoViewAttacher.OnScaleChangeListener() {
#Override
public void onScaleChange(float scaleFactor, float focusX, float focusY) {
//REPLACE IMAGE
}
});
This code above works, too BUT i want to "jump" to the last "position" and "zooming-level"?!
Does anyone has a solution for it?
Or maybe someone has an alternative library for this problem?
Do i have to save the last "position-state"?

Related

Keep Size Image with Ground Overlay in Google Map

I'm new in Android and I'm building an app. My problem is that when I want to put my own image in google map app using Ground Overlay.
But When I put my images, I can't keep the size, it very small and when I zoom it is the real size. What I would like to have it's when I zoom it keep the same size.
For example in Booking App, The "H" image is always at the same size (Airbnb had the same also).
Here is my code:
#Override
public void onMapReady(GoogleMap map) {
// Register a listener to respond to clicks on GroundOverlays.
map.setOnGroundOverlayClickListener(this);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 11));
mImages.clear();
mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.newark_nj_1922));
mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.newark_prudential_sunny));
mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.logo_franprix));
//Resources res = getResources();
//Drawable drawable = res.getDrawable(R.drawable.ic_logo_franprix);
// Add a small, rotated overlay that is clickable by default
// (set by the initial state of the checkbox.)
mGroundOverlayRotated = map.addGroundOverlay(new GroundOverlayOptions().image(mImages.get(2)).anchor(0, 1)
.position(new LatLng(48.886517, 2.37105), 300f,300f));
I hope that you could help me, thank you very much.

LayoutTransition animation on ExpandableListview does not work properly

I want to apply a simple rotationX animation when a group expands/collapses on an ExpandableListView. The code below works in first two expands but after two/three/five times does not play the defined animation. The strange thing is that the first time that I expand /collapse it works perfect!
ExpandableListview exp = new ExpandableListview(context);
// set adapter code
LayoutTransition transition = new LayoutTransition();
Animator appearAnim = ObjectAnimator.ofFloat(null, "rotationX", 90f,0f).setDuration(transition.getDuration(LayoutTransition.APPEARING));
transition.setAnimator(LayoutTransition.APPEARING, appearAnim); // I also tried first argument equals DISSAPEARING, CHANGING, etc
exp.setLayoutTransition(transition);
Any idea?? Is this approach totally wrong???
I have to notice at this point that this is not a duplicate! I am looking for a solution that will be approached with LayoutTransition class / methods.
Here is some awesome sample which may help you to do animation on expands/collapses on an ExpandableListView, In the android-flip library that uses OpenGL for rendering animation ,if the minimum supported Android version for the app was 4.0 we can use standard Android SDK methods instead of OpenGL: View.setRotationX(), View.setScaleX(), etc. When hardware acceleration is enabled (and it is enabled by default if your target API level is >=14), these methods work quite efficiently using the device GPU.
You can use this FoldableLayout to implement folding animation for expands/collapses on an ExpandableListView.
Layout implementation in FoldableLayout:
The first element to design was a layout that can fold in half. Our
approach was rather bold: the main layout (FoldableItemLayout) simply
contains a specialized layout (BaseLayout). During the animation, the
BaseLayout writes its contents to cache which is a specially created
Bitmap object based on the size of the original layout. view plaincopy
to clipboardprint?
class FoldableItemLayout extends FrameLayout {
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
Bitmap cacheBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mBaseLayout.setCacheCanvas(new Canvas(cacheBitmap));
}
}
class BaseLayout extends FrameLayout {
private Canvas mCacheCanvas;
private void setCacheCanvas(Canvas cacheCanvas) {
mCacheCanvas = cacheCanvas;
}
#Override
public void draw(Canvas canvas) {
mCacheCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
super.draw(mCacheCanvas);
}
}
In addition, we needed to use two extra Views (PartView) – for the
upper and lower image halves – which would display the corresponding
data in cache that represents the upper and lower halves of the image
(Bitmap). Both Views encompass the entire area of the main layout, but
display only the required parts. To achieve this effect, we calculated
the Bitmap limits – and in the onDraw() method we made Canvas draw the
required part via the drawBitmap (Bitmap bitmap, Rect src, RectF dst,
Paint paint) method.
Then we managed to rotate these extra Views by setting the
setRotationX() method to the corresponding angle, achieving
independent rotation of the lower and upper parts of the images. To
pull this off, we add a new parameter for the FoldableItemLayout –
with the name FoldRotation.
Source: How to Make a Paper Folding Animation in Android?

IllegalArgumentException: width and height must be > 0 while loading Bitmap from View

I'm trying to draw on an ImageViewTouch, a library which enables pinch zooming. I'm able to draw over the image using Canvas, but when I zoom the image, the drawing disappears.
For this, I'm trying to convert the view to a bitmap and set theImageBitmap for this same view. Here's the code:
mImage.setDrawPath(true);
mImage.setImageBitmap(loadBitmapFromView(mImage));
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getWidth(), v.getHeight());
v.draw(c);
return b;
}
When I do this, I get the following log error:
07-11 21:13:41.567: E/AndroidRuntime(20056): java.lang.IllegalArgumentException: width and height must be > 0
07-11 21:13:41.567: E/AndroidRuntime(20056): at android.graphics.Bitmap.createBitmap(Bitmap.java:638)
07-11 21:13:41.567: E/AndroidRuntime(20056): at android.graphics.Bitmap.createBitmap(Bitmap.java:620)
If I remove the loadBitmapFromView call the drawing normally appears over the image. When I try to do any interaction with the image (like zooming in or out), the drawing disappears, remaing only the background image, which is a picture.
--- EDIT ---
Here's some more code placed after the loadBitmapFromView call. The case is: I have a radio group listenner and when I check some radio button, I have to load the image and draw some possibles drawings over it.. then I'm trying to convert everything (the image and the drawings) into only one bitmap.
Here's the ohter part of the code:
bitmap = BitmapUtils.decodeSampledBitmapFromResource(root + DefinesAndroid.CAMINHO_SHOPPINGS_SDCARD + nomeImagemAtual, size.x, size.y);
mImage.setImageBitmap(bitmap);
After that, I draw everything I have to draw and try to convert the view to bitmap using the loadImageBitmap method I have shown.
the decodeSampledBitmapFromResource method I got from this link on android developers http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
I finally figured out a solution for my problem.
I'm using the post method of the View, which will be executed only after the view measuring and layouting, this way getWidth() and getHeight() returns the actual width and height.
Here's the code sample:
mImage.post(new Runnable() {
#Override
public void run() {
mImage.setImageBitmap(loadBitmapFromView(mImage));
}
});
Hope it also helps someone else :)
If everything else is correct you are executing your code too early.
The view's layout has not yet been measured by Android. Try executing in OnResume just to see if this is your problem.
Cheers!
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
loadBitmapFromView(mImage);
}
}, 1000);
java.lang.IllegalArgumentException: width and height must be > 0 is because in your Bitmap.createBitmap() call v.getLayoutParams().width or v.getLayoutParams().height is 0. Probably the LayoutParams are wrongly set in mImage.
Update: setLayoutParams() for the mImage before using createBitmap(). Something like this:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
mImage.setLayoutParams(layoutParams);
In my case, I forgot to set orientation to vertical for parent LinearLayout and the default value of horizontal has been set, so I set it and my problem gone...
android:orientation="vertical"
I'm not sure if it relates to this specific question , but I had the same error and I solved it with getting a image source , in the layout xml file , from drawable instead of from mipmap (of course I had to put the image there before).

WebView Content size

I have some problems with webview content size. I need to convert full web page to a single image and tried this code
Bitmap screenshot;
screenshot = Bitmap.createBitmap(view.getWidth(), view.getContentHeight(), Bitmap.Config.ARGB_8888);
final Canvas c =new Canvas(screenshot);
view.draw(c);
where "view" is a WebView object
But the result is an image that have 1024x769 pixels. My web page is quite bigger (height is about 2000px). I've tried different ways to solve this problem, but still with zero rezult.
I find solution.
If you have the same problem, you can add onPicture Listener to your webview, like this
web.setPictureListener(new WebView.PictureListener() {
public void onNewPicture(WebView view, Picture picture) {
float temp = (float) view.getHeight();
height = view.getContentHeight() * a;
}
});
and get all your need height and width
For taking the picture of webview you have to enabled cache and from cache you can get it as a bitmap ,below i am mentioning the code hope will useful for you-
webview.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(webview.getDrawingCache());
webview.setDrawingCacheEnabled(false);
it is working for me just check your side,will wait for your acceptance of answer.thanks

Android ui element animation

I'm trying to animate UI elements. I would like to move an editText and a Button from the middle to the top of the screen and display results of an http call below them in a table. It would be great if anyone could point me in the right direction, at this point I don't know wether I should use Java or XML for this.
Thanks in advance.
Use Translation framework to achieve this, this works as:
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
So you need to write your code for moving view in y-axis direction, as follows:
mAnimation = new TranslateAnimation(0, 0, 0, 599);
mAnimation.setDuration(10000);
mAnimation.setFillAfter(true);
mAnimation.setRepeatCount(-1);
mAnimation.setRepeatMode(Animation.REVERSE);
view.setAnimation(mAnimation);
Here view may be anything, textview, imageView etc.
accepted answer caused an error inside my code, code snippet below almost identical to accepted answer & worked without causing errors, to slide an object off the screen. i needed gestures tied to keyPad to also 'slide away' , and switched from TranslateAnimation to ObjectAnimator (second block of code below).
final LinearLayout keyPad = (LinearLayout)findViewById(R.id.keyPad);
moveKeyPadUp(keyPad);
private void moveKeyPadUp(LinearLayout keyPad){
Animation animation = new TranslateAnimation(0,0,0,-500);
animation.setDuration(1000);
animation.setFillAfter(true);
keyPad.startAnimation(animation);
}
private void moveKeyPadUpdated(LinearLayout keyPad){
ObjectAnimator mover = ObjectAnimator.ofFloat(keyPad,"translationY",0,-500);
mover.setDuration(300);
mover.start();
}

Categories