【问题标题】:I can't draw more than one circle on an image我不能在一张图片上画一个以上的圆圈
【发布时间】:2016-01-28 14:13:44
【问题描述】:

我有问题。我需要在图像上画一些圆圈,但我不知道为什么,它只画了一个圆圈。我把代码放在下面是为了更好地解释它:

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 上有我想要绘制的点的坐标,而且这个坐标是相对于图像的,所以我必须将图像宽度和图像高度的坐标相乘。

它只画了一个绿色圆圈,但我需要(在那次追逐中)画五个绿点和四个红点。

我也放了arraylist信息:

presasX = [49.583333333333,80.833333333333,13.541666666667,4.5833333333333,77.708333333333,49.583333333333,4.5833333333333,95.208333333333,96.875] P>

presasY = [49.722222222222, 5, 22.5, 20.277777777778, 33.888888888889, 49.722222222222, 20.277777777778, 54.7222222226666666]

颜色 = [2, 2, 2, 2, 2, 1, 1, 1, 1]

非常感谢!

【问题讨论】:

  • Float.parseFloat(presasX.get(i)) * myImage.getWidth() - 鉴于presasX.get(0) == 49.583333333333,这将是一个非常大的数字,超出了图像的范围。
  • 我除以 100,但结果相同。有什么想法吗?

标签: java android canvas bitmap imageview


【解决方案1】:

这是因为布局没有完全膨胀,因此 myImage.getWidth() 和 getHeight() 返回 0。所以实际上所有圆圈的中心是相同的(ImageView 的左上角)和下一个正在覆盖前一个。

对于实际宽度和高度,您可以使用 ViewTreeObserver.addOnGlobalLayoutListener 或以编程方式创建具有所需尺寸的 ImageView 或创建扩展标准 ImageView 的自定义视图并实现其onDraw() 等。

【讨论】:

    猜你喜欢
    • 2018-07-18
    • 2014-11-15
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2014-08-22
    • 1970-01-01
    • 2015-10-22
    相关资源
    最近更新 更多