1

我想快速更改屏幕上的一张图像。它在 htc 欲望 s 或 sumsung s1 上没有任何问题。但是在 s3 或大屏幕设备上安装应用程序时。图像变化缓慢,一段时间后强制关闭应用程序。错误是:“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;


        }
    }
4

2 回答 2

2

如果您的目标来自 Api 级别 10,您可以在 Application Tag 中使用 Android:largeHeap="true" 请参阅以下链接

于 2013-07-10T06:48:46.990 回答
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);

    }
于 2015-10-04T06:21:48.197 回答