Android中如何自定义view
16lz
2021-01-24
Hi,titan:
To the best of my knowledge, there are two ways to implement customized view.
The first is using "WindowManager.LayoutParams"
Ex as below,
And the second is to define your customized view's class, see below:
BR,
Allen
To the best of my knowledge, there are two ways to implement customized view.
The first is using "WindowManager.LayoutParams"
Ex as below,
WindowManager.LayoutParams lp;//allen add view begin LayoutInflater inflate = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); floatview = (RelativeLayout) inflate.inflate(R.layout.mapfloatview, null); mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE); lp = new WindowManager.LayoutParams( 320, 40, -160,-180, WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, //WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, PixelFormat.TRANSLUCENT); //allen add view end mWindowManager.addView(floatview, lp);
And the second is to define your customized view's class, see below:
drawView dv;dv = new drawView(this);//try to canvas, not used nowpublic class drawView extends View{private Bitmap bmp;private int x, y;public drawView(Context context){super(context);setFocusable(true);bmp = BitmapFactory.decodeResource(getResources(), R.drawable.b1);x = 0;y = 0;}@Overridepublic void onDraw(Canvas canvas){super.onDraw(canvas);Paint paint = new Paint();/*switch(posState){case PosState.LEFT_TOP:{if(leftTopState == LEFT_TOP_STATE_ONE){Log.i(TAG,"leftTopState == LEFT_TOP_STATE_ONE");canvas.drawBitmap(bmp, 0, 0, paint);}else if(leftTopState == LEFT_TOP_STATE_TWO){Log.i(TAG,"leftTopState == LEFT_TOP_STATE_TWO");canvas.drawBitmap(bmp, 0, 189, paint);}break;}}*///Here you may draw anythingcanvas.drawBitmap(bmp, x, y, paint);}}//Add viewlayout.addView(dv);
BR,
Allen
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer