Android-imageview.setBackgroundResource问题
我有一个图像视图,我已经设置为400dp(宽度)和300dp(高度)。此图像视图用于从url加载图像的列表视图。在加载图像期间,我显示了一个类似进度条的动画,设置如下:Android-imageview.setBackgroundResource问题,android,android-animation,android-imageview,Android,Android Animation,Android Imageview,我有一个图像视图,我已经设置为400dp(宽度)和300dp(高度)。此图像视图用于从url加载图像的列表视图。在加载图像期间,我显示了一个类似进度条的动画,设置如下: imageView.setBackgroundResource(R.drawable.progress); final AnimationDrawable startAnimation = (AnimationDrawable) imageView .getBack
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文件。您应该使用
setImageResource
制作动画。背景图像始终缩放以适合整个图像。在开始动画之前和加载图像之后,还需要更改缩放类型
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);
来吧,伙计。你是在用更长的方法做这件事。最好使用定制的Progressbar。您需要做的是使用与Imageview大小相同的框架布局,并将进度条&Imageview放入Imageview上方的同一视图中。加载图像后,将progressbar的可见性更改为View.GONE或View.INVISIBLE。它也可以在ListView中公平地使用,而不会出现任何内存问题。要实现自定义progressbar,请使用以下命令
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="@anim/progress_bar_anim" >
</ProgressBar>
可绘制进度\u bar\u 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>
您是否尝试将动画可绘制设置为setImageResource(而不是setBackgroundResource),谢谢。工作得很好。但是动画是以这种方式显示的。垂直-居中,水平-最左侧。如何使其水平和垂直居中?android:layout\u width=“wrap\u content”
是问题所在。尝试将其更改为问题中所述的400dp