如何在运行动画时保持活动顺畅

时间:2014-01-06 12:51:27

标签: android animation out-of-memory

我有一个应用程序,它在每个单独的活动中将图像显示为帧动画,但动画导致我的应用程序fc出现内存不足错误,尽管我的动画在那里工作就好了。我也在创建方法的每个活动上调用start(),我理解这是不赞成的,但是在给出内存不足错误之前,应用程序将经历5或6个这些动画活动,如果我崩溃,我发现它崩溃了什么按钮让它以前崩溃,它将奇迹般地工作。我猜它是因为它放弃了资源而不确定最好的方法来解决这个问题,任何人都可以提供任何见解吗?这是最后一个活动的代码和被调用时关闭的活动。

上次活动.Java

package com.martinsapp.socialstories;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

public class TeethActivityBed extends Activity {
AnimationDrawable teethBrushing;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bed_teeth);
    ImageView teeth_brushing_bed = (ImageView) findViewById(R.id.teethImageView);
    teeth_brushing_bed.setBackgroundResource(R.drawable.teeth_brushing_bed);
    teethBrushing = (AnimationDrawable) teeth_brushing_bed.getBackground();
    teethBrushing.stop();
    teethBrushing.start();

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.teeth_activity_bed, menu);
    return true;
}
public void nextAct(View view){
    Intent intent = new Intent(this, SleepingActivity.class);
    startActivity(intent);
}

}

last activity.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".TeethActivity"
android:background="@android:color/transparent">



<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:contentDescription="@string/teeth_bed_image"
    android:id="@+id/teethImageView"
    android:scaleType="centerCrop"
    android:layout_alignParentBottom="true"
    android:background="@drawable/teeth_brushing_bed" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/teeth_bed_text"
    android:textSize="23sp"
    android:textStyle="bold|italic"
    android:id="@+id/textView"
    android:gravity="center"
    android:background="@android:color/transparent" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/next"
    android:id="@+id/button"
    android:textSize="20sp"
    android:textStyle="bold|italic"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@android:color/transparent"
    android:layout_alignParentEnd="false"
    android:layout_alignParentStart="false"
    android:gravity="bottom"
    android:onClick="nextAct"/>

</RelativeLayout>

关闭.Java的活动

package com.martinsapp.socialstories;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;

public class SleepingActivity extends Activity {
AnimationDrawable sleep;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sleep);
    ImageView sleeping = (ImageView) findViewById(R.id.imageView);

    sleeping.setBackgroundResource(R.drawable.goodnight);
    sleep = (AnimationDrawable) sleeping.getBackground();
    sleep.stop();
    sleep.start();
}



public void nextImage2(View view){
    Intent intent = new Intent(this, SecondActivity.class);
    startActivity(intent);
}
}

关闭.xml的活动

<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/imageView"
    android:background="@drawable/goodnight"
    android:contentDescription="@string/wake_image"
    android:scaleType="centerCrop" />



<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:text="@string/sleepText"
    android:textSize="23sp"
    android:textStyle="bold|italic"
    android:background="@android:color/transparent"
    android:gravity="center" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/finished"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="nextImage2"
    android:id="@+id/nextbutton"/>
</RelativeLayout>

0 个答案:

没有答案