问题
I have developed an app that plays a music when the user presses on button. And I have also developed a splash screen along with the app and it did work with the app. However, I want to modify my app so that splash screen plays gif file instead of regular image. When I tried .gif file by creating new project it did worked. I used movie class for that. I tried intent for my splash screen so that it calls gif.java file. But it did not worked. It just showed me the distorted image of my gif file. And than It said "Your APP HAS CRASHED". My main java, splash screen and gif .java files and manifest.xml are the following.
Main Java
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(new MYGIFView (this));
setVolumeControlStream(AudioManager.STREAM_MUSIC);
findViewById(R.id.button_1).setOnClickListener(this);
findViewById(R.id.button_2).setOnClickListener(this);
findViewById(R.id.button_3).setOnClickListener(this);
}
public void onClick(View v) {
int resId=1;
// Release any resources from previous MediaPlayer
if (mp != null) {
mp.release();
}
switch (v.getId()) {
case R.id.button_1: resId = R.raw.button_1; break;
case R.id.button_2: resId = R.raw.button_2; break;
case R.id.button_3:
startActivity(new Intent(MainActivity.this,SecondActivity.class));
return;
}
// Create a new MediaPlayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
}
}
Splash Screen. java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MYGIFView.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
MYGIF.java
import java.io.InputStream;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.view.View;
class MYGIFView extends View{
Movie movie,movie1;
InputStream is=null,is1=null;
long moviestart;
public MYGIFView(Context context) {
super(context);
is=context.getResources().openRawResource(R.drawable.th_welcome);
movie=Movie.decodeStream(is);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
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);
canvas.scale(3.50f, 3.50f);
movie.draw(canvas,20,20);
this.invalidate();
}
}
Manifest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="11" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/app_name"/>
</application>
</manifest>
Logcat error
07-24 17:04:36.332: E/AndroidRuntime(580): FATAL EXCEPTION: main
07-24 17:04:36.332: E/AndroidRuntime(580): android.content.ActivityNotFoundException: Unable to find explicit activity class
{.MYGIFView}; have you declared this activity in your AndroidManifest.xml?
07-24 17:04:36.332: E/AndroidRuntime(580): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.app.Activity.startActivityForResult(Activity.java:3190)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.app.Activity.startActivity(Activity.java:3297)
07-24 17:04:36.332: E/AndroidRuntime(580): at com.$1.run(SplashScreen.java:29)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.os.Handler.handleCallback(Handler.java:605)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.os.Handler.dispatchMessage(Handler.java:92)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.os.Looper.loop(Looper.java:137)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-24 17:04:36.332: E/AndroidRuntime(580): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 17:04:36.332: E/AndroidRuntime(580): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 17:04:36.332: E/AndroidRuntime(580): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-24 17:04:36.332: E/AndroidRuntime(580): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-24 17:04:36.332: E/AndroidRuntime(580): at dalvik.system.NativeStart.main(Native Method)
Thanks in advance..
回答1:
Your code is starting a new "activity" with the MYGIFView class. It seem that it should be an activity, not a View.
Think just need to had
setContentView(new MYGIFView(this));
In the onCreate of the SplashActivity, before the handler declaration. and remove it in the onCreate of the MainActivity (seem it's a mistake here).
And then replace MyGIFView.class
with MainActivity.class
in the post delayed Handler :
Intent i = new Intent(SplashScreen.this, MainActivity.class);
And also need to remove the LAUNCHER
and MAIN
param of the manifest for the main activity:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Edited : erata SecondActivity -> MainActivity
来源:https://stackoverflow.com/questions/24939745/how-to-connect-splash-screen-with-gif-animation-file