`

Android中如何自定义view

阅读更多
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,
    
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 now
	public 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;
		}
		
		@Override
		public 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 anything
			canvas.drawBitmap(bmp, x, y, paint);
		}
	}

	//Add view
	layout.addView(dv);


BR,
Allen
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics