I've got RelativeLayout, that has 3 Relative Relayouts inside. Second level Relative layout has imageView that is downloaded and may be in different size (height, width). I want to check if imageView fits my layout.
My second level Relative layout looks like this:
<RelativeLayout
android:id="@+id/tv_show_image_layout"
android:layout_width="400dp"
android:layout_height="200dp">
<ImageView
android:id="@+id/tvshow_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="matrix"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/dummy" />
</RelativeLayout>
1) If image is too small, I need to scale him to fit layout height
2) If image is same size as layout, It's ok.
3) If image is too big, I need to add animation to slowly move up and down
For moving animation I use this code and it works fine (if i put hardcoded values):
TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f,
-20.0f, 120.0f); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation.setDuration(9000); // animation duration
animation.setRepeatCount(15); // animation repeat count
animation.setRepeatMode(2); // repeat animation (left to right, right to left )
//animation.setFillAfter(true);
show_image.startAnimation(animation); // start animation
But main problem that I can't get correct value of image height to set animation moving ranges.
How should I get this to start animation that moves up/down only in imageView ranges?
Finally, i did what i needed - scaled imageView to fit layout width and started animation to move up/down and show image in this way.
Firstly, xml looks like this. image_layout is child layout at is placed inside other RelativeLayout. It has fixed height (200dp)
<RelativeLayout
android:id="@+id/image_layout"
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:id="@+id/tvshow_imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transformPivotX="0dp"
android:transformPivotY="0dp"
android:scaleType="fitXY"
android:src="@drawable/dummy"/>
</RelativeLayout>
Code looks like this:
UrlImageViewHelper.setUrlDrawable(show_image, url, R.drawable.dummy, new UrlImageViewCallback() {
@Override
public void onLoaded(ImageView imageView, Bitmap loadedBitmap,
String url, boolean loadedFromCache) {
if (!url.equalsIgnoreCase(imageURL)) {
show_image.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
int imageViewWidth = dpToPx(imageRelativeLayout.getWidth());
int imageViewHeight = dpToPx(imageRelativeLayout.getHeight());
float ratioWidth = (float) imageViewWidth / loadedBitmap.getWidth();
float animationBounds = loadedBitmap.getHeight() * ratioWidth - imageViewHeight;
imageView.setScaleX(ratioWidth);
imageView.setScaleY((ratioWidth));
if (animationBounds > 0)
showImageAnimation(animationBounds);
}
}
});
So I'm downloading image and if download is successful, i scale it to fit and start animation (moving up and down). If download is not successful I show dummy image that I have in drawables. Also it is needed to have dpToPx method to calculate scale ratio.
private int dpToPx(int value) {
Resources r = InformationFragment.this.getResources();
return (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
value,
r.getDisplayMetrics()
);
}
And last method to show animation (need fix on animation speed formula):
private void showImageAnimation(float animationBounds) {
TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f,
0.0f, -animationBounds);
animation.setDuration((int)animationBounds/20*1000);
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(Animation.REVERSE);
show_image.startAnimation(animation);
}