不使用位图帆布发生在Android的更多的内存 [英] Canvas without using bitmap takes more memory in Android

查看:124
本文介绍了不使用位图帆布发生在Android的更多的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的地方使用Canvas和路径画一个树形结构图,它看起来像下面给出的之一。我画树的方式是一个坏的编码习惯,我非常清楚的,但我不得不遵循这种风格,由于缺乏更好的办法。 (我没有尝试在OpenGL,我有点不敢尝试)。这样,发生在树中的步骤如下:


  1. 树被绘制在不使用它的任何文本。

  2. 当用户添加到树,树叶颜色得到改变和文本添加到它。

  3. 继此,这些叶子应该有图像,而不是可以通过使用 drawBitmap 这显然会导致我OOM(内存不足)在实现纯文本未来。

因此​​,这些都是这种情况发生,截至目前的步骤,这需要我至少3-4秒时的着色叶数较少加载屏幕(意味着有较少的用户),如果有更多的用户则加载时间超过5秒以上,有时,它显示的应用程序没有响应由于更多的内存消耗。

我有,因为这显示屏幕给用户占用了所有的内存和强制我的应用程序关闭如何进一步开展不知道。

所以,我完全停留在这一点上无法进一步前进,我已经投入了大量时间建立这棵树,所以我希望,应该有办法清除内存并重新建立在画布上。

我学到了什么至今:
Android不容许再次绘制页面的一部分,所以整个画布必须再次再生的加载页面时。

P.S:
如果有一种方法使用现有的code,克服了存储问题,则其高度AP preciated造成浪费的时间已经太多了,如果在所有有没有其他办法,只能彻底摧毁这一点,从开始从无到有那么我希望这样能给我我想要的东西。

树:

code:

  @覆盖
    公共无效的onDraw(帆布canv){        // gggggggggggg
        对于(CircleArea圈:mCircles){                如果(dbHelper.getRelationTypes()大小()方式> 0)
                {
                    。//System.out.println(\"dbHelper.getRelationTypes()大小()大于0);
                    //System.out.println(\"Inside配偶或父亲母亲);
                    CircleCanvas.spouse_added = TRUE;
                    //情侣
                    如果(circle.radius == XHDPI_COUPLE_RADIUS_LIMIT)
                    {
                        //的System.out.println(如果(circle.radius == 70));
                        如果(circle.getCenterX()== my_x&放大器;&放大器; circle.getCenterY()== my_y)
                        {
                            dbHelper.insertXYOfRelation(自我,circle.getCenterX(),circle.getCenterY());                            如果(DashboardActivity.profile_image_from_local!= NULL)
                            {
                                位图大小= Bitmap.createScaledBitmap(DashboardActivity.profile_image_from_local,200,200,真);
                                位图的结果= NULL;
                                尝试{
                                    结果= Bitmap.createBitmap(200,200,Bitmap.Config.ARGB_8888);
                                    帆布帆布=新的Canvas(结果);
                                    INT颜色= 0xff424242;
                                    涂料粉刷=新的油漆();
                                    矩形矩形=新的Rect(0,0,200,200);
                                    // RectF rectF =新RectF(RECT);
                                    paint.setAntiAlias​​(真);
                                    canvas.drawARGB(0,0,0,0);
                                    paint.setColor(颜色);
                                    canvas.drawCircle(XHDPI_COUPLE_RADIUS_LIMIT,XHDPI_COUPLE_RADIUS_LIMIT,XHDPI_COUPLE_RADIUS_LIMIT,油漆);
                                    //canvas.drawOval(rectF,油漆);
                                    paint.setXfermode(新PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
                                    canvas.drawBitmap(调整大小,矩形,矩形,油漆);                                }赶上(NullPointerException异常五){
                                }赶上(OutOfMemoryError异常O){
                                }                                的System.out.println(我内心);
                                canv.drawBitmap(结果,circle.getCenterX() - result.getWidth()/ 3,circle.getCenterY() - result.getHeight()/ 3,空);
                            }                            否则,如果(DashboardActivity.profile_image!= NULL)
                            {
                                位图大小= Bitmap.createScaledBitmap(DashboardActivity.profile_image,200,200,真);
                                位图的结果= NULL;
                                尝试{
                                    结果= Bitmap.createBitmap(200,200,Bitmap.Config.ARGB_8888);
                                    帆布帆布=新的Canvas(结果);
                                    INT颜色= 0xff424242;
                                    涂料粉刷=新的油漆();
                                    矩形矩形=新的Rect(0,0,200,200);
                                    // RectF rectF =新RectF(RECT);
                                    paint.setAntiAlias​​(真);
                                    canvas.drawARGB(0,0,0,0);
                                    paint.setColor(颜色);
                                    canvas.drawCircle(XHDPI_COUPLE_RADIUS_LIMIT,XHDPI_COUPLE_RADIUS_LIMIT,XHDPI_COUPLE_RADIUS_LIMIT,油漆);
                                    //canvas.drawOval(rectF,油漆);
                                    paint.setXfermode(新PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
                                    canvas.drawBitmap(调整大小,矩形,矩形,油漆);                                }赶上(NullPointerException异常五){
                                }赶上(OutOfMemoryError异常O){
                                }                                的System.out.println(我内心);
                                canv.drawBitmap(结果,circle.getCenterX() - result.getWidth()/ 3,circle.getCenterY() - result.getHeight()/ 3,空);
                            }
                            其他
                            {
                                canv.drawCircle(circle.getCenterX(),circle.getCenterY(),XHDPI_COUPLE_RADIUS_LIMIT,mMarriedPaint);
                                canv.drawText(dbHelper.getNameOfRelation(自我),circle.getCenterX() - 34,circle.getCenterY(),mXHDPICoupleTextPaint);
                                Log.e(OnDraw中自我,自我的OnDraw);
                            }
                        }
                        其他
                        {
                            如果(dbHelper.getWhatRelationTypes(配偶))
                            {
                               //的System.out.println(内部配偶);
                                dbHelper.insertXYOfRelation(配偶,circle.getCenterX(),circle.getCenterY());
                                Log.e(Spouseeeeeeee,+ circle.getCenterX()++ circle.getCenterY()+ dbHelper.insertXYOfRelation(配偶,circle.getCenterX(),circle.getCenterY()));
                                canv.drawCircle(circle.getCenterX(),circle.getCenterY(),XHDPI_COUPLE_RADIUS_LIMIT,mMarriedPaint);
                                canv.drawText(dbHelper.getNameOfRelation(配偶),circle.getCenterX() - 34,circle.getCenterY(),mXHDPICoupleTextPaint);
                            }
                            其他
                            {
                                canv.drawCircle(circle.getCenterX(),circle.getCenterY(),XHDPI_COUPLE_RADIUS_LIMIT,mFilledPaint);
                                Log.e(OnDraw的配偶,配偶的OnDraw);
                            }
                        }
                    }                    //父母
                    如果(circle.radius == XHDPI_PARENTS_RADIUS_LIMIT)
                    {                       //的System.out.println(如果(circle.radius == 55));
                        如果(circle.getCenterX()== father_x&放大器;&放大器; circle.getCenterY()== father_y)
                        {                            如果(dbHelper.getWhatRelationTypes(父亲))
                            {
                                dbHelper.insertXYOfRelation(父亲,circle.getCenterX(),circle.getCenterY());
                              //的System.out.println(内部父+X:+ dbHelper.getXOfRelation(父亲)+Y:+ dbHelper.getYOfRelation(父亲));
                                    canv.drawCircle(dbHelper.getXOfRelation(父亲),dbHelper.getYOfRelation(父亲),XHDPI_PARENTS_RADIUS_LIMIT,mMarriedPaint);
                                    canv.drawText(dbHelper.getNameOfRelation(父亲),circle.getCenterX() - 22,circle.getCenterY(),mXHDPIParentsTextPaint);
                            }
                            其他
                            {
                                canv.drawCircle(circle.getCenterX(),circle.getCenterY(),XHDPI_PARENTS_RADIUS_LIMIT,mFilledPaint);
                                Log.e(OnDraw中的父亲,爸爸的OnDraw);
                            }
                        }否则如果(circle.getCenterX()== mother_x&放大器;&放大器; circle.getCenterY()== mother_y)
                        {
                            如果(dbHelper.getWhatRelationTypes(母亲))
                            {
                               //的System.out.println(内部母亲);
                                dbHelper.insertXYOfRelation(母亲,circle.getCenterX(),circle.getCenterY());
                                canv.drawCircle(dbHelper.getXOfRelation(母亲),dbHelper.getYOfRelation(母亲),XHDPI_PARENTS_RADIUS_LIMIT,mMarriedPaint);
                                canv.drawText(dbHelper.getNameOfRelation(母亲),circle.getCenterX() - 22,circle.getCenterY(),mXHDPIParentsTextPaint);
                            }
                            其他
                            {
                                canv.drawCircle(circle.getCenterX(),circle.getCenterY(),XHDPI_PARENTS_RADIUS_LIMIT,mFilledPaint);
                                Log.e(OnDraw中的母亲,母亲的OnDraw);
                            }
                        }否则如果(circle.getCenterX()== spouse_father_x&放大器;&放大器; circle.getCenterY()== spouse_father_y)
                        {
                            如果(dbHelper.getWhatRelationTypes(配偶父亲))
                            {
                               //的System.out.println(内部配偶父亲);
                                dbHelper.insertXYOfRelation(配偶父,circle.getCenterX(),circle.getCenterY());
                                canv.drawCircle(dbHelper.getXOfRelation(配偶父亲),dbHelper.getYOfRelation(配偶父亲),XHDPI_PARENTS_RADIUS_LIMIT,mMarriedPaint);
                                canv.drawText(dbHelper.getNameOfRelation(配偶父亲),circle.getCenterX() - 22,circle.getCenterY(),mXHDPIParentsTextPaint);
                            }
                            其他
                            {
                                canv.drawCircle(circle.getCenterX(),circle.getCenterY(),XHDPI_PARENTS_RADIUS_LIMIT,mFilledPaint);
                                Log.e(OnDraw的配偶的父亲,配偶的OnDraw父);
                            }
                        }否则如果(circle.getCenterX()== spouse_mother_x&放大器;&放大器; circle.getCenterY()== spouse_mother_y)
                        {
                            如果(dbHelper.getWhatRelationTypes(配偶母亲))
                            {
                                dbHelper.insertXYOfRelation(配偶母亲,circle.getCenterX(),circle.getCenterY());
                                canv.drawCircle(dbHelper.getXOfRelation(配偶母亲),dbHelper.getYOfRelation(配偶母亲),XHDPI_PARENTS_RADIUS_LIMIT,mMarriedPaint);
                                canv.drawText(dbHelper.getNameOfRelation(配偶母亲),circle.getCenterX() - 22,circle.getCenterY(),mXHDPIParentsTextPaint);
                            }
                            其他
                            {
                                canv.drawCircle(circle.getCenterX(),circle.getCenterY(),XHDPI_PARENTS_RADIUS_LIMIT,mFilledPaint);
                                Log.e(OnDraw的配偶的母亲,配偶的OnDraw母亲);
                            }
                        }                    }                   //儿童
                    如果(circle.radius == XHDPI_CHILD_RADIUS_LIMIT){                        如果(dbHelper.FetchChildFriendCountFromUserTable(子),大小()方式> 0){                            child_x.clear();
                            child_y.clear();                            child_x.add(c1_x);
                            child_x.add(c2_x);
                            child_x.add(c3_x);
                            child_x.add(c4_x);
                            child_x.add(c5_x);                            child_y.add(c1_y);
                            child_y.add(c2_y);
                            child_y.add(c3_y);
                            child_y.add(c4_y);
                            child_y.add(c5_y);
                            对于(INT R = 0;为r。dbHelper.FetchChildFriendCountFromUserTable(子),大小(); R ++){
                                如果(dbHelper.FetchChildFriendCountFromUserTable(子)。得到(R).equalsIgnoreCase(Child1))
                                {                                    new_c_x = c1_x;
                                    new_c_y = c1_y;                                    dbHelper.insertChildOriginalXY(Child1,new_c_x,new_c_y);
                                    dbHelper.insertXYOfRelation(Child1,new_c_x,new_c_y);                                    child_x.remove(新的整数(c1_x));
                                    child_y.remove(新的整数(c1_y));                                    canv.drawCircle(dbHelper.getXOfRelation(Child1),dbHelper.getYOfRelation(Child1),XHDPI_DRAWN_CHILD_RADIUS_LIMIT,mChildFilledPaint);
                                    canv.drawText(dbHelper.getNameOfRelation(Child1),dbHelper.getXOfRelation(Child1) - 14,dbHelper.getYOfRelation(Child1),mXHDPIChildTextPaint);                                }否则如果(dbHelper.FetchChildFriendCountFromUserTable(子)。得到(R).equalsIgnoreCase(CHILD2))
                                {                                    new_c_x = c2_x;
                                    new_c_y = c2_y;                                    child_x.remove(新的整数(c2_x));
                                    child_y.remove(新的整数(c2_y));                                    dbHelper.insertChildOriginalXY(CHILD2,new_c_x,new_c_y);
                                    dbHelper.insertXYOfRelation(CHILD2,new_c_x,new_c_y);
                                    canv.drawCircle(dbHelper.getXOfRelation(CHILD2),dbHelper.getYOfRelation(CHILD2),XHDPI_DRAWN_CHILD_RADIUS_LIMIT,mChildFilledPaint);
                                    canv.drawText(dbHelper.getNameOfRelation(CHILD2),dbHelper.getXOfRelation(CHILD2) - 14,dbHelper.getYOfRelation(CHILD2),mXHDPIChildTextPaint);
                                }否则如果(dbHelper.FetchChildFriendCountFromUserTable(子)。得到(R).equalsIgnoreCase(Child3))
                                {                                    new_c_x = c3_x;
                                    new_c_y = c3_y;
                                    child_x.remove(新的整数(c3_x));
                                    child_y.remove(新的整数(c3_y));
                                    dbHelper.insertChildOriginalXY(Child3,new_c_x,new_c_y);
                                    dbHelper.insertXYOfRelation(Child3,new_c_x,new_c_y);
                                    canv.drawCircle(dbHelper.getXOfRelation(Child3),dbHelper.getYOfRelation(Child3),XHDPI_DRAWN_CHILD_RADIUS_LIMIT,mChildFilledPaint);
                                    canv.drawText(dbHelper.getNameOfRelation(Child3),dbHelper.getXOfRelation(Child3) - 14,dbHelper.getYOfRelation(Child3),mXHDPIChildTextPaint);
                                }否则如果(dbHelper.FetchChildFriendCountFromUserTable(子)。得到(R).equalsIgnoreCase(Child4))
                                {                                    new_c_x = c4_x;
                                    new_c_y = c4_y;
                                    child_x.remove(新的整数(c4_x));
                                    child_y.remove(新的整数(c4_y));
                                    dbHelper.insertChildOriginalXY(Child4,new_c_x,new_c_y);
                                    dbHelper.insertXYOfRelation(Child4,new_c_x,new_c_y);
                                    canv.drawCircle(dbHelper.getXOfRelation(Child4),dbHelper.getYOfRelation(Child4),XHDPI_DRAWN_CHILD_RADIUS_LIMIT,mChildFilledPaint);
                                    canv.drawText(dbHelper.getNameOfRelation(Child4),dbHelper.getXOfRelation(Child4) - 14,dbHelper.getYOfRelation(Child4),mXHDPIChildTextPaint);
                                }否则如果(dbHelper.FetchChildFriendCountFromUserTable(子)。得到(R).equalsIgnoreCase(Child5))
                                {                                    new_c_x = c5_x;
                                    new_c_y = c5_y;
                                    child_x.remove(新的整数(c5_x));
                                    child_y.remove(新的整数(c5_y));                                    dbHelper.insertChildOriginalXY(Child5,new_c_x,new_c_y);
                                    dbHelper.insertXYOfRelation(Child5,new_c_x,new_c_y);
                                    canv.drawCircle(dbHelper.getXOfRelation(Child5),dbHelper.getYOfRelation(Child5),XHDPI_DRAWN_CHILD_RADIUS_LIMIT,mChildFilledPaint);
                                    canv.drawText(dbHelper.getNameOfRelation(Child5),dbHelper.getXOfRelation(Child5) - 14,dbHelper.getYOfRelation(Child5),mXHDPIChildTextPaint);
                                }
                            }
                            如果(child_x.size()!= 0)
                            {
                                的for(int i = 0; I< child_x.size();我++)
                                {
                                    canv.drawCircle(child_x.get(ⅰ),child_y.get(i)中,XHDPI_CHILD_RADIUS_LIMIT,mCirclePaint);
                                }                            }
                        }其他{                            canv.drawCircle(circle.getCenterX(),circle.getCenterY(),XHDPI_CHILD_RADIUS_LIMIT,mCirclePaint);                        }
                    }                    //友
                    如果(circle.radius == FRIENDS_RADIUS_LIMIT)
                    {
                        // tujomila
                        如果(dbHelper.FetchChildFriendCountFromUserTable(朋友),大小()方式> 0)
                        {                           // Log.e(,好友列表大小为+ dbHelper.getFriendRelationTypes()大小());
                           // Log.e(,circlepointers的大小是+ circle_pointers);                            对于(INT Q = 1; Q< = dbHelper.FetchChildFriendCountFromUserTable(朋友),大小();问++)
                            {
                                朋友//Log.e(\"\",\"The X+ Q +就是+ dbHelper.getFriendCorrespondingXValue(Q,结婚));
                                朋友//Log.e(\"\",\"The Y+ Q +就是+ dbHelper.getFriendCorrespondingYValue(Q,结婚));                                canv.drawCircle(dbHelper.getFriendCorrespondingXValue(q,\"Married\"),dbHelper.getFriendCorrespondingYValue(q,\"Married\"),FRIENDS_RADIUS_LIMIT,mChildFilledPaint);
                                canv.drawText(dbHelper.getNameOfRelation(好友+ Q),dbHelper.getXOfRelation(好友+ Q) - 17,dbHelper.getYOfRelation(好友+ Q),mXHDPIFriendTextPaint);
                                dbHelper.insertXYOfRelation(好友+ Q,dbHelper.getFriendCorrespondingXValue(Q,嫁),dbHelper.getFriendCorrespondingYValue(Q,结婚));
                                friend_x.add(dbHelper.getFriendCorrespondingXValue(Q,结婚));
                                friend_y.add(dbHelper.getFriendCorrespondingYValue(Q,结婚));                            }
                            对于(中间体W = dbHelper.FetchChildFriendCountFromUserTable(朋友),大小()+ 1; W< = 34; W-- ++){                               // Log.e(指针+ W,为W x值是+ dbHelper.getFriendCorrespondingXValue(W已婚)+为W Y值是+ dbHelper.getFriendCorrespondingYValue(W,已婚));
                                canv.drawCircle(dbHelper.getFriendCorrespondingXValue(w,\"Married\"),dbHelper.getFriendCorrespondingYValue(w,\"Married\"), FRIENDS_RADIUS_LIMIT,mCirclePaint);
                            }                        }
                        其他
                        {
                            canv.drawCircle(circle.getCenterX(),circle.getCenterY(),FRIENDS_RADIUS_LIMIT,mCirclePaint);
                        }
                    }
                }
                其他
                {                    CircleCanvas.member_type的//System.out.println(\"Else = NULL)!;
                    如果(circle.radius == FRIENDS_RADIUS_LIMIT){
                        canv.drawCircle(circle.getCenterX(),circle.getCenterY(),FRIENDS_RADIUS_LIMIT,mCirclePaint);
                    }否则如果(circle.radius == XHDPI_CHILD_RADIUS_LIMIT){
                    canv.drawCircle(circle.getCenterX(),circle.getCenterY(),XHDPI_CHILD_RADIUS_LIMIT,mCirclePaint);
                    }否则如果(circle.radius == XHDPI_PARENTS_RADIUS_LIMIT){
                        canv.drawCircle(circle.getCenterX(),circle.getCenterY(),XHDPI_PARENTS_RADIUS_LIMIT,mFilledPaint);
                    }其他{
                        canv.drawCircle(circle.getCenterX(),circle.getCenterY(),XHDPI_COUPLE_RADIUS_LIMIT,mMarriedPaint);                    }
                }
            }        }    }


解决方案

我看到你的code。所以,你使用的是查看来绘制所有实心圆。在的onDraw 方法超负荷计算和对象实例化。我建议你​​从的onDraw删除尽可能多的,你可以方法。如果计算可以查看施工过程中进行,然后做它在构造函数中,而不是在的onDraw 。你需要保持的onDraw 尽可能轻。

现在,这是一个好的设计?在你的情况下,查看是不是一个好主意,因为一个变化将重绘整个视图。相反,我建议你创建一个自定义的的ViewGroup ,并拥有所有这些实心圆作为孩子的。这样,当在颜色或文本单圈的变化,你需要重新绘制整个屏幕。

I have a view where I draw a tree shaped structure using Canvas and Path and it looks like the one given below. The way I draw the tree is a bad coding habit and I am well aware of it but I had to follow this style due to the lack of better way. (I did not try it in OpenGL, I'm a bit afraid to try that). So the steps that takes place in tree are as follows :

  1. Tree is drawn without using any text in it.
  2. As the users are added to the tree, the leaves color gets changed and text is added to it.
  3. Further to this, these leaves should have images rather than plain text which can be achieved by using drawBitmap which will obviously lead me to OOM (Out Of Memory) in the future.

So these are the steps that happens as of now, this takes me at least 3-4 seconds to load the screen when the colored leaf count is less (meaning that there are less users), if there are more users then the loading time exceeds more than 5 seconds and at times, it shows "App not responding" due to more memory consumption.

I have no idea on how to proceed further because showing this screen to the user takes up all the memory and forces my app to close.

So I am completely stuck at this point unable to proceed further and I have invested a great deal of time building this tree so I hope that there should be way to clear the memory and re-build the canvas.

What have I learned so far : Android does not allow to draw a part of the page again, so the whole canvas has to be regenerated again when the page is loaded.

P.S: If there is a way to overcome the memory issue using the existing code then its highly appreciated cause the time wasted has been too much, if at all there is no other way but to destroy this completely and start from scratch then I hope that way will give me exactly what I want.

Tree :

CODE :

 @Override
    public void onDraw(Canvas canv) {



        //gggggggggggg
        for (CircleArea circle : mCircles) {



                if(dbHelper.getRelationTypes().size() > 0)
                {
                    //System.out.println("dbHelper.getRelationTypes().size() > 0");
                    //System.out.println("Inside the Spouse or Father and Mother");
                    CircleCanvas.spouse_added = true;
                    //Couples
                    if(circle.radius==XHDPI_COUPLE_RADIUS_LIMIT)
                    {
                        // System.out.println("if(circle.radius==70)");
                        if(circle.getCenterX() == my_x && circle.getCenterY() == my_y)
                        {
                            dbHelper.insertXYOfRelation("Self", circle.getCenterX(), circle.getCenterY());

                            if(DashboardActivity.profile_image_from_local!=null)
                            {
                                Bitmap resized = Bitmap.createScaledBitmap(DashboardActivity.profile_image_from_local, 200, 200, true);
                                Bitmap result = null;
                                try {
                                    result = Bitmap.createBitmap(200,200, Bitmap.Config.ARGB_8888);
                                    Canvas canvas = new Canvas(result);
                                    int color = 0xff424242;
                                    Paint paint = new Paint();
                                    Rect rect = new Rect(0, 0, 200, 200);
                                    //RectF rectF = new RectF(rect);
                                    paint.setAntiAlias(true);
                                    canvas.drawARGB(0, 0, 0, 0);
                                    paint.setColor(color);
                                    canvas.drawCircle(XHDPI_COUPLE_RADIUS_LIMIT, XHDPI_COUPLE_RADIUS_LIMIT, XHDPI_COUPLE_RADIUS_LIMIT, paint);
                                    //canvas.drawOval(rectF, paint);
                                    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
                                    canvas.drawBitmap(resized, rect, rect, paint);

                                } catch (NullPointerException e) {
                                } catch (OutOfMemoryError o) {
                                }

                                System.out.println("Inside Me");
                                canv.drawBitmap(result, circle.getCenterX() - result.getWidth() / 3, circle.getCenterY() - result.getHeight() / 3, null);
                            }

                            else  if(DashboardActivity.profile_image!=null)
                            {
                                Bitmap resized = Bitmap.createScaledBitmap(DashboardActivity.profile_image, 200, 200, true);
                                Bitmap result = null;
                                try {
                                    result = Bitmap.createBitmap(200,200, Bitmap.Config.ARGB_8888);
                                    Canvas canvas = new Canvas(result);
                                    int color = 0xff424242;
                                    Paint paint = new Paint();
                                    Rect rect = new Rect(0, 0, 200, 200);
                                    //RectF rectF = new RectF(rect);
                                    paint.setAntiAlias(true);
                                    canvas.drawARGB(0, 0, 0, 0);
                                    paint.setColor(color);
                                    canvas.drawCircle(XHDPI_COUPLE_RADIUS_LIMIT, XHDPI_COUPLE_RADIUS_LIMIT, XHDPI_COUPLE_RADIUS_LIMIT, paint);
                                    //canvas.drawOval(rectF, paint);
                                    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
                                    canvas.drawBitmap(resized, rect, rect, paint);

                                } catch (NullPointerException e) {
                                } catch (OutOfMemoryError o) {
                                }

                                System.out.println("Inside Me");
                                canv.drawBitmap(result, circle.getCenterX() - result.getWidth() / 3, circle.getCenterY() - result.getHeight() / 3, null);
                            }
                            else
                            {
                                canv.drawCircle(circle.getCenterX(), circle.getCenterY(), XHDPI_COUPLE_RADIUS_LIMIT, mMarriedPaint);
                                canv.drawText(dbHelper.getNameOfRelation("Self"), circle.getCenterX() - 34, circle.getCenterY(), mXHDPICoupleTextPaint);
                                Log.e("Ondraw Self", "Ondraw Self");
                            }


                        }
                        else
                        {
                            if(dbHelper.getWhatRelationTypes("Spouse"))
                            {
                               // System.out.println("Inside Spouse");
                                dbHelper.insertXYOfRelation("Spouse",circle.getCenterX(),circle.getCenterY());
                                Log.e("Spouseeeeeeee ", "" + circle.getCenterX() + " " + circle.getCenterY() + dbHelper.insertXYOfRelation("Spouse", circle.getCenterX(), circle.getCenterY()));
                                canv.drawCircle(circle.getCenterX(), circle.getCenterY(), XHDPI_COUPLE_RADIUS_LIMIT, mMarriedPaint);
                                canv.drawText(dbHelper.getNameOfRelation("Spouse"), circle.getCenterX() - 34, circle.getCenterY(), mXHDPICoupleTextPaint);
                            }
                            else
                            {
                                canv.drawCircle(circle.getCenterX(), circle.getCenterY(), XHDPI_COUPLE_RADIUS_LIMIT, mFilledPaint);
                                Log.e("Ondraw Spouse", "Ondraw Spouse");
                            }


                        }
                    }

                    //Parents
                    if(circle.radius==XHDPI_PARENTS_RADIUS_LIMIT)
                    {

                       // System.out.println("if(circle.radius==55)");
                        if(circle.getCenterX() == father_x && circle.getCenterY() == father_y)
                        {

                            if(dbHelper.getWhatRelationTypes("Father"))
                            {
                                dbHelper.insertXYOfRelation("Father", circle.getCenterX(), circle.getCenterY());
                              //  System.out.println("Inside Father" + "X : " + dbHelper.getXOfRelation("Father") + " Y: " +dbHelper.getYOfRelation("Father"));
                                    canv.drawCircle(dbHelper.getXOfRelation("Father"),dbHelper.getYOfRelation("Father"), XHDPI_PARENTS_RADIUS_LIMIT, mMarriedPaint);
                                    canv.drawText(dbHelper.getNameOfRelation("Father"), circle.getCenterX() - 22, circle.getCenterY(), mXHDPIParentsTextPaint);
                            }
                            else
                            {
                                canv.drawCircle(circle.getCenterX(), circle.getCenterY(), XHDPI_PARENTS_RADIUS_LIMIT, mFilledPaint);
                                Log.e("Ondraw Father", "Ondraw Father");
                            }
                        }else if(circle.getCenterX() == mother_x && circle.getCenterY() == mother_y)
                        {
                            if(dbHelper.getWhatRelationTypes("Mother"))
                            {
                               // System.out.println("Inside Mother");
                                dbHelper.insertXYOfRelation("Mother", circle.getCenterX(), circle.getCenterY());
                                canv.drawCircle(dbHelper.getXOfRelation("Mother"), dbHelper.getYOfRelation("Mother"), XHDPI_PARENTS_RADIUS_LIMIT, mMarriedPaint);
                                canv.drawText(dbHelper.getNameOfRelation("Mother"), circle.getCenterX() - 22, circle.getCenterY(), mXHDPIParentsTextPaint);
                            }
                            else
                            {
                                canv.drawCircle(circle.getCenterX(), circle.getCenterY(), XHDPI_PARENTS_RADIUS_LIMIT, mFilledPaint);
                                Log.e("Ondraw Mother", "Ondraw Mother");
                            }
                        }else if(circle.getCenterX() == spouse_father_x && circle.getCenterY() == spouse_father_y)
                        {
                            if(dbHelper.getWhatRelationTypes("Spouses Father"))
                            {
                               // System.out.println("Inside Spouses Father");
                                dbHelper.insertXYOfRelation("Spouses Father", circle.getCenterX(), circle.getCenterY());
                                canv.drawCircle(dbHelper.getXOfRelation("Spouses Father"), dbHelper.getYOfRelation("Spouses Father"), XHDPI_PARENTS_RADIUS_LIMIT, mMarriedPaint);
                                canv.drawText(dbHelper.getNameOfRelation("Spouses Father"), circle.getCenterX() - 22, circle.getCenterY(), mXHDPIParentsTextPaint);
                            }
                            else
                            {
                                canv.drawCircle(circle.getCenterX(), circle.getCenterY(), XHDPI_PARENTS_RADIUS_LIMIT, mFilledPaint);
                                Log.e("Ondraw Spouses Father", "Ondraw Spouses Father");
                            }
                        }else if(circle.getCenterX() == spouse_mother_x && circle.getCenterY() == spouse_mother_y)
                        {
                            if(dbHelper.getWhatRelationTypes("Spouses Mother"))
                            {
                                dbHelper.insertXYOfRelation("Spouses Mother", circle.getCenterX(), circle.getCenterY());
                                canv.drawCircle(dbHelper.getXOfRelation("Spouses Mother"), dbHelper.getYOfRelation("Spouses Mother"), XHDPI_PARENTS_RADIUS_LIMIT, mMarriedPaint);
                                canv.drawText(dbHelper.getNameOfRelation("Spouses Mother"), circle.getCenterX() - 22, circle.getCenterY(), mXHDPIParentsTextPaint);
                            }
                            else
                            {
                                canv.drawCircle(circle.getCenterX(), circle.getCenterY(), XHDPI_PARENTS_RADIUS_LIMIT, mFilledPaint);
                                Log.e("Ondraw Spouses Mother", "Ondraw Spouses Mother");
                            }
                        }

                    }



                   //Childrens
                    if (circle.radius == XHDPI_CHILD_RADIUS_LIMIT) {

                        if (dbHelper.FetchChildFriendCountFromUserTable("Child").size() > 0) {

                            child_x.clear();
                            child_y.clear();

                            child_x.add(c1_x);
                            child_x.add(c2_x);
                            child_x.add(c3_x);
                            child_x.add(c4_x);
                            child_x.add(c5_x);

                            child_y.add(c1_y);
                            child_y.add(c2_y);
                            child_y.add(c3_y);
                            child_y.add(c4_y);
                            child_y.add(c5_y);


                            for (int r = 0; r < dbHelper.FetchChildFriendCountFromUserTable("Child").size(); r++) {


                                if(dbHelper.FetchChildFriendCountFromUserTable("Child").get(r).equalsIgnoreCase("Child1"))
                                {

                                    new_c_x = c1_x;
                                    new_c_y = c1_y;

                                    dbHelper.insertChildOriginalXY("Child1", new_c_x, new_c_y);
                                    dbHelper.insertXYOfRelation("Child1", new_c_x, new_c_y);

                                    child_x.remove(new Integer(c1_x));
                                    child_y.remove(new Integer(c1_y));

                                    canv.drawCircle(dbHelper.getXOfRelation("Child1"), dbHelper.getYOfRelation("Child1"), XHDPI_DRAWN_CHILD_RADIUS_LIMIT, mChildFilledPaint);
                                    canv.drawText(dbHelper.getNameOfRelation("Child1"), dbHelper.getXOfRelation("Child1") - 14, dbHelper.getYOfRelation("Child1"), mXHDPIChildTextPaint);

                                }else  if(dbHelper.FetchChildFriendCountFromUserTable("Child").get(r).equalsIgnoreCase("Child2"))
                                {

                                    new_c_x = c2_x;
                                    new_c_y = c2_y;

                                    child_x.remove(new Integer(c2_x));
                                    child_y.remove(new Integer(c2_y));

                                    dbHelper.insertChildOriginalXY("Child2", new_c_x, new_c_y);
                                    dbHelper.insertXYOfRelation("Child2", new_c_x, new_c_y);
                                    canv.drawCircle(dbHelper.getXOfRelation("Child2"), dbHelper.getYOfRelation("Child2"), XHDPI_DRAWN_CHILD_RADIUS_LIMIT, mChildFilledPaint);
                                    canv.drawText(dbHelper.getNameOfRelation("Child2"), dbHelper.getXOfRelation("Child2") - 14, dbHelper.getYOfRelation("Child2"), mXHDPIChildTextPaint);
                                }else   if(dbHelper.FetchChildFriendCountFromUserTable("Child").get(r).equalsIgnoreCase("Child3"))
                                {

                                    new_c_x = c3_x;
                                    new_c_y = c3_y;


                                    child_x.remove(new Integer(c3_x));
                                    child_y.remove(new Integer(c3_y));


                                    dbHelper.insertChildOriginalXY("Child3", new_c_x, new_c_y);
                                    dbHelper.insertXYOfRelation("Child3", new_c_x, new_c_y);
                                    canv.drawCircle(dbHelper.getXOfRelation("Child3"), dbHelper.getYOfRelation("Child3"), XHDPI_DRAWN_CHILD_RADIUS_LIMIT, mChildFilledPaint);
                                    canv.drawText(dbHelper.getNameOfRelation("Child3"), dbHelper.getXOfRelation("Child3") - 14, dbHelper.getYOfRelation("Child3"), mXHDPIChildTextPaint);
                                }else   if(dbHelper.FetchChildFriendCountFromUserTable("Child").get(r).equalsIgnoreCase("Child4"))
                                {

                                    new_c_x = c4_x;
                                    new_c_y = c4_y;


                                    child_x.remove(new Integer(c4_x));
                                    child_y.remove(new Integer(c4_y));


                                    dbHelper.insertChildOriginalXY("Child4", new_c_x, new_c_y);
                                    dbHelper.insertXYOfRelation("Child4", new_c_x, new_c_y);
                                    canv.drawCircle(dbHelper.getXOfRelation("Child4"), dbHelper.getYOfRelation("Child4"), XHDPI_DRAWN_CHILD_RADIUS_LIMIT, mChildFilledPaint);
                                    canv.drawText(dbHelper.getNameOfRelation("Child4"), dbHelper.getXOfRelation("Child4") - 14, dbHelper.getYOfRelation("Child4"), mXHDPIChildTextPaint);
                                }else  if(dbHelper.FetchChildFriendCountFromUserTable("Child").get(r).equalsIgnoreCase("Child5"))
                                {

                                    new_c_x = c5_x;
                                    new_c_y = c5_y;


                                    child_x.remove(new Integer(c5_x));
                                    child_y.remove(new Integer(c5_y));

                                    dbHelper.insertChildOriginalXY("Child5", new_c_x, new_c_y);
                                    dbHelper.insertXYOfRelation("Child5", new_c_x, new_c_y);
                                    canv.drawCircle(dbHelper.getXOfRelation("Child5"), dbHelper.getYOfRelation("Child5"), XHDPI_DRAWN_CHILD_RADIUS_LIMIT, mChildFilledPaint);
                                    canv.drawText(dbHelper.getNameOfRelation("Child5"), dbHelper.getXOfRelation("Child5") - 14, dbHelper.getYOfRelation("Child5"), mXHDPIChildTextPaint);
                                }


                            }
                            if(child_x.size()!=0)
                            {
                                for(int i=0;i<child_x.size();i++)
                                {
                                    canv.drawCircle(child_x.get(i),child_y.get(i),XHDPI_CHILD_RADIUS_LIMIT,mCirclePaint);
                                }

                            }


                        } else {

                            canv.drawCircle(circle.getCenterX(), circle.getCenterY(), XHDPI_CHILD_RADIUS_LIMIT, mCirclePaint);

                        }
                    }

                    //Friends
                    if (circle.radius==FRIENDS_RADIUS_LIMIT)
                    {
                        //tujomila
                        if(dbHelper.FetchChildFriendCountFromUserTable("Friend").size() > 0)
                        {

                           // Log.e("","The friend list size is " + dbHelper.getFriendRelationTypes().size());
                           // Log.e("","The size of circlepointers is " + circle_pointers);

                            for (int q=1;q<=dbHelper.FetchChildFriendCountFromUserTable("Friend").size();q++)
                            {
                                //Log.e("","The X of friend" + q + " is " + dbHelper.getFriendCorrespondingXValue(q,"Married")) ;
                                //Log.e("","The Y of friend" + q + " is " + dbHelper.getFriendCorrespondingYValue(q,"Married")) ;

                                canv.drawCircle(dbHelper.getFriendCorrespondingXValue(q,"Married"),dbHelper.getFriendCorrespondingYValue(q,"Married"),FRIENDS_RADIUS_LIMIT,mChildFilledPaint);
                                canv.drawText(dbHelper.getNameOfRelation("Friend" + q), dbHelper.getXOfRelation("Friend" + q) - 17, dbHelper.getYOfRelation("Friend"+q), mXHDPIFriendTextPaint);
                                dbHelper.insertXYOfRelation("Friend" + q, dbHelper.getFriendCorrespondingXValue(q,"Married"), dbHelper.getFriendCorrespondingYValue(q,"Married"));
                                friend_x.add(dbHelper.getFriendCorrespondingXValue(q,"Married"));
                                friend_y.add(dbHelper.getFriendCorrespondingYValue(q,"Married"));

                            }
                            for(int w=dbHelper.FetchChildFriendCountFromUserTable("Friend").size()+ 1;w<=34;w++) {

                               // Log.e("Pointer is " + w,"The X value at w is " + dbHelper.getFriendCorrespondingXValue(w,"Married") + " The Y value at w is " + dbHelper.getFriendCorrespondingYValue(w,"Married"));
                                canv.drawCircle(dbHelper.getFriendCorrespondingXValue(w,"Married"),dbHelper.getFriendCorrespondingYValue(w,"Married"), FRIENDS_RADIUS_LIMIT, mCirclePaint);
                            }

                        }
                        else
                        {
                            canv.drawCircle(circle.getCenterX(), circle.getCenterY(), FRIENDS_RADIUS_LIMIT, mCirclePaint);
                        }
                    }
                }
                else
                {

                    //System.out.println("Else of CircleCanvas.member_type!=null");
                    if (circle.radius == FRIENDS_RADIUS_LIMIT) {
                        canv.drawCircle(circle.getCenterX(), circle.getCenterY(), FRIENDS_RADIUS_LIMIT, mCirclePaint);
                    }else if (circle.radius == XHDPI_CHILD_RADIUS_LIMIT) {
                    canv.drawCircle(circle.getCenterX(), circle.getCenterY(), XHDPI_CHILD_RADIUS_LIMIT, mCirclePaint);
                    } else if (circle.radius == XHDPI_PARENTS_RADIUS_LIMIT) {
                        canv.drawCircle(circle.getCenterX(), circle.getCenterY(), XHDPI_PARENTS_RADIUS_LIMIT, mFilledPaint);
                    } else {
                        canv.drawCircle(circle.getCenterX(), circle.getCenterY(), XHDPI_COUPLE_RADIUS_LIMIT, mMarriedPaint);

                    }
                }


            }

        }



    }

解决方案

I saw your code. So you are using a View to draw all the filled circles. The onDraw method is overloaded with calculations and object instantiation. I suggest that you remove as much as you can from onDraw method. If the calculation can be carried out during the construction of the View, then do it in the constructor and not in the onDraw. You need to keep onDraw as light as possible.

Now is this a good design? Having a View in your case is not a good idea, because a single change will redraw the whole view. Instead I suggest that you create a custom ViewGroup, and have all these filled circles as child to that. That way, when a single circle changes in color or text, you need to redraw the whole screen.

这篇关于不使用位图帆布发生在Android的更多的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆