0

我想在单击按钮时绘制一个矩形。我所做的所有研究都告诉我这应该有效,但事实并非如此。我简直无法理解为什么。

在我的 MainActivity 课程中,我有:

Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
public Canvas canvas = new Canvas(b);

在我的构造函数中,我有:

 button.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            Paint paint = new Paint();
            paint.setColor(Color.GREEN);
            //paint.setStrokeWidth(5);
            canvas.drawRect(0, 0, 50, 50, paint);
            Log.e("Blah Blah Blah", "Blah Blah, Blah");
        }
    });

它确实进入了我看到的函数,因为当我点击它时,“Blah Blah Blah”被记录下来,但它没有绘制矩形。

有任何想法吗?

4

1 回答 1

1

您需要将画布设置为 ImageView

//Create a new image bitmap and attach a brand new canvas to it
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);

//Draw the image bitmap into the cavas
tempCanvas.drawBitmap(myBitmap, 0, 0, null);

 Paint paint = new Paint();
 paint.setColor(Color.GREEN);
 //paint.setStrokeWidth(5);
tempCanvas.drawRoundRect(new RectF(x1,y1,x2,y2), 2, 2, paint);

//Attach the canvas to the ImageView
myImageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
于 2013-06-13T19:44:22.647 回答