1

假设我将一些点设为 (x1,y1) = (133,123)、(x2,y2) = (149,136)、(x3,y3) = (182,136) 等等,这样就形成了这样的形状: 在此处输入图像描述 在此处输入图像描述

现在我想根据屏幕分辨率更改这些点的位置,以便形状调整大小并居中,并且形状不会受到损坏。请帮我。

4

2 回答 2

2

您可以从Android - Supporting Multiple Screens文档DisplayMetrics中获取比例因子:

final float scale = getResources().getDisplayMetrics().density;

将所有 x 和 y 坐标相乘,scale并且您的点与屏幕密度无关。

为了使图像适合屏幕(或View可能),您可以获取视图的宽度高度。检查图像的宽度和高度并计算最大比例因子。

结合(乘以)两个比例因子,您的图像应该适合您的视图。

于 2012-12-21T10:10:26.573 回答
1

您可以使用 onMeasure 方法获取测量值,然后您可以从该位置开始绘画。我不知道下面的代码是否真正起作用,也许它需要优化。

protected void onDraw(Canvas canvas) {
        int height = getMeasuredHeight();
        int width = getMeasuredWidth();

        // Find the center
        px = width / 2;
        py = height / 2;

        canvas.drawColor(BACKGROUND);
        canvas.drawBitmap(mBitmap, 0, 0, null);
        canvas.drawPath(mPath, mPaint);

        // TODO remove if you dont want points to be drawn
        for (Point point : mPoints) {
            canvas.drawPoint(point.x + px, point.y + py, mPaint);
        }
    }




@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int measuredHeight = measureHeight(heightMeasureSpec);
        int measuredWidth = measureWidth(widthMeasureSpec);

        setMeasuredDimension(measuredHeight, measuredWidth);
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        // Default size if no limits are specified.
        int result = 500;

        if (specMode == MeasureSpec.AT_MOST) {
            // Calculate the ideal size of your
            // control within this maximum size.
            // If your control fills the available
            // space return the outer bound.
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {
            // If your control can fit within these bounds return that value.
            result = specSize;
        }
        return result;
    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        // Default size if no limits are specified.
        int result = 500;

        if (specMode == MeasureSpec.AT_MOST) {
            // Calculate the ideal size of your control
            // within this maximum size.
            // If your control fills the available space
            // return the outer bound.
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {
            // If your control can fit within these bounds return that value.
            result = specSize;
        }

        return result;
    }
于 2012-12-26T09:53:36.107 回答