簡體   English   中英

我在圖像上不能畫一個以上的圓圈

[英]I can't draw more than one circle on an image

我有個問題。 我需要在圖像上繪制一些圓圈,但是我不知道為什么,它只能繪制一個圓圈。 我將下面的代碼進行了更好的解釋:

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.previsualizacion);



    myImage = (ImageView) findViewById(R.id.imageView1);

    ctx = getApplicationContext();

    //Obtenemos la información del layout anterior y la asignamos al tamaño del paso
    data = getIntent();
    myBundle = data.getExtras();    

    presasX = myBundle.getStringArrayList("arrayPixelX");
    presasY = myBundle.getStringArrayList("arrayPixelY");
    colores = myBundle.getStringArrayList("arrayColores");      

    Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);  
    bitMap = bitMap.copy(bitMap.getConfig(), true);     
    Canvas canvas = new Canvas(bitMap);                 

    Paint paint = new Paint();                          
    paint.setColor(Color.RED);
    paint.setStyle(Style.FILL_AND_STROKE);
    //paint.setStrokeWidth(0.5f);
    paint.setAntiAlias(true);      

    myImage.setImageBitmap(bitMap);
    //changed set image resource to set image background resource
    myImage.setBackgroundResource(R.drawable.pared);

    for(int i = 0; i < presasX.size(); i++)
    {
        if(colores.get(i).equals("1"))
        {
            paint.setColor(Color.GREEN);    
        }
        else if(colores.get(i).equals("2"))
        {
            paint.setColor(Color.RED);  
        }
        else if(colores.get(i).equals("3"))
        {
            paint.setColor(Color.YELLOW);   
        }
        canvas.drawCircle(Float.parseFloat(presasX.get(i)) * myImage.getWidth() / 100, Float.parseFloat(presasY.get(i)) * myImage.getHeight() / 100, 3, paint);
    }

  //invalidate to update bitmap in imageview
    myImage.invalidate();
}

我在某些ArrayList上具有要繪制的點的坐標,而且該坐標是相對於圖像的,因此我必須將該坐標乘以imagewidht和imageheight。

它只畫了一個綠色圓圈,但我需要畫(按此順序)五個綠色點和四個紅色點。

我也把arraylist信息:

presasX = [49.583333333333、80.833333333333、13.541666666667、4.5833333333333、77.708333333333、49.583333333333、4.5833333333333、95.208333333333、96.875]

presasY = [49.722222222222、5、22.5、20.277777777778、33.888888888889、49.722222222222、20.277777777778、54.722222222222、61.666666666667]

顏色= [2,2,2,2,2,1,1,1,1]

非常感謝!

這是因為布局尚未完全膨脹,因此myImage.getWidth()和getHeight()返回0。因此,實際上所有圓的中心都相同(ImageView的左上角),下一個覆蓋了前一個。

對於實際的寬度和高度,您可以使用ViewTreeObserver.addOnGlobalLayoutListener或以編程方式創建具有所需尺寸的ImageView或創建擴展標准ImageView並實現其onDraw ()等的自定義視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM