1. 程式人生 > >安卓懸浮窗小球的實現

安卓懸浮窗小球的實現


1.首先自定義view懸浮小球

public class FloatCircleView extends View{

}

2.重寫構造方法

public FloatCircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
// 相對於定義了佈局檔案,有style樣式
initPaints();// 初始化畫筆的方法
}

3.在構造方法初始化畫筆

private void initPaints() {
// TODO Auto-generated method stub
circlePaint = new Paint();//畫筆物件
circlePaint.setColor(Color.GRAY);//畫筆顏色
circlePaint.setAntiAlias(true);//防止鋸齒效果


textPaint = new Paint();
textPaint.setTextSize(25);//畫筆大小
textPaint.setColor(Color.WHITE);//畫筆顏色
textPaint.setAntiAlias(true);//畫筆防止鋸齒效果
textPaint.setFakeBoldText(true);//加粗效果

Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.zoom_plate);
bitmap = Bitmap.createScaledBitmap(src, width, height, true);
}

4.重寫onMeasure方法

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
setMeasuredDimension(width, height);// 控制元件寬高
}

5.重寫onDraw方法

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
if(drag){
canvas.drawBitmap(bitmap, 0, 0,null);
}else{
/**
* drawCircle畫圓
* 引數一 引數二:確認中心點圓心  引數三:半徑 引數四:對應畫筆

* drawText繪製文字
* 引數一:文字內容 引數二,三:文字x軸座標,文字y軸基線座標,引數四:相應畫筆
*/
canvas.drawCircle(width/2, height/2, width/2, circlePaint);
float textwidth = textPaint.measureText(text);//文字寬度
float x = width/2 - textwidth/2;
FontMetrics metrics = textPaint.getFontMetrics();
float dy = -(metrics.descent+metrics.ascent)/2;
float y = height/2+dy;
canvas.drawText(text, x, y, textPaint);
}

}

6.在浮窗管理FloatViewManager處理懸浮小球的移動,貼邊,開啟,關閉等功能

 (1)單例類

private static FloatViewManager instance;// 私有化FloatViewManager類
private WindowManager wn;// 通過這個windowmanager來操控浮窗體的顯示和隱藏以及位置的改變


// 通過公共的方法獲取FloatViewManager類,使其成為單例類
public static FloatViewManager getInstance(Context context) {
if (instance == null) {
synchronized (FloatViewManager.class) {
if (instance == null) {
instance = new FloatViewManager(context);
}


}
}
return instance;


}

7.通過OnTouchListener來控制懸浮窗的狀態

private OnTouchListener onTouchListener = new OnTouchListener() {

@Override
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:

手指點選時
startx = event.getRawX();
starty = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:

移動時懸浮窗
float x = event.getRawX();
float y = event.getRawY();

float dx = x-startx;
float dy = y-starty;

params.x+=dx;
params.y+=dy;

circleView.setDragState(true);
wn.updateViewLayout(circleView, params);
startx = x;
starty = y;
break;
case MotionEvent.ACTION_UP:

最後懸浮窗放開的位置
float x1 =event.getRawX();
if(x1>getScreenWidth()/2){
params.x = getScreenWidth()-circleView.width;
}else {
params.x = 0;
}
circleView.setDragState(false);
wn.updateViewLayout(circleView, params);
break;


default:
break;
}
return false;
}
};

下面是demo原始碼,上面是demo的一部分程式碼

http://download.csdn.net/download/ou775968876/10107916