簡體   English   中英

在主屏幕上使用后台服務繪制位圖圖像

[英]drawing bitmap image using background service on home screen

當這個按鈕點擊調用的后台服務時,我有一個帶有就緒按鈕的UI。 我需要在主屏幕上設置位圖圖像就像屏幕上的划痕一樣。 如果沒有在活動中顯示布局,有沒有辦法做到這一點。 或者,如果我們使用布局在屏幕上顯示該圖像,那么我需要背景(主頁,按鈕和當前打開的所有其他內容)活動

我附上上面的圖片作為參考,你可以看到我想要的東西。 請幫我。 您的關注將受到高度贊賞。

這是你的遲到答案,但也許可以節省別人的一天...... :)

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);
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM