如何在视图上添加视图(例如图像上的按钮)

How to add view on view (e.g. button on image)

图片在imageview中设置。在下面的代码的帮助下,我在图像上成功地绘制了文本。

  BitmapFactory.Options myOptions = new BitmapFactory.Options();
        myOptions.inDither = true;
        myOptions.inScaled = false;
        myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
        myOptions.inPurgeable = true;

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.awais, myOptions);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);


        Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
        Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);


        Canvas canvas = new Canvas(mutableBitmap);
        // canvas.drawCircle(60, 50, 25, paint);
        canvas.drawText(String.valueOf(f), h, w, paint);


        imageView.setAdjustViewBounds(true);
        imageView.setImageBitmap(mutableBitmap);

但我想在具有不同高度和宽度的图像上绘制按钮。

    If you already define your imageView inside relative layout in xml than just instantiate it by findViewById

             //RelativeLayout relativeLayout =(RelativeLayout)(findViewById(.....));

    otherwise dynamically create relative layout like this

    RelativeLayout relativeLayout = new RelativeLayout(this);        
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.WRAP_CONTENT,
                            RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.CENTER_IN_PARENT);
    relativeLayout.setLayoutParams(lp);
    relativeLayout.addView(imageview);

    //than create button dynmaically
                    Button b = new Button(this);
                    b.setText("Button");
                    RelativeLayout.LayoutParams btnrl = new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.WRAP_CONTENT,
                            RelativeLayout.LayoutParams.WRAP_CONTENT);
                    btnrl.addRule(RelativeLayout.ALIGN_BOTTOM);
                    b.setLayoutParams(btnrl);

                    relativeLayout.addView(b);


        **Edit:**
In case you want arbitrary width and height than set button with and height like this


        int width = 0; // assign your width
        int height = 0; // assign your height
        RelativeLayout.LayoutParams btnrl = new RelativeLayout.LayoutParams(
                        width,height);
        btnrl.addRule(RelativeLayout.ALIGN_BOTTOM);
        b.setLayoutParams(btnrl);