我需要有关启动动画的帮助。 我有一个第一堂课(Graph),我想在我的GameView类上开始Graph对象的动画。 GameView类: 这是定义动画的xml: 我尝试使用animate()方法启动动画。 一切工作正常,但我无法启动动画。 我将不胜感激。 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在关注 Google 提供的如何将 AnimationDrawable 与 ImageView 一起使用的示例。 你可以在这里找到它: http : //developer.android.com/guide/topics/graphics/drawable-animation.html
imageView.setBackgroundResource(R.drawable.animation);
AnimationDrawable animation = (AnimationDrawable)imageView.getBackground();
animation.start();
当我运行它时,我收到错误:
java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
谷歌似乎认为这应该有效,但如果你不能将 BitmapDrawable 转换为 AnimationDrawable 我不确定这应该如何工作?
我想出了解决这个问题的办法。
imageView.setImageDrawable(getResources().getDrawable(R.drawable.animation));
AnimationDrawable animation = (AnimationDrawable) imageView.getDrawable();
animation.start();
我不知道为什么 Google 的文档说要使用背景,但是使用 setImageDrawable 和 getDrawable 是有效的。 老实说,无论如何,以这种方式工作比以其他方式工作更有意义。
我有同样的问题。 我知道这个线程已经有几个月了,但也许有人会读到我的经历。
我不知道为什么,但谷歌在将其用于动画时不接受他的图片名称中的“_”之类的空格标记。 我使用诸如“loading_frame1”之类的名称,但它不起作用。 我将名称更改为“loadingframe1”之类的名称,并且可以正常工作....
前:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@drawable/loading_frame1" android:duration="100" />
<item android:drawable="@drawable/loading_frame2" android:duration="100" />
<item android:drawable="@drawable/loading_frame3" android:duration="100" />
<item android:drawable="@drawable/loading_frame4" android:duration="100" />
<item android:drawable="@drawable/loading_frame5" android:duration="100" />
<item android:drawable="@drawable/loading_frame6" android:duration="100" />
<item android:drawable="@drawable/loading_frame7" android:duration="100" />
<item android:drawable="@drawable/loading_frame8" android:duration="100" />
<item android:drawable="@drawable/loading_frame9" android:duration="100" />
<item android:drawable="@drawable/loading_frame10" android:duration="100" />
<item android:drawable="@drawable/loading_frame11" android:duration="100" />
<item android:drawable="@drawable/loading_frame12" android:duration="100" />
<item android:drawable="@drawable/loading_frame13" android:duration="100" />
<item android:drawable="@drawable/loading_frame14" android:duration="100" />
<item android:drawable="@drawable/loading_frame15" android:duration="100" />
<item android:drawable="@drawable/loading_frame16" android:duration="100" />
<item android:drawable="@drawable/loading_frame17" android:duration="100" />
<item android:drawable="@drawable/loading_frame18" android:duration="100" />
<item android:drawable="@drawable/loading_frame19" android:duration="100" />
<item android:drawable="@drawable/loading_frame20" android:duration="100" />
</animation-list>
后:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@drawable/loadingframe1" android:duration="100" />
<item android:drawable="@drawable/loadingframe2" android:duration="100" />
<item android:drawable="@drawable/loadingframe3" android:duration="100" />
<item android:drawable="@drawable/loadingframe4" android:duration="100" />
<item android:drawable="@drawable/loadingframe5" android:duration="100" />
<item android:drawable="@drawable/loadingframe6" android:duration="100" />
<item android:drawable="@drawable/loadingframe7" android:duration="100" />
<item android:drawable="@drawable/loadingframe8" android:duration="100" />
<item android:drawable="@drawable/loadingframe9" android:duration="100" />
<item android:drawable="@drawable/loadingframe10" android:duration="100" />
<item android:drawable="@drawable/loadingframe11" android:duration="100" />
<item android:drawable="@drawable/loadingframe12" android:duration="100" />
<item android:drawable="@drawable/loadingframe13" android:duration="100" />
<item android:drawable="@drawable/loadingframe14" android:duration="100" />
<item android:drawable="@drawable/loadingframe15" android:duration="100" />
<item android:drawable="@drawable/loadingframe16" android:duration="100" />
<item android:drawable="@drawable/loadingframe17" android:duration="100" />
<item android:drawable="@drawable/loadingframe18" android:duration="100" />
<item android:drawable="@drawable/loadingframe19" android:duration="100" />
<item android:drawable="@drawable/loadingframe20" android:duration="100" />
</animation-list>
这里是 LoadingAnimation.class 列表
package com.justkidding.animation;
import android.support.v7.app.ActionBarActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class LoadingAnimation extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading_animation);
}
@Override
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
ImageView animation = (ImageView)findViewById(R.id.aniimage);
animation.setBackgroundResource(R.drawable.loading_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) animation.getBackground();
if(hasFocus) {
frameAnimation.start();
} else {
frameAnimation.stop();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.loading_animation, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
谷歌的代码有效。 导致我来到这里的“无法投射问题”是因为我没有注意并将我的 animation.xml 放在 res.anim 而不是 res.drawable 中。
但是我同意使用 setImageDrawable 和 getDrawable 效果更好。
关于这个问题,我对文档中谷歌示例代码的细节有一些疏忽,这可能是使用指南的几个人的情况。
有一个单独的 xml 文件保存可绘制对象,指示转换并具有标记:
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />>
</animation-list>
上面的文件名为 Rocket_thrust,在以下几行中将这个文件设置为 backgroundDrawable:
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
尝试一下,并确保文档没有错误。
祝你好运。
完成动画的完整过程是: 1. 使用 imageView 创建 XML 布局 2. 假设 drawable/animation.xml 为动画创建 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/twit" android:duration="120"></item>
<item android:duration="120" android:drawable="@drawable/a111"></item>
<item android:duration="120" android:drawable="@drawable/a2"></item>
<item android:duration="120" android:drawable="@drawable/a3"></item>
<item android:duration="120" android:drawable="@drawable/a4"></item>
<item android:duration="120" android:drawable="@drawable/a5"></item>
<item android:duration="120" android:drawable="@drawable/a6"></item>
</animation-list>
现在 3. 创建主要活动
然后输入此代码
public class AnimationMe extends AppCompatActivity {
private ImageView imgView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logoo);
imgView = (ImageView) findViewById(R.id.imgView);
// the frame-by-frame animation defined as a xml file within the drawable folder
/*imgView.setBackgroundResource(R.drawable.animation);*/
imgView.setImageDrawable(getResources().getDrawable(R.drawable.animation));
// It's not possible to start the animation during the onCreate.
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable animationDrawable = (AnimationDrawable)imgView.getDrawable();
if(hasFocus)
{
animationDrawable.start();
}
else
{
animationDrawable.stop();
}
}
}
*
注意:ImageView 具有可绘制的背景,并为特定图像指定一个 animation.xml 名称,然后在 AnimationDrawable 中使用 imageview.getDrawable 调用。 ---- 你不能在 onCreate 方法中运行动画。 在 onCreate() 中的 Imageview 中设置 drawable 属性,但在 onCreate() 块之外调用 AnimationDrawable 方法。
*
当然它会起作用!
只是根据我的经验为此页面添加更多答案,因为 Stackoverflow 似乎对此问题的答案非常有限
我的情况是我尝试为我的背景布局设置动画,该布局使用可绘制的圆角半径。 我收到 logcat 错误
java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
结果我必须将布局文件上的背景属性设置为这个可绘制文件
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
<item android:drawable="@drawable/rounded_corner" android:duration="80" />
<item android:drawable="@drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="@drawable/rounded_corner" android:duration="80" />
<item android:drawable="@drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="@drawable/rounded_corner" android:duration="80" />
<item android:drawable="@drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="@drawable/rounded_corner" android:duration="80" />
然后在我的主要活动上调用此代码
val backgroundAnim = info_layout?.background as AnimationDrawable
backgroundAnim.start()
我的错误是之前我将@drawable/rounded_corner
作为背景属性放在布局文件上。 希望这可以帮助某人,因为我花了 3 个小时来解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.