在叠加层中的位图上绘制动态文本 window
Drawing dynamic text on a bitmap placed in overlay window
我有一张显示在系统覆盖图中的位图 window,我想为其放置一个动态计数器。
public class AlwaysOnTop extends Service {
HUDView mView;
@Override
public void onCreate() {
super.onCreate();
final Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
R.drawable.gold_btn);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
kangoo.getWidth(),
kangoo.getHeight(),
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.BOTTOM;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
mView = new HUDView(this, kangoo);
mView.setOnTouchListener(new OnTouchListener() {
int counter = Integer.parseInt(getFromSharedPrefs("counter"));
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_UP) {
if (arg1.getX() < kangoo.getWidth() & arg1.getY() > 0) {
counter++;
//now it should update the text on bitmap
}
}
return true;
}
});
wm.addView(mView, params);
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
@SuppressLint("DrawAllocation")
class HUDView extends ViewGroup {
Bitmap kangoo;
Context context;
Canvas myCanvas;
Boolean canvasLoaded = false;
public HUDView(Context context, Bitmap kangoo) {
super(context);
this.context = context;
this.kangoo = kangoo;
}
protected void onDraw(Canvas canvas) {
myCanvas = canvas;
Resources resources = getContext().getResources();
float scale = resources.getDisplayMetrics().density;
canvas.drawBitmap(kangoo, 0, 0, null);
canvasLoaded = true;
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.rgb(61, 61, 61));
// text size in pixels
paint.setTextSize((int) (14 * scale));
// text shadow
paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(counter, 0, counter.length(), bounds);
int x = (kangoo.getWidth() - bounds.width()) / 2;
int y = (kangoo.getHeight() + bounds.height()) / 2;
//canvas.drawText(counter, x, y, paint);
}
protected Canvas getCanvas() {
if (getCanvasLoaded()) {
return myCanvas;
} else {
Log.e("AHA", "canvas returned null!");
return null;
}
}
protected Boolean getCanvasLoaded() {
return canvasLoaded;
}
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
}
public boolean onTouchEvent(MotionEvent event) {
return true;
}
}
如代码所示,每次调用 onTouch() 时,计数器都会递增。但我无法动态更改位图上的文本。那就是我需要帮助的地方。
谢谢。
我终于知道怎么做了。诀窍是在 onTouch()
上使用 invalidate()
方法,它会在每次点击时重绘 canvas 上的位图。
我有一张显示在系统覆盖图中的位图 window,我想为其放置一个动态计数器。
public class AlwaysOnTop extends Service {
HUDView mView;
@Override
public void onCreate() {
super.onCreate();
final Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
R.drawable.gold_btn);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
kangoo.getWidth(),
kangoo.getHeight(),
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.BOTTOM;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
mView = new HUDView(this, kangoo);
mView.setOnTouchListener(new OnTouchListener() {
int counter = Integer.parseInt(getFromSharedPrefs("counter"));
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_UP) {
if (arg1.getX() < kangoo.getWidth() & arg1.getY() > 0) {
counter++;
//now it should update the text on bitmap
}
}
return true;
}
});
wm.addView(mView, params);
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
@SuppressLint("DrawAllocation")
class HUDView extends ViewGroup {
Bitmap kangoo;
Context context;
Canvas myCanvas;
Boolean canvasLoaded = false;
public HUDView(Context context, Bitmap kangoo) {
super(context);
this.context = context;
this.kangoo = kangoo;
}
protected void onDraw(Canvas canvas) {
myCanvas = canvas;
Resources resources = getContext().getResources();
float scale = resources.getDisplayMetrics().density;
canvas.drawBitmap(kangoo, 0, 0, null);
canvasLoaded = true;
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.rgb(61, 61, 61));
// text size in pixels
paint.setTextSize((int) (14 * scale));
// text shadow
paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(counter, 0, counter.length(), bounds);
int x = (kangoo.getWidth() - bounds.width()) / 2;
int y = (kangoo.getHeight() + bounds.height()) / 2;
//canvas.drawText(counter, x, y, paint);
}
protected Canvas getCanvas() {
if (getCanvasLoaded()) {
return myCanvas;
} else {
Log.e("AHA", "canvas returned null!");
return null;
}
}
protected Boolean getCanvasLoaded() {
return canvasLoaded;
}
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
}
public boolean onTouchEvent(MotionEvent event) {
return true;
}
}
如代码所示,每次调用 onTouch() 时,计数器都会递增。但我无法动态更改位图上的文本。那就是我需要帮助的地方。
谢谢。
我终于知道怎么做了。诀窍是在 onTouch()
上使用 invalidate()
方法,它会在每次点击时重绘 canvas 上的位图。