在叠加窗口中放置的位图上绘制动态文本

时间:2015-08-02 21:01:23

标签: android bitmap android-service viewgroup

我有一个位图显示在系统覆盖窗口中,我想为它放置一个动态计数器。

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()时,计数器都会递增。但我无法动态更改位图上的文本。这就是我需要帮助的地方。

感谢。

1 个答案:

答案 0 :(得分:0)

我终于找到了如何做到这一点。诀窍是在invalidate()上使用onTouch()方法,每次点击都会在画布上重绘位图。