- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题是我在这个论坛上问过的多个问题的跟进,有关为什么我一直尝试的动画不起作用。
简要介绍一下以前的问题:
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);
}
}
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)
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
最佳答案
与其在运行时解码资源,不如创建一个动画列表,如下所示:
进入动画文件夹
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);
}
关于android - 使用线程或异步任务的位图工厂动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24341876/
我应该执行以下操作: 可能通过服务/工厂,使用 $q(异步)查询 API 以获取大型名称数据集 有另一个服务(也是异步的),它应该只返回上述工厂的元素,如果它们与某个字符串(搜索字段)匹配。目的是缩小
我有一个通用的基类。我有一个实现基类的具体类。 我将如何创建工厂类/方法来交付不同类型的具体类? 举个例子: public class ReceiverBase where T : IInte
我正在查看以下链接中的 Ninject Factory 扩展: http://www.planetgeek.ch/2011/12/31/ninject-extensions-factory-intro
工厂、提供商和服务这三个术语之间有什么区别? 刚刚了解 NHibernate 及其存储库模式(POCO 类等)。 最佳答案 工厂:通过将一堆位组合在一起或基于某种上下文选择类型来组装类 Provide
使用CGLIB我可以做到 final var enhancer = new Enhancer(); enhancer.setUseCache(false); enhancer.setSuperclas
我试图在 Kotlin 中使用伴随对象工厂方法(相当于 Java 中的静态工厂方法)创建一个嵌套内部类。这是我的代码的简化版本。 class OuterClass { var myData:L
我正在为我的大学做一个项目,但遇到了问题。 基本上,该项目由一个客户端-服务器应用程序组成,我想创建一个用于通信的 Packet 类。数据包由 header 和主体组成。现在问题来了。我可以有一些不同
这个问题在这里已经有了答案: Why doesn't polymorphism work without pointers/references? (6 个答案) What is object sl
我正在制作一个套接字工厂。我希望每个外部应用程序都使用 Socket 类的接口(interface),它是几个类(ServerSocketTCP、ClientSocketTCP、ServerSocke
我是 angularjs 的新手,我正在尝试创建一个小型电影数据库。这是我第一次使用工厂,我想确保这是正确的方法,以及如何在另一个功能中使用这个工厂,如下所示? 我希望这个工厂只运行一次,这样我就可以
这个问题在这里已经有了答案: Java inner class and static nested class (28 个答案) 关闭 5 年前。 public class DataFactory
我看过很多关于 C++ 工厂的帖子,但到目前为止我还没有看到解决我的问题的解决方案。 (虽然我可能遗漏了一些东西。) 示例控制台应用程序: #include #include #include
这是一个简单的 C++ 项目,有 2 种设计模式:单例和工厂,sigleton 也是一个模板化类,一个接口(interface) (IHash) 和一个类 (Hash1)。一个简单的工厂类 (Hash
这个问题类似于Factory and generics ,并且可能有相同的答案,但它是不同的。我有一个通用基类,它将由完全独立的 JAR 中的类进行扩展。所述 JAR 应该能够在不更改任何其他代码的情
问题是我需要为传递的类创建一个新实例 有没有办法重写这个函数,让它可以接受任意数量的参数? function createInstance(ofClass, arg1, arg2, arg3, ...
我想用简单的 C++ 语法创建一个简单的工厂方法: void *createObject(const char *str,...) { if(!strcmp("X",str)) retu
经过大约 10 个月的程序化 PHP 学习后,我现在正尝试着手研究基本的 OOP 原则和设计模式。这是一个爱好,我没有那么多时间去追求它,所以请原谅这个问题的水平很低。 我的网站(目前 100% 程序
我有一个简单的问题。 我如何编写一个工厂来定义使用 make() 或 create() 的关系,具体取决于原始调用 make() 还是 create()? 这是我的用例: 我有一个简单的工厂 /**
我正在尝试在延迟加载模块中提供 APP_BASE_HREF 注入(inject) token ,然而,工厂方法根本没有被调用。 在这里https://github.com/MaurizioCascia
我有以下 ast: import { factory as f } from 'typescript' const typeDeclaration = f.createTypeAliasDeclara
我是一名优秀的程序员,十分优秀!