how to add a button to a custom view that displays bitmap
i've an app that has a custom view that draws a bitmap. i'd like to place a button in the view on top of the bitmap. the activity's setContentView is set to the custom view class so i can't inflate the button from the main.xml. How do i get around this? Is it possible to create a button programatically in the view class and then attach a listener?
public class Jjilapp extends Activity {
private static final String TAG = "*********jjil";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "***********inside oncreate about to set contentview = ");
setConten开发者_运维百科tView(new TouchView(this));
}
class TouchView extends View{
public TouchView(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
.......
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inSampleSize = 1;
bm = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length, bfo);
bgr = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
bgr = bm.copy(bm.getConfig(), true);
}// end of class touchView
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawBitmap(bgr, 0, 0, null);
canvas.drawCircle(centreX, centreY, radius,pTouch);
}//end of onDraw
}
}
You could:
- use a
FrameLayout
(it can pile up views) containing your view and the button; - draw some button in your
onDraw()
and check if touches are over that area; - derive your view from some
ViewGroup
or*Layout
, so that you can add views to it.
edit: in the last case, remember to call setWillNotDraw(false)
.
精彩评论