这个问题是关于多个问题的跟进,我在这个论坛上询问为什么我一直在尝试的动画无效。
简要介绍以前的问题:
我已经得出结论,我的主要线程可能正在做过多的工作,这可能是动画没有正确发生的原因。
因此使用2个线程我编码了以下序列,但仍然得到相同的结果。 有人可以解释一下发生了什么,为什么我不能让一个简单的动画工作?
我的ButtonAnime类
public class ButtonAnime extends Activity{
private static int SPLASH_TIME_OUT = 500;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AnimationDrawable animate= new AnimationDrawable();
final ImageView imageView = (ImageView) findViewById(R.id.iVArcRail_2);
new Thread(new Runnable() { <---- thread 1
@Override
public void run() {
// TODO Auto-generated method stub
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 24;
options.inPurgeable = true;
options.inScaled = true;
Bitmap b;
Drawable d;
int i =5;
b = BitmapFactory.decodeResource(getResources(),R.drawable.ab1, options);
d = new BitmapDrawable(getResources(),b);
animate.addFrame(d, i);
b = BitmapFactory.decodeResource(getResources(),R.drawable.ab2, options);
d = new BitmapDrawable(getResources(),b);
animate.addFrame(d, i);
b = BitmapFactory.decodeResource(getResources(),R.drawable.ab3, options);
d = new BitmapDrawable(getResources(),b);
animate.addFrame(d, i);
b = BitmapFactory.decodeResource(getResources(),R.drawable.ab4, options);
d = new BitmapDrawable(getResources(),b);
animate.addFrame(d, i);
b = BitmapFactory.decodeResource(getResources(),R.drawable.ab5, options);
d = new BitmapDrawable(getResources(),b);
animate.addFrame(d, i);
b = BitmapFactory.decodeResource(getResources(),R.drawable.ab6, options);
d = new BitmapDrawable(getResources(),b);
animate.addFrame(d, i);
b = BitmapFactory.decodeResource(getResources(),R.drawable.ab7, options);
d = new BitmapDrawable(getResources(),b);
animate.addFrame(d, i);
b = BitmapFactory.decodeResource(getResources(),R.drawable.ab8, options);
d = new BitmapDrawable(getResources(),b);
animate.addFrame(d, i);
b = BitmapFactory.decodeResource(getResources(),R.drawable.ab9, options);
d = new BitmapDrawable(getResources(),b);
animate.addFrame(d, i);
b = BitmapFactory.decodeResource(getResources(),R.drawable.ab10, options);
d = new BitmapDrawable(getResources(),b);
animate.addFrame(d, i);
imageView.setBackground(animate);
imageView.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
animate.start();
}
});
}
});
new Handler().postDelayed(new Runnable() { <-- thread 2
@Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this , Home.class);
startActivity(i);
finish();
System.gc();
}
}, 50);
}
}
在按钮点击加载动画期间使用anime_loading ..后的log cat
GC_EXPLICIT freed 10K, 1% free 27351K/27444K, paused 6ms+7ms, total 71ms
GC_FOR_ALLOC freed 6098K, 22% free 23220K/29404K, paused 125ms, total 126ms
GC_FOR_ALLOC freed <1K, 15% free 25169K/29404K, paused 185ms, total 189ms
GC_FOR_ALLOC freed <1K, 8% free 27118K/29404K, paused 215ms, total 218ms
GC_FOR_ALLOC freed <1K, 2% free 29068K/29404K, paused 139ms, total 140ms
Grow heap (frag case) to 30.367MB for 1995856-byte allocation
Clamp target GC heap from 32.367MB to 32.000MB
GC_FOR_ALLOC freed <1K, 2% free 31017K/31356K, paused 235ms, total 235ms
Clamp target GC heap from 32.368MB to 32.000MB
GC_FOR_ALLOC freed <1K, 2% free 31018K/31356K, paused 232ms, total 233ms
Forcing collection of SoftReferences for 1995856-byte allocation
Clamp target GC heap from 32.368MB to 32.000MB
GC_BEFORE_OOM freed <1K, 2% free 31018K/31356K, paused 290ms, total 290ms
Out of memory on a 1995856-byte allocation.
"main" prio=5 tid=1 RUNNABLE
| group="main" sCount=0 dsCount=0 obj=0xb3a90c90 self=0xb8f64380
| sysTid=1723 nice=0 sched=0/0 cgrp=apps handle=-1225301676
| state=R schedstat=( 5530000000 8170000000 2275 ) utm=486 stm=67 core=0
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:575)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:410)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.loadDrawable(Resources.java:2110)
at android.content.res.Resources.getDrawable(Resources.java:700)
at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2092)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.ImageView.<init>(ImageView.java:129)
at android.widget.ImageView.<init>(ImageView.java:119)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1928)
at com.example.thomas.anime.Button_Anime.onCreate(Button_Anime.java:21)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
--- allocation failed for scaled bitmap
D/AndroidRuntime(1723): Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0xb3a8fb90)
log cat表示使用anime_loading时发生的错误msf
FATAL EXCEPTION: main
Process: com.example.thomas, PID: 1723
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thomas/com.example.thomas.anime.Button_Anime}: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
E/AndroidRuntime(1723): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1928)
at com.example.thomas.anime.Button_Anime.onCreate(Button_Anime.java:21)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
... 24 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:575)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:410)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.loadDrawable(Resources.java:2110)
at android.content.res.Resources.getDrawable
at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2092)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.ImageView.<init>(ImageView.java:129)
at android.widget.ImageView.<init>(ImageView.java:119)
... 27 more
答案 0 :(得分:0)
为什么不创建像这样的动画列表,而不是在运行时解码资源:
进入动画文件夹
anim_button.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/animation1"
android:duration="@integer/frame_animation_time_ms"/>
<item
android:drawable="@drawable/animation2"
android:duration="@integer/frame_animation_time_ms"/>
<item
android:drawable="@drawable/animation3"
android:duration="@integer/frame_animation_time_ms"/>
<item
android:drawable="@drawable/animation4"
android:duration="@integer/frame_animation_time_ms"/>
<item
android:drawable="@drawable/animation5"
android:duration="@integer/frame_animation_time_ms"/>
<item
android:drawable="@drawable/animation6"
android:duration="@integer/frame_animation_time_ms"/>
</animation-list>
这是在布局
内部<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/anim_button" />
这是运行它的代码
private void animateImageView() {
AnimationDrawable imageViewAnimation = (AnimationDrawable) imageView.getBackground();
imageViewAnimation.setVisible(true, true);
}