【问题标题】:Drawing to the screen - need to be able to zoom and pinch绘制到屏幕 - 需要能够缩放和捏合
【发布时间】:2013-02-22 05:05:07
【问题描述】:

我目前正在我的应用中绘制到屏幕上。用户可以绘制一个代表他们家的房间的矩形,但目前它们仅限于初始空间。

让屏幕在更大的画布上成为一个“窗口”的最佳方法是什么,用户可以在绘图时滚动、缩放等? (我很欣赏必须有“绘图”触摸模式和“移动”触摸模式)

另外,canvas 是一个自定义的 View 类:

class HomerView extends View { // the custom View for drawing on
    // set up Bitmap, canvas, path and paint
    private Bitmap myBitmap; // the initial image we turn into our canvas
    private Canvas myCanvas; // the canvas we are drawing on
    private Rect myRect; // the mathematical path of the lines we draw
    private Paint myBitmapPaint; // the paint we use to draw the bitmap

    // get the width of the entire tablet screen
    private int screenWidth = getContext().getResources()
            .getDisplayMetrics().widthPixels;
    // get the height of the entire tablet screen
    private int screenHeight = getContext().getResources()
            .getDisplayMetrics().heightPixels;

    public HomerView(Context context) { // constructor of HomerView
        super(context);
        myBitmap = Bitmap.createBitmap(screenWidth, screenHeight,
                Bitmap.Config.ARGB_8888); // set our drawable space - the bitmap which becomes the canvas we draw on
        myCanvas = new Canvas(myBitmap); // set our canvas to our bitmap which we just set up
        myRect = new Rect(); // make a new rect
        myBitmapPaint = new Paint(Paint.DITHER_FLAG); // set dither to ON in our saved drawing - gives better color interaction
    }

    protected void onDraw(Canvas canvas) { // method used when we want to draw something to our canvas
        canvas.drawColor(Color.TRANSPARENT); // sets canvas colour
        canvas.drawBitmap(myBitmap, 0, 0, myBitmapPaint); // save the canvas to bitmap - the numbers are the x, y coords we are drawing from
        canvas.drawRect(myRect, myPaint); // draw the rectangle that the user has drawn using the paint we set up
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { // if screen size changes, alter the bitmap size
        super.onSizeChanged(w, h, oldw, oldh);
        /*myBitmap = Bitmap.createBitmap(screenWidth, screenHeight,
                Bitmap.Config.ARGB_8888);
        myCanvas = new Canvas(myBitmap);*/
    }

            public void drawApplianceIcon() {
        myCanvas.drawBitmap(bmp, iX - 50, iY - 50, myBitmapPaint);
        makeToast("Lamp Drawn To Canvas");
    }


    // these variables are needed in touch listening
    private int mX, mY, iX, iY; // current x,y and initial x,y
    private static final float TOUCH_TOLERANCE = 4;

private void touch_start {} // touch methods
private void touch_move {}
private void touch_up {}

在 onCreate 中声明如下:

 View drawingAreaView = new HomerView(this);
 setContentView(drawingAreaView);

如何调整我的代码以使其成为可滚动窗口?

【问题讨论】:

    标签: android scroll android-view


    【解决方案1】:

    在视图初始化期间试试这个?

    setHorizontalScrollBarEnabled(true);
    setVerticalScrollBarEnabled(true);
    

    我猜你还需要http://developer.android.com/reference/android/view/ScaleGestureDetector.html

    还有一些包含所有位置的视口逻辑。

    也找到了这个。

    Android: Enable Scrollbars on Canvas-Based View

    【讨论】:

    • 感谢您的帮助!什么是视口?我会看看你发布的内容。
    • 查看区域。我想你可以使用 canvas.translate。
    猜你喜欢
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多