Android 如何在视图上添加视图(例如,图像上的按钮)
图像是在imageview中设置的。借助下面的代码,我成功地在图像上绘制了文本Android 如何在视图上添加视图(例如,图像上的按钮),android,button,imageview,Android,Button,Imageview,图像是在imageview中设置的。借助下面的代码,我成功地在图像上绘制了文本 BitmapFactory.Options myOptions = new BitmapFactory.Options(); myOptions.inDither = true; myOptions.inScaled = false; myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
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);
但我想在图片上画不同高度和宽度的按钮 在relativelayout中添加您的imageview,然后在此布局上添加您的按钮。我希望在图像上编程绘制按钮。将您迄今为止尝试的代码从数据库中获取高度和宽度,并在该hiegt宽度上绘制按钮。按钮b=新按钮(此按钮);b、 setText(“动态添加的按钮!”);b、 setLayoutParams(新建FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_内容,FrameLayout.LayoutParams.WRAP_内容));b、 setId(0);ll.addView(b);这里是线性布局,但我想在imageview上添加按钮,从数据库中获取高度和宽度。
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);