我有RelativeLayout,里面有3个相对中继。第二级相对布局具有下载的imageView,并且可以具有不同的大小(高度,宽度)。我想检查imageView是否适合我的布局。
我的第二级相对布局如下所示:
<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)如果图像太小,我需要缩放他以适应布局高度
2)如果图像与布局大小相同,那没关系。
3)如果图像太大,我需要添加动画来慢慢上下移动
对于移动动画我使用此代码并且它工作正常(如果我输入硬编码值):
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
但主要问题是我无法获得正确的图像高度值来设置动画移动范围。
我应该怎样才能启动仅在imageView范围内上/下移动的动画?
答案 0 :(得分:0)
最后,我做了我需要的工作 - 缩放imageView以适应布局宽度并开始动画上下移动并以这种方式显示图像。
首先,xml看起来像这样。 image_layout是子布局,位于其他RelativeLayout内。它有固定的高度(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>
代码如下所示:
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);
}
}
});
所以我正在下载图片,如果下载成功,我会缩放它以适应并开始动画(向上和向下移动)。如果下载不成功,我会在drawables中显示虚拟图像。还需要使用dpToPx方法来计算比例。
private int dpToPx(int value) {
Resources r = InformationFragment.this.getResources();
return (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
value,
r.getDisplayMetrics()
);
}
显示动画的最后一种方法(需要修复动画速度公式):
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);
}