如何使用setContentView在vanilla Activity之外绘制平滑图形

时间:2015-04-15 21:46:51

标签: android android-activity android-canvas

如果有人能看到我做错了什么,我会遇到一个有趣的情况?我在下面有一个名为TrackView的类。它基本上打开了一个洞,您可以触摸屏幕,仅在该触摸点显示隐藏的背景。当我从setContentView(new TrackView(this))调用它时,这一切都很好用。图形快速流畅。如果我以下面的方式使用完全相同的TrackView类,这是从服务开始的(为什么我使用WindowManager),图形绘制得非常糟糕和缓慢。在每种情况下视图层次结构都是相同的。我也尝试过SurfaceView,但问题并没有消失。

public class OverlayTesting
{
    private WindowManager wm;
    private LayoutInflater li;
    private RelativeLayout rl;
    private View v;
    private TrackView tv;

    public OverlayTesting(Context context)
    {
        wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        li = LayoutInflater.from(context);
        v  = li.inflate(R.layout.activity_overlay, null);
        rl = (RelativeLayout) v.findViewById(R.id.relativeLayout);
        tv = new TrackView(context);
        rl.addView(tv);
    }

    public void show()
    {
        wm.addView(rl, new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
    }

    private class TrackView extends View
    {
        Bitmap bitmapBase;
        Bitmap bitmapDefault;
        Bitmap bitmapUsed;
        Canvas canvasBase;
        Canvas canvasDefault;
        Canvas canvasUsed;
        Paint paint1;
        Paint paint2;
        int displayHeight;
        int displayWidth;
        int X = -100;
        int Y = -100;

        public TrackView(Context context)
        {
            super(context);

            Display display = wm.getDefaultDisplay();
            Point point = new Point();

            display.getSize(point);
            displayWidth  = point.x;
            displayHeight = point.y;

            // create base background
            bitmapBase = Bitmap.createBitmap(displayWidth, displayHeight, Bitmap.Config.ARGB_8888);
            canvasBase = new Canvas(bitmapBase);
            canvasBase.drawColor(Color.WHITE);

            // create overlays and masks
            bitmapDefault = Bitmap.createBitmap(displayWidth, displayHeight, Bitmap.Config.ARGB_8888);
            canvasDefault = new Canvas(bitmapDefault);
            canvasDefault.drawColor(Color.BLACK);

            bitmapUsed = Bitmap.createBitmap(displayWidth, displayHeight, Bitmap.Config.ARGB_8888);  
            canvasUsed = new Canvas(bitmapUsed);

            // paint structures here

        }

        @Override
        public boolean onTouchEvent(MotionEvent ev)
        {
            switch (ev.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    X = (int) ev.getX();
                    Y = (int) ev.getY();
                    invalidate();

                    break;

                case MotionEvent.ACTION_MOVE:
                    X = (int) ev.getX();
                    Y = (int) ev.getY();
                    invalidate();

                    break;
            }

            return true;
        }

        @Override
        public void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);

            // refresh overlay with default bitmap and open new hole at XY
            // refresh overlay code here

            // refresh core view canvas showing only area at XY
            // draw canvas code here
        }
    }
}

0 个答案:

没有答案