绘制按钮单击

时间:2013-06-13 19:41:01

标签: android

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

在我的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”,但它不会绘制矩形。

有什么想法吗?

1 个答案:

答案 0 :(得分: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));