在主屏幕上使用后台服务绘制位图图像

时间:2012-10-23 20:12:11

标签: java android layout android-windowmanager

我有一个带有就绪按钮的用户界面,此按钮点按了调用的后台服务。我需要在主屏幕上设置位图图像就像屏幕上的划痕一样。如果没有在活动中显示布局,有没有办法做到这一点。或者,即使我们使用布局在屏幕上显示该图像,然后我需要背景(主页,按钮和当前打开的所有其他内容)激活

我附上上面的图片作为参考,你可以看到我想要的东西。请帮我。您的关注将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

对你来说这是迟到的答案,但也许可以节省别人的一天...... :)

Permission : android.permission.SYSTEM_ALERT_WINDOW

 class ScreenOverlayService extends Service {
HUDView mView;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("onStartCommand", "called....");      

    //      return android.app.Service.START_STICKY;    // don't kill service...
    return START_NOT_STICKY;
}

@Override
public void onCreate() {
    super.onCreate();

    mView = new HUDView(this);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.RIGHT | Gravity.TOP;
    params.setTitle("Load Average");
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.addView(mView, params);

}

@Override
public void onDestroy() {
    super.onDestroy();

    if(mView != null){
        ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
        mView = null;
    }
}

class HUDView extends ViewGroup {
    private Paint mLoadPaint;
    Context context;

    public HUDView(Context context_)
    {
        super(context_);
        context = context_;

        mLoadPaint = new Paint();
        mLoadPaint.setAntiAlias(true);
        mLoadPaint.setTextSize(10);
        mLoadPaint.setARGB(255, 255, 0, 0);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.drawText("Hello World", 5, 15, mLoadPaint);

        // Read the image
        Bitmap markerImage = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);

        //  Draw it, centered around the given coordinates
        canvas.drawBitmap(markerImage,
                screenPoint.x - markerImage.getWidth() / 2,
                screenPoint.y - markerImage.getHeight() / 2, null);

    }

    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
}

}