android - 大屏幕内存不足 - 快速更改图像

时间:2013-07-10 06:01:46

标签: android animation bitmap imageview out-of-memory

我想快速更改屏幕中的一张图片。它对htc欲望s或sumsung s1有任何问题。但是当在s3或大屏幕设备上安装app时。图像变化缓慢,一段时间后关闭应用程序。错误是:" 6354208字节分配的内存不足。"我改变了我的图像分辨率,它工作得更好,但错误是修复,有时会发生。在s3中使用的内存是35 mb !!!

        float accelationSquareRoot = (x * x + y * y + z * z)
            / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
    long actualTime = System.currentTimeMillis();
    if (accelationSquareRoot >= 1.5) //
    {
        if (actualTime - lastUpdate < 30) {

            return;
        }
        Random r=new Random();
        randomWord+=r.nextInt(3);
        randomWord%=4;
        lastUpdate = actualTime;

        isStarted=true;
//          ((BitmapDrawable)shakedButton.getDrawable()).getBitmap().recycle();
        switch (randomWord) {
        case 0:
            shakedButton.setImageResource(R.drawable.alef);
            break;
        case 1:
            shakedButton.setImageResource(R.drawable.b);
            break;
        case 2:
            shakedButton.setImageResource(R.drawable.jim);
            break;
        case 3:
            shakedButton.setImageResource(R.drawable.dal);
            break;


        }
    }

2 个答案:

答案 0 :(得分:2)

如果您的目标是从Api等级10开始,您可以在应用标记中使用Android:largeHeap =“true”,请参阅以下link

答案 1 :(得分:2)

我使用画布,结果非常好

我写这个课:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;

public class MyView extends View {

private int imageIndex;

private int dstWidth;
private int dstHeight;
private int x, y;

public MyView(Context context) {
    super(context);

    imageIndex = 0;

    dstWidth = 265;
    dstHeight = 240;

    x = 0;
    y = 0;
}

@Override
protected void onDraw(Canvas canvas) {

    Bitmap harf;

    if( imageIndex == 0 )
        harf = BitmapFactory.decodeResource( getResources(), R.drawable.alef);
    else if( imageIndex == 1 )
        harf = BitmapFactory.decodeResource( getResources(), R.drawable.b );
    else if( imageIndex == 2 )
        harf = BitmapFactory.decodeResource( getResources(), R.drawable.jim );
    else
        harf = BitmapFactory.decodeResource( getResources(), R.drawable.dal );

    Bitmap mBitmap = Bitmap.createScaledBitmap( harf, dstWidth, dstHeight, true);

    canvas.drawBitmap(mBitmap, x, y, null);     
}

public void setIndexAndDraw( int index )
{
    imageIndex = index;
    invalidate();
}
}

我在onCreate方法

中以这种方式初始化了shakedView
    MyView shakedView;
    lp.addRule(RelativeLayout.CENTER_VERTICAL);
    lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
    lp.leftMargin=70;
    shakedView=new MyView(this);
    rLayout.addView(shakedView,lp);

我问题中的代码以这种方式改变

if (accelationSquareRoot >= 1.5) //
    {
        if (actualTime - lastUpdate < 30) {

            return;
        }
        Random r=new Random();
        randomWord+=r.nextInt(3);
        randomWord%=4;
        lastUpdate = actualTime;
        Toast.LENGTH_SHORT).show();
        isStarted=true;
        shakedView.setIndexAndDraw(randomWord);

    }