画布部分放大

时间:2012-07-30 11:34:15

标签: android

我想部分放大帆布板,因为字母托盘按钮将被固定,而且电路板将变焦,
部分编码如下所述,屏幕完全画在画布上,可以在底部看到..请帮助并感谢您的关注。

        public class BoardView extends SurfaceView implements SurfaceHolder.Callback
                {
        class DrawingThread extends Thread implements OnTouchListener {

        public DrawingThread(SurfaceHolder holder, Handler handler) {
                    mSurfaceHolder = holder;

        public void setRunning(boolean b) {
                    mRun = b;
                }

                @Override
                public void run() {
                    while (mRun) {
                        Canvas c = null;
                        try {
                            c = mSurfaceHolder.lockCanvas(null);

        synchronized (mSurfaceHolder) {
                                // System.gc();
                                // c.scale(canvasScaleX, canvasScaleY);
                                // c.save();
                                // c.translate(canvasTranslateX, canvasTranslateY);
                                doDraw(c);
                            }

                            updateGame();
                        } finally {
                            if (c != null) {
                                mSurfaceHolder.unlockCanvasAndPost(c);
                            }
                        }
        }

        private void doDraw(Canvas canvas) {
                    if (ge == null)
                        return;

                        Rect bRect = new Rect(0, 0, dims.getTotalWidth(),
                        dims.getScoreHeight() + dims.getBoardheight());
                Drawable drawable = getResources().getDrawable(R.drawable.board);
                drawable.setBounds(bRect);

                drawable.draw(canvas);

                Rect tRect = new Rect(0, dims.getScoreHeight()
                        + dims.getBoardheight(), dims.getTotalWidth(),
                        dims.getTotalHeight());
                canvas.drawRect(tRect, fillTrayPaint);

                int topHeight = dims.getScoreHeight() + dims.getBoardheight();
                int bottom = (dims.getTotalHeight() + 5)
                        - (dims.getTotalWidth() / Tray.TRAY_SIZE);
                Rect rect = new Rect(0, topHeight, dims.getTotalWidth(), bottom - 7);
                Drawable drawableTray = getResources()
                        .getDrawable(R.drawable.strip);
                drawableTray.setBounds(rect);
                drawableTray.draw(canvas);
                        drawTray(canvas);
                drawBoard(canvas);

                // drawScore(canvas);
                drawMovingTile(canvas);
            }


public BoardView(Context context, AttributeSet attrs) {
        super(context, attrs);

        SurfaceHolder holder = getHolder();
        holder.addCallback(this);

        // endTurn=(ImageButton)findViewById(R.id.endturn_button_horizontal);
        thread = new DrawingThread(holder, handler);
    }
@Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        final float scale1 = getResources().getDisplayMetrics().density;
        defaultFontSize = (int) (MIN_FONT_DIPS * scale1 + 0.5f);
        thread.setDefaultFontSize(defaultFontSize);
        dimensions = calculateDimensions(getWidth(), getHeight());
        thread.setDimensions(dimensions);

        thread.setRunning(true);

        // if (thread != null && !thread.isAlive())
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        thread.setRunning(false);
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {

            }
        }
    }

The screen is fully draw in canvas which is mention as below :-

4 个答案:

答案 0 :(得分:2)

我们可以通过canvas.clipRect(rectangle)来完成它,此时我们要做的是保存画布canvas.save()并在canvas.clipRect(rectangle)之后调用canvas.restore()。< / p>

答案 1 :(得分:1)

考虑尝试这样的事情:

  • 从您要放大的画布创建位图。
  • 将该位图渲染到屏幕时,您可以更改scr rect (将“缩放”到位图中。)

我希望这会有所帮助。

答案 2 :(得分:0)

我认为您需要将游戏板渲染到屏幕外位图,然后使用Matrix在画布上绘制此位图,这样您就可以更改其位置和比例。

答案 3 :(得分:0)

我认为最简单的方法是使用Canvas#scale(float scale, int pivotx, int pivoty)方法。它基本上可以统一地增加整个画布,这实际上就是从2D角度进行缩放。

canvas.scale(2.0f, getWidth()/2, getHeight()/2)会将图像从中心缩放2。 canvas.scale(1.0f, getWidth()/2, getHeight()/2)会缩回它。

我从来没有尝试过这种情况,所以如果你决定尝试报告结果请!我想知道以备将来参考。