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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值