我正在创建一个应用,用户从图库中选择图片并选择他/她选择的歌曲,然后点击button
'运行电影'。动画与歌曲一起播放。
我被困在动画播放部分,它只播放音乐,不显示任何图片。
当我从drawables中选取并在animation.xml
中指定的图像时,我成功地播放了带声音的动画。请指导我在哪里犯错。
更新:我可以获取用户选择的所有图片和歌曲路径。唯一的问题是播放frameanimation
。
这是代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation_sound);
m_imageView = (ImageView)findViewById(R.id.imageView1);
Bundle bundle = getIntent().getExtras();
imgPathArr = bundle.getStringArray("picsPath");
songPath = bundle.getString("songPath");
m_imageView.setBackgroundResource(R.drawable.animation);
frameAnimation = (AnimationDrawable)m_imageView.getBackground();
if(imgPathArr == null || imgPathArr.length == 0){
Toast.makeText(this, "Please selct images first", Toast.LENGTH_LONG).show();
}else{
for(int i =0 ; i< imgPathArr.length; i++){
Log.d("Activity", imgPathArr[i]);
**frameAnimation.addFrame(
Drawable.createFromPath(imgPathArr[i]), 5000);**
}
}
animation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="4000"
android:fromAlpha="0.0"
android:toAlpha="0.9" >
</animation-list>
当我从drawables中选择图像时,我的动画带有声音的代码
m_imageView.setBackgroundResource(R.anim.anim);
frameAnimation = (AnimationDrawable)m_imageView.getBackground();
anim.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/pic1"
android:fromAlpha="0.0"
android:toAlpha="0.9"
android:duration="5000"/>
<item
android:drawable="@drawable/pic2"
android:fromAlpha="0.0"
android:toAlpha="0.9"
android:duration="5000"/>
<item
android:drawable="@drawable/pic3"
android:fromAlpha="0.0"
android:toAlpha="0.9"
android:duration="5000"/>
</animation-list>
答案 0 :(得分:0)
“当我从drawables中挑选图像时,我成功地播放了带声音的动画。”
如果这对您有用,那么您可以使用这些方法将图片转换为BitmapDrawable
BitmapDrawable(Resources res, String filepath)
BitmapDrawable(Resources res, Bitmap bitmap)
来自文档 “包装位图并可以平铺,拉伸或对齐的Drawable。您可以从文件路径,输入流,XML通胀或Bitmap对象创建BitmapDrawable。” 强>
像这样使用
Drawable d =new BitmapDrawable(getResources(),filepath);
希望它适合你。干杯:)