如何在droid中制作拼图应用而又不会耗尽内存?

用户名

我注意到Google Play商店中的其他益智应用程序最多可以添加400个独立的可移动拼图

我一直在尝试学习如何至少拍摄代表拼图的图像,裁剪某些部分并掩盖拼图块设计留下的图像空间,以创建我的拼图块

我想为我的应用程序最大化20个片段,但到目前为止,根据我的android studio内存日志,一旦位图工厂完成创建一个拼图块,在创建20个片段以及所有其他片段之后,我正在消耗大约18​​mb的内存该应用程序的功能我正在使用400 + mb的内存,必须使用“ largeHeap = true”来防止内存不足,但是我已经接近超出那些更高的限制,以至于该应用程序非常缓慢且足够动画活动不可避免地会使应用程序崩溃

我很想知道其他Play Store拼图应用正在做什么,但我不是

任何输入都非常感谢

仅供参考,我正在为图像使用PNG24,并且测试图像上的尺寸为556x720

这是一个示例,如果我仅创建一个可动画显示的拼图图片

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);//Remove title bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//Hides notification bar
    this.setContentView(R.layout.activity_main);//set content view AFTER ABOVE sequence (to avoid crash)

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    mainDisplay = getWindowManager().getDefaultDisplay();
    mainLayout = (ViewGroup) findViewById(R.id.id_layout);

    DisplayMetrics m = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(m);
    int windowHeight = m.heightPixels;
    int windowWidth = m.widthPixels;

    offsetX = (windowWidth / 1440.0f);
    offsetY = (windowHeight / 2560.0f);

    ourContext = this;

    xpos = new float[2];
    ypos = new float[2];

    iv_PuzzleOne();

    bkgdbm = BitmapFactory.decodeResource(getResources(), R.drawable.puzzleimage); 

    paintObject = new Paint();
    paintObject.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR)); 

    bm_PuzzleOne();

    thisTimerTask = new ThisClass();
    thisTimer = new Timer();
    thisTimer.scheduleAtFixedRate(thisTimerTask, 16, 16);

    touchpad = new ImageButton(this);
    SetPos(0, 0, 1440, 2560);
    touchpad.setLayoutParams(layoutPositioner);
    touchpad.getBackground().setAlpha(1);
    mainLayout.addView(touchpad);

    touchpad.setOnTouchListener(new View.OnTouchListener() {

        //@SuppressLint("NewApi")
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                xpos[0] = event.getX(); //storing my finger's position coordinates when I first touch the screen into the 1st element
                ypos[0] = event.getY();

                if ((event.getX() > imgx1) && (event.getX() < imgx1 + imagewidth1)
                        && (event.getY() > imgy1) && (event.getY() < imgy1 + imageheight1)) {
                    touching1Puzzle = true;
                    img1.bringToFront();
                }

            }

            if (event.getAction() == MotionEvent.ACTION_MOVE) {

                xpos[1] = event.getX(); //add my finger's new current coordinates into the 2nd element
                ypos[1] = event.getY();

                if (touching1Puzzle) {
                    adjustImg();
                }
            }

            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (touching1Puzzle) {
                    touching1Puzzle = false;
                }

            }

            return false;
        }

    });
}


void bm_PuzzleOne()
{
    //start of 1st puzzle

    foregdimg1 = BitmapFactory.decodeResource(getResources(), R.drawable.puzzlepieceprac);//puzzle cut out (42.48MB) +6.15
    mutableforegdimg1 = foregdimg1.copy(Bitmap.Config.ARGB_8888, true); //(48.32MB) +5.84

    compositeImage1 = Bitmap.createBitmap(mutableforegdimg1);//cuts out foreground info into bkgdimage (54.43MB) +6.11

    imgCanvas1 = new Canvas(compositeImage1); //canvas references puzzle cut out image (54.43MB) +0
    imgCanvas1.drawBitmap(croppedBmp, null, new Rect(0, 0, 1500, 2000), paintObject);//places puzzle image on canvas (54.43MB) +0

    img1.setImageBitmap(compositeImage1);

}


void iv_PuzzleOne()
{
    img1 = new ImageView(ourContext);
    SetPos(imgx1, imgy1, imagewidth1, imageheight1);
    //bkgdimg.setImageResource(R.drawable.c);
    //img1.setBackgroundColor(0xffF07305); //Orange
    img1.setLayoutParams(layoutPositioner);
    mainLayout.addView(img1);

}

void adjustImg()
{
    if (touching1Puzzle)
    {
        if (xpos[1] > xpos[0]) //if the image had slid to the right
        {
            xPositionDifference = xpos[1] - xpos[0]; // find the difference in coordinate value between where my finger was and where it currently is
            imgx1 += xPositionDifference; //add that difference to the current image position ...

            xpos[0] += xPositionDifference; // ... store that difference for the next shift in finger postion

        } else if (xpos[1] < xpos[0]) //if the image had slid to the left
        {
            xPositionDifference = xpos[0] - xpos[1]; // find the difference in coordinate value between where my finger was and where it currently is
            imgx1 -= xPositionDifference; //subtract that difference to the current image position ...

            xpos[0] -= xPositionDifference; // ... store that difference for the next shift in finger postion
        }

        if (ypos[1] > ypos[0]) //if the image had slid to the right
        {
            yPositionDifference = ypos[1] - ypos[0]; // find the difference in coordinate value between where my finger was and where it currently is
            imgy1 += yPositionDifference; //add that difference to the current image position ...

            ypos[0] += yPositionDifference; // ... store that difference for the next shift in finger postion

        } else if (ypos[1] < ypos[0]) //if the image had slid to the left
        {
            yPositionDifference = ypos[0] - ypos[1]; // find the difference in coordinate value between where my finger was and where it currently is
            imgy1 -= yPositionDifference; //subtract that difference to the current image position ...

            ypos[0] -= yPositionDifference; // ... store that difference for the next shift in finger postion

        }
    }

}

class ThisClass extends TimerTask {

    @Override
    public void run() {
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if(touching1Puzzle)
                {SetPos(imgx1, imgy1, imagewidth1, imageheight1);
                    img1.setLayoutParams(layoutPositioner);}

            }
        });
    }
}

public void SetPos(float x, float y, float width, float height) {
    layoutPositioner = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutPositioner.topMargin = (int) (offsetY * y);
    layoutPositioner.leftMargin = (int) (offsetX * x);
    layoutPositioner.width = (int) (width * offsetX);
    layoutPositioner.height = (int) (height * offsetY);
}

}

这是加载5张图像时的样子

http://s15.postimg.org/ymqspk77v/Screenshot_2016_02_29_07_19_26.png

马赫·阿伯特拉(Maher Abuthraa)

只需使用向量而不是图像即可。您将为运行应用程序和存储节省大量内存。它的支持很好。您可以使用SVG资源

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在应用程序中保持5GB的持续内存而又不会由于GC而导致性能下降?

如何在一次malloc调用中为数组和结构分配内存,而又不破坏严格的别名?

如何在悬停时增加边框宽度而又不移走CSS中的div位置?

如何在T SQL中对XML进行编码而又不增加XML开销

如何在R中将很大的OpenStreetMap文件分成较小的文件而又不会耗尽内存?

如何在Haskell中旋转OpenGL图形而又不重新评估图形对象?

如何在Anaconda中卸载pip ipykernel而又不卸载ipykernel?

如何调整<tr>的大小而又不会失去边界的底部

如何在输入更改上提交表单而又不会卡住输入值?

如何在<div>中调整背景图像的亮度而又不影响前景项目?

如何在Django中从项目级别包导入而又不与同名的应用程序级别模块冲突?

如何在Swift 4中使用Swift搜索到先前的屏幕而又不丢失数据

如何在dplyr中对许多列进行突变而又不重复多次?

如何在FlatList中更改单个图标的颜色而又不同时更改它们的颜色

如何在ImageView中从相机放映中获取图像而又不损失其在Android中的质量?

如何在角度7中从阵列拼接元素而又不干扰主阵列

如何在Wordpress中创建动态页面而又不会遇到缓存问题?

如何满足shellcheck而又不会导致脚本失败?

如何在C ++中返回对象而又不会超出范围?

如何在Windows中通过命令行退出Firefox而又不强制退出?

如何轻松地访问喜爱的应用程序而又不会过多地启动程序?

如何在Chrome打包的应用中允许此代码而又不违反CSP?

如何在非移动应用程序中从一个视图切换到另一个视图而又不破坏旧视图

如何在脚本中减少使用而又不获取ESC转义字符?

如何从shell脚本中打开.jar文件而又不会失去焦点?

如何在列中输入一组值,而又不更改预先存在的条目?

如何调整图像的大小而又不会错位?

我如何在Visual Studio中的模拟器上测试应用程序而又不将应用程序的项目包括在解决方案中?

如何在绘制时创建变量“ DayOfWeek”而又不会丢失一天?