【问题标题】:onDraw() method not drawing all of the bitmapsonDraw() 方法未绘制所有位图
【发布时间】:2016-01-25 08:11:54
【问题描述】:

我有一个根据人的生日绘制行星和星座的应用程序。我在自定义View 中使用onDraw() 方法,该方法添加到XML 中的FrameLayout。问题是当我离开页面并调整日期时,活动只绘制行星而不是新日期的星座。所有这些“重新”绘图都发生在同一个onDraw() 方法中,我可以看到onDraw() 方法被调用,即使是添加星座位图的部分。鉴于他们正在被调用,我无法判断出了什么问题,并且感觉它与位图有关。而且由于行星被重新绘制,我对问题可能是什么感到更加困惑。我已经包含了据称正在执行但未显示在屏幕上的代码。

    Log.i("ascendet planets", Integer.toString(ascendent));

    Bitmap capricornBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.capricorn);
    Bitmap aquariusBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aquarius);
    Bitmap piscesBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pisces);
    Bitmap airesBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aires);
    Bitmap taurusBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.taurus);
    Bitmap geminiBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gemini);
    Bitmap cancerBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cancer);
    Bitmap leoBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.leo);
    Bitmap virgoBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.virgo);
    Bitmap libraBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.libra);
    Bitmap scorpioBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.scorpio);
    Bitmap sagitariusBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sagittarius);


    ArrayList<Bitmap> bitmapArray = new ArrayList<>();
    bitmapArray.add(capricornBitmap);
    bitmapArray.add(aquariusBitmap);
    bitmapArray.add(piscesBitmap);
    bitmapArray.add(airesBitmap);
    bitmapArray.add(taurusBitmap);
    bitmapArray.add(geminiBitmap);
    bitmapArray.add(cancerBitmap);
    bitmapArray.add(leoBitmap);
    bitmapArray.add(virgoBitmap);
    bitmapArray.add(libraBitmap);
    bitmapArray.add(scorpioBitmap);
    bitmapArray.add(sagitariusBitmap);




    int bitmapWidth;
    int bitmapHeight;



    float x = 0;
    float y = 0;
    for (int i = ascendent; i < ascendent + 12; i++) {
        int multiplier = i - ascendent;
        bitmapWidth = bitmapArray.get(i%12).getWidth();
        bitmapHeight = bitmapArray.get(i%12).getHeight();

        x = (float) getPositionX(r_adjusted, marginOneSide, multiplier * 30 + 15, ascendent);
        y = (float) getPositionY(r_adjusted, height, multiplier * 30 +  15,          ascendent);

        x += bitmapWidth / 2;
        y -= bitmapHeight / 2;

        Log.i("bitmapWidth", Integer.toString(bitmapHeight));
        Log.i("bitmapHeight", Integer.toString(bitmapHeight));
        Log.i("i in planets", Integer.toString(i));

        canvas.drawBitmap(bitmapArray.get(i % 12), x, y, null);
    }

【问题讨论】:

    标签: android view bitmap ondraw


    【解决方案1】:

    您是否尝试过记录“x”和“y”?所有计算后的值是否正确?

    【讨论】:

    • 我希望它是那么简单。函数 getPostionX() 和 getPositionY() 只返回 15、45、75...345 度圆上 X 和 Y 的值。这些点然后接收黄道十二宫位图。黄道带位图第一次加载到屏幕上,但它们只加载一次。十二生肖应该随着时间改变位置。如果在行星中输入不同的时间和日期,则位置会改变,但黄道带符号不会改变,尽管通过了相同的 onDraw() 方法。不过现在我会发布 x、y 坐标。
    • 感谢您的建议。一个简单的答案。我记录了 x 和 y 我意识到我有一个 (+ n*Math.PI/6) ,我没有将它放在余弦函数中的 Math.toRadians() 中。所以问题解决了。感谢外部视角。可能剃了一天的头发。
    猜你喜欢
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    • 2014-05-30
    • 2016-10-30
    • 2015-04-10
    相关资源
    最近更新 更多