我是 Android 新手,在我的布局背景中播放动画 .gif 文件时遇到问题。当我运行应用程序时,背景是动画 .gif 的单帧,就像图片一样。我主要做了两个方面的研究:
下面的链接是我得到最多帮助的地方。
下面的链接让我认为我收到的错误与设置为私有(private)、公共(public)或 protected 类有关。
这是代码 ------------------------------------------- -------------------------------------------------- ----------------------
错误由::ERROR #::表示,代码下方有完整描述。
import java.io.InputStream;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(new MYGIFView()); :: ERROR 1 ::
}
}
private class MYGIFView extends View{ :: ERROR 2 ::
Movie movie,movie1;
InputStream is=null , is1=null;
long moviestart;
public GIFView(Context context) { :: ERROR 3 ::
super(context); :: ERROR 4 ::
is=context.getResources().openRawResource(R.drawable.swing);
movie=Movie.decodeStream(is);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
System.out.println("now="+now);
if (moviestart == 0) { // first time
moviestart = now;
}
System.out.println("\tmoviestart="+moviestart);
int relTime = (int)((now - moviestart) % movie.duration()) ;
System.out.println("time="+relTime+"\treltime="+movie.duration());
movie.setTime(relTime);
movie.draw(canvas,this.getWidth()/2-20,this.getHeight()/2-40);
this.invalidate();
}
}
::错误列表::错误列表::错误列表::错误列表::错误列表::错误列表::
::ERROR 1::构造函数 MYGIFView() 不可见
::ERROR 2::Implicit Super Constructor View() 未定义可见构造函数
::ERROR 3::方法的返回类型丢失
::ERROR 4::构造函数调用必须是构造函数中的第一条语句
.
.
--------- 更新 --------- 更新 ------------ 更新 ------------- -- 更新 ------------- 更新 ---------- 更新 --------- 更新 ------------更新 --------------- 更新 ------------- 更新 --------- 更新
.
.
我已经创建了动画列表 = res/drawable-hdpi/progress_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/frame02" android:duration="50" />
<item android:drawable="@drawable/frame03" android:duration="50" />
<item android:drawable="@drawable/frame04" android:duration="50" />
<item android:drawable="@drawable/frame05" android:duration="50" />
<item android:drawable="@drawable/frame06" android:duration="50" />
<item android:drawable="@drawable/frame07" android:duration="50" />
<item android:drawable="@drawable/frame08" android:duration="50" />
<item android:drawable="@drawable/frame09" android:duration="50" />
<item android:drawable="@drawable/frame10" android:duration="50" />
.........................................
<item android:drawable="@drawable/frame57" android:duration="50" />
<item android:drawable="@drawable/frame58" android:duration="50" />
</animation-list>
.
.
我创建了布局文件来托管 ImageView = res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView android:id="@+id/swing_play" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
.
Java类Activity调用ImageView和AnimationDrawable = com.example.apptwo/MainActivity.java
.
package com.exampleone.apptwo;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acivity_main);
ImageView img = (ImageView)findViewById(R.id.swing_play);
img.setBackgroundResource(R.drawable.progress_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
frameAnimation.start();
}
}
.
IDE 不会标记代码中的任何错误。但是,当我运行应用程序时,我的模拟器显示白色背景,并显示一条消息“不幸的是,AppTwo 已停止。”
ErrorLog 读取 =“未处理的事件循环异常”
LOGCAT ---------------------------------------------- -------------------------------------------------- ----------------------------------
这是 session 过滤器 LOGCAT
04-06 17:10:48.193: D/AndroidRuntime(989): Shutting down VM
04-06 17:10:48.193: W/dalvikvm(989): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-06 17:10:48.213: E/AndroidRuntime(989): FATAL EXCEPTION: main
04-06 17:10:48.213: E/AndroidRuntime(989): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apptwo/com.example.apptwo.MainActivity}: java.lang.NullPointerException
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-06 17:10:48.213: E/AndroidRuntime(989): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 17:10:48.213: E/AndroidRuntime(989): at android.os.Looper.loop(Looper.java:137)
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-06 17:10:48.213: E/AndroidRuntime(989): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 17:10:48.213: E/AndroidRuntime(989): at java.lang.reflect.Method.invoke(Method.java:511)
04-06 17:10:48.213: E/AndroidRuntime(989): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-06 17:10:48.213: E/AndroidRuntime(989): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-06 17:10:48.213: E/AndroidRuntime(989): at dalvik.system.NativeStart.main(Native Method)
04-06 17:10:48.213: E/AndroidRuntime(989): Caused by: java.lang.NullPointerException
04-06 17:10:48.213: E/AndroidRuntime(989): at com.example.apptwo.MainActivity.onCreate(MainActivity.java:14)
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.Activity.performCreate(Activity.java:5104)
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-06 17:10:48.213: E/AndroidRuntime(989): ... 11 more
要发布的 LogCat(所有消息)太多,但下面的这条似乎在错误发生时很重要。
04-06 17:10:48.213: E/AndroidRuntime(989): FATAL EXCEPTION: main
.
更新到包含内容 View 的 LOGCAT! ! !
: error opening trace file: No such file or directory (2)
: GC_FOR_ALLOC freed 67K, 7% free 2500K/2688K, paused 49ms, total 54ms
: I/dalvikvm-heap(1813): Grow heap (frag case) to 4.022MB for 1536016-byte allocation
: D/dalvikvm(1813): GC_FOR_ALLOC freed 2K, 5% free 3998K/4192K, paused 125ms, total 125ms
: D/dalvikvm(1813): GC_CONCURRENT freed <1K, 5% free 3998K/4192K, paused 5ms+15ms, total 105ms
: D/dalvikvm(1813): GC_FOR_ALLOC freed <1K, 5% free 3998K/4192K, paused 36ms, total 37ms
: Grow heap (frag case) to 5.485MB for 1536016-byte allocation
: GC_CONCURRENT freed <1K, 4% free 5498K/5696K, paused 75ms+14ms, total 156ms
: GC_FOR_ALLOC freed <1K, 4% free 5499K/5696K, paused 32ms, total 33ms
: Grow heap (frag case) to 6.950MB for 1536016-byte allocation
: GC_CONCURRENT freed <1K, 3% free 6999K/7200K, paused 74ms+17ms, total 156ms
: D/dalvikvm(1813): GC_FOR_ALLOC freed <1K, 3% free 6999K/7200K, paused 46ms, total 47ms
: I/dalvikvm-heap(1813): Grow heap (frag case) to 8.416MB for 1536016-byte allocation
: GC_CONCURRENT freed <1K, 3% free 8499K/8704K, paused 79ms+4ms, total 173ms
: GC_FOR_ALLOC freed <1K, 3% free 10001K/10208K, paused 33ms, total 34ms
: GC_FOR_ALLOC freed <1K, 2% free 11501K/11712K, paused 35ms, total 35ms
: GC_FOR_ALLOC freed <1K, 2% free 13002K/13216K, paused 38ms, total 38ms
: GC_FOR_ALLOC freed <1K, 2% free 14503K/14720K, paused 35ms, total 36ms
: GC_FOR_ALLOC freed <1K, 2% free 16003K/16224K, paused 39ms, total 40ms
: GC_FOR_ALLOC freed <1K, 2% free 17504K/17728K, paused 41ms, total 41ms
: GC_FOR_ALLOC freed <1K, 2% free 19004K/19232K, paused 42ms, total 43ms
: GC_FOR_ALLOC freed <1K, 2% free 20505K/20736K, paused 58ms, total 58ms
: GC_FOR_ALLOC freed <1K, 2% free 22006K/22240K, paused 137ms, total 138ms
: GC_FOR_ALLOC freed <1K, 1% free 23506K/23744K, paused 47ms, total 47ms
: GC_FOR_ALLOC freed <1K, 1% free 25007K/25248K, paused 48ms, total 48ms
: GC_FOR_ALLOC freed <1K, 1% free 26507K/26752K, paused 50ms, total 51ms
: GC_FOR_ALLOC freed <1K, 1% free 28008K/28256K, paused 56ms, total 57ms
: GC_FOR_ALLOC freed <1K, 1% free 29509K/29760K, paused 58ms, total 58ms
: Clamp target GC heap from 32.398MB to 32.000MB
: GC_FOR_ALLOC freed <1K, 1% free 31009K/31264K, paused 57ms, total 58ms
: Clamp target GC heap from 33.863MB to 32.000MB
: GC_FOR_ALLOC freed <1K, 1% free 32510K/32768K, paused 56ms, total 56ms
: Forcing collection of SoftReferences for 1536016-byte allocation
:Clamp target GC heap from 33.855MB to 32.000MB
: GC_BEFORE_OOM freed 9K, 1% free 32500K/32768K, paused 87ms, total 88ms
: Out of memory on a 1536016-byte allocation.
: "main" prio=5 tid=1 RUNNABLE
: | group="main" sCount=0 dsCount=0 obj=0x40a729a0 self=0x2a00bba8
: | sysTid=1813 nice=0 sched=0/0 cgrp=apps handle=1073849308
: | state=R schedstat=( 3245039730 1438341923 288 ) utm=275 stm=49 core=0
: at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
: at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
: at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
: at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
: at android.content.res.Resources.loadDrawable(Resources.java:1965)
: at android.content.res.Resources.getDrawable(Resources.java:660)
: at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
: at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:885)
: at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
: at android.content.res.Resources.loadDrawable(Resources.java:1950)
: at android.content.res.Resources.getDrawable(Resources.java:660)
: at android.view.View.setBackgroundResource(View.java:14468)
: at com.example.appthree.MainActivity.onCreate(MainActivity.java:15)
: at android.app.Activity.performCreate(Activity.java:5104)
: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
: at android.app.ActivityThread.access$600(ActivityThread.java:141)
: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
: at android.os.Handler.dispatchMessage(Handler.java:99)
: at android.os.Looper.loop(Looper.java:137)
: at android.app.ActivityThread.main(ActivityThread.java:5041)
: at java.lang.reflect.Method.invokeNative(Native Method)
: at java.lang.reflect.Method.invoke(Method.java:511)
: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
: at dalvik.system.NativeStart.main(Native Method)
: --- decoder->decode returned false
: Shutting down VM
: threadid=1: thread exiting with uncaught exception (group=0x40a71930)
: FATAL EXCEPTION: main
: java.lang.OutOfMemoryError
: at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
: at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
: at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
: at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
: at android.content.res.Resources.loadDrawable(Resources.java:1965)
: at android.content.res.Resources.getDrawable(Resources.java:660)
: at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
: at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:885)
: at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
: at android.content.res.Resources.loadDrawable(Resources.java:1950)
: at android.content.res.Resources.getDrawable(Resources.java:660)
: at android.view.View.setBackgroundResource(View.java:14468)
: at com.example.appthree.MainActivity.onCreate(MainActivity.java:15)
: at android.app.Activity.performCreate(Activity.java:5104)
: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
: at android.app.ActivityThread.access$600(ActivityThread.java:141)
: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
: at android.os.Handler.dispatchMessage(Handler.java:99)
: at android.os.Looper.loop(Looper.java:137)
: at android.app.ActivityThread.main(ActivityThread.java:5041)
: at java.lang.reflect.Method.invokeNative(Native Method)
: at java.lang.reflect.Method.invoke(Method.java:511)
: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
: at dalvik.system.NativeStart.main(Native Method)
: GC_CONCURRENT freed 30038K, 93% free 2539K/32768K, paused 73ms+97ms, total 472ms
最佳答案
正如 Martin 所说,Android 不支持 GIF。作为一种解决方法,Android 提供 Animation List/AnimationDrawable .您需要将 GIF 转换为单独的帧 [.png 文件]。我使用 GIMP 进行转换
这个 GIF 可以分解成帧:
将它们保存为 frame01.png、frame02.png 等,并创建一个动画列表 XML 文件,例如 progress_animation.xml
<animation-list android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/frame01" android:duration="50" />
<item android:drawable="@drawable/frame02" android:duration="50" />
<item android:drawable="@drawable/frame03" android:duration="50" />
....
<item android:drawable="@drawable/frameN" android:duration="50" />
要开始动画,您需要将图像转换为 AnimationDrawable,并在其上调用 start()
AnimationDrawable progressAnimation = (AnimationDrawable) yourImageView.getBackground();
progressAnimation.start();
关于android - 将动画 .GIF 设置为背景 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15842901/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植
我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r
Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路