【问题标题】:android - out of memory in large screen - change image fastandroid - 大屏幕内存不足 - 快速更改图像
【发布时间】:2013-07-10 06:01:46
【问题描述】:

我想快速更改屏幕上的一张图像。它在 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;


        }
    }

【问题讨论】:

  • 您需要减少应用程序的内存使用量。您应该始终使用少于 16mb 的空间。
  • 4 张图片中的每一张只有 38 KB 大小
  • 您可以调整图片大小、重用对象等blog.gorges.us/2010/07/…
  • 您还可以建议在执行内存密集型操作之前运行 GC。 System.gc();
  • 你的 TargetSdkVersion 和 MinSdkVersion 是什么

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


【解决方案1】:

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

【讨论】:

    【解决方案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);
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多