Android - ImageView设置背景资源的问题

3

我有一个图像视图,宽度设置为400dp,高度设置为300dp。这个图像视图用于列表视图中,在该列表视图中,我从url加载图片。在加载图片时,我显示一个类似于进度条的动画,设置如下:

imageView.setBackgroundResource(R.drawable.progress);
            final AnimationDrawable startAnimation = (AnimationDrawable) imageView
                    .getBackground();

            startAnimation.start();

一旦图片加载完成,我将移除此动画,并将已加载的图像设置如下:

imageView.setBackgroundResource(0);
            imageView.setImageBitmap(bitmap);

但问题是,动画也使用了指定的宽度和高度,即400dp和300dp,我如何仅减小动画可绘制对象的大小?

我的动画文件:progress.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/anim_black"
    android:oneshot="false" >

    <item
        android:drawable="@drawable/frame_1"
        android:duration="100"/>
    <item
        android:drawable="@drawable/frame_2"
        android:duration="100"/>
    <item
        android:drawable="@drawable/frame_3"
        android:duration="100"/>
    <item
        android:drawable="@drawable/frame_4"
        android:duration="100"/>
    <item
        android:drawable="@drawable/frame_5"
        android:duration="100"/>
    <item
        android:drawable="@drawable/frame_6"
        android:duration="100"/>
    <item
        android:drawable="@drawable/frame_7"
        android:duration="100"/>
    <item
        android:drawable="@drawable/frame_8"
        android:duration="100"/>
    <item
        android:drawable="@drawable/frame_9"
        android:duration="100"/>
    <item
        android:drawable="@drawable/frame_10"
        android:duration="100"/>
    <item
        android:drawable="@drawable/frame_11"
        android:duration="100"/>
    <item
        android:drawable="@drawable/frame_12"
        android:duration="100"/>

</animation-list>

帧1、帧2……帧12是我通过分割GIF图像获得的PNG文件。


1
你尝试过将动画可绘制对象设置为setImageResource(而不是setBackgroundResource)吗? - MatrixDev
2个回答

2

在您的情况下,应使用setImageResource进行动画处理。背景图像始终按比例缩放以适应整个图像。在开始动画之前和图像加载完成后,您还需要更改ScaleType

imageView.setImageResource(R.drawable.progress);
imageView.setScaleType(ScaleType.CENTER);

final AnimationDrawable startAnimation = (AnimationDrawable) imageView.getDrawable();
startAnimation.start();

// and once image is loaded
imageView.setImageBitmap(bitmap);
imageView.setScaleType(ScaleType.CENTER_INSIDE);

谢谢。工作完美。但是动画以这种方式显示。垂直居中和水平极左。如何使其在水平和垂直方向上都居中? - suresh cheemalamudi
<ImageView android:id="@+id/place_image" android:layout_width="wrap_content" android:layout_height="300dp" android:scaleType="centerCrop" /> - suresh cheemalamudi
1
android:layout_width="wrap_content" 是问题所在。尝试按照你的问题描述将其更改为 400dp - Vladimir Mironov

1
使用自定义进度条会更好,你需要做的是使用与ImageView相同大小的FrameLayout,并将ProgressBar和ImageView放在同一个视图中,在ImageView上方。一旦图像加载完成,将ProgressBar的可见性更改为View.GONE或View.INVISIBLE。这可以在列表视图中很好地使用,而且没有任何内存问题。要实现自定义ProgressBar,请使用此链接。
     <ProgressBar
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:indeterminateDrawable="@anim/progress_bar_anim" >
      </ProgressBar>

你的可绘制进度条动画 progress_bar_anim.xml 位于哪里

 <?xml version="1.0" encoding="utf-8"?>
  <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/pro_indi"
    android:pivotX="50%"
    android:pivotY="50%" />

pro_indi是任何要旋转的圆形图像。

<FrameLayout>
     <ImageView>
     <Progressbar>
</FrameLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接