I'm developing a android wallpaper app. I will show albums using grid layout, when user clicks on album, first image of album will be displayed in full screen. If width of the image is more than the screen width then image will be animated horizontally. User can use 'next' and 'previous' button to show remaining images in album.
My problem is, when full image is animating, if user drags the image to left or right to see the full image (instead of waiting to see image through animation) then image's background appears.
1) I want to restrict the image to boundaries. Even user drags the image, image should not be moved from edges. 2) And one more issue is, when user drags the image, suppose 2nd half of image is present on screen and when user clicks the next image, then next image is started animation from half, due to this, complete background appears to user.
How I can rectify these issues. Please help. Please check the attached screen shots and code what I've used.
private void fetchFullResolutionImage(Wallpaper selectedPhoto) {
//fullImageView.setTranslationX(0);
//fullImageView.clearAnimation();
//resetAnimation();
/*if(isAnimated) {
fullImageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
int[] locations = new int[2];
fullImageView.getLocationOnScreen(locations);
imageXposition = locations[0];
Toast.makeText(getApplicationContext(),
"first "+((Float)imageXposition).toString(), Toast.LENGTH_SHORT)
.show();
fullImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
ObjectAnimator obj = ObjectAnimator.ofFloat(fullImageView, "translationX", 0f,-imageXposition);
obj.setRepeatMode(0);
obj.setRepeatMode(1);
obj.setDuration(1000);
obj.start();
}
});
}*/
//fullImageView.clearAnimation();
String url = selectedPhoto.getPhotoJson();
// show loader before making request
pbLoader.setVisibility(View.VISIBLE);
llSetWallpaper.setVisibility(View.GONE);
llNextWallpaper.setVisibility(View.GONE);
llPreviousWallpaper.setVisibility(View.GONE);
// volley's json obj request
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url,
null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// Parsing the json response
JSONObject entry = response
.getJSONObject(TAG_ENTRY);
JSONArray mediacontentArry = entry.getJSONObject(
TAG_MEDIA_GROUP).getJSONArray(
TAG_MEDIA_CONTENT);
JSONObject mediaObj = (JSONObject) mediacontentArry
.get(0);
final String fullResolutionUrl = mediaObj
.getString(TAG_IMG_URL);
// image full resolution widht and height
final int width = mediaObj.getInt(TAG_IMG_WIDTH);
final int height = mediaObj.getInt(TAG_IMG_HEIGHT);
final ImageLoader imageLoader = AppController
.getInstance().getImageLoader();
// We download image into ImageView instead of
// NetworkImageView to have callback methods
// Currently NetworkImageView doesn't have callback
// methods
imageLoader.get(fullResolutionUrl,
new ImageListener() {
@Override
public void onErrorResponse(
VolleyError arg0) {
Toast.makeText(
getApplicationContext(),
getString(R.string.msg_wall_fetch_error),
Toast.LENGTH_LONG).show();
}
@Override
public void onResponse(
ImageContainer response,
boolean arg1) {
if (response.getBitmap() != null) {
fullImageView
.setImageBitmap(response
.getBitmap());
isAnimated = true;
animation = new TranslateAnimation(0.0f, -(vWidth-aWidth),0.0f, 0.0f);
//vWidth is double the aWidth, aWidth is screen's width
animation.setDuration(7000);
animation.setRepeatCount(1);
animation.setRepeatMode(2);
animation.setFillAfter(true);*/
//animation.setFillEnabled(true);
pbLoader.setVisibility(View.GONE);
llSetWallpaper
.setVisibility(View.VISIBLE);
llPreviousWallpaper
.setVisibility(View.VISIBLE);
llNextWallpaper
.setVisibility(View.VISIBLE);
fullImageView.startAnimation(animation);
}
}
});
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
getString(R.string.msg_unknown_error),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// unable to fetch wallpapers
// either google username is wrong or
// devices doesn't have internet connection
Toast.makeText(getApplicationContext(),
getString(R.string.msg_wall_fetch_error),
Toast.LENGTH_LONG).show();
}
});
// Remove the url from cache
AppController.getInstance().getRequestQueue().getCache().remove(url);
// Disable the cache for this url, so that it always fetches updated
// json
jsonObjReq.setShouldCache(false);
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
when ever user clikcs next or previous button, fetchFullResolutionImage() method will be called.
Tried resetting animation with Matrix.
public void resetAnimation() {
Matrix m = new Matrix();
m.setScale(1f, 1f);
fullImageView.setImageMatrix(m);
fullImageView.invalidate();
}
but no use.
Please find the images from below URL
http://awesomewallpapers.in/imageview/