(x1、y1)=(133,123)、(x2、y2)=(149,136)、(x3、y3)=(182,136)などのようにいくつかの点を配置すると、次のような形状になります。
次に、画面の解像度に従ってこれらのポイントの位置を変更して、形状のサイズが変更されて中央に配置され、形状が損傷しないようにします。私を助けてください。
(x1、y1)=(133,123)、(x2、y2)=(149,136)、(x3、y3)=(182,136)などのようにいくつかの点を配置すると、次のような形状になります。
次に、画面の解像度に従ってこれらのポイントの位置を変更して、形状のサイズが変更されて中央に配置され、形状が損傷しないようにします。私を助けてください。
Android - Supporting Multiple ScreensのドキュメントDisplayMetrics
に示されているように、から倍率を取得できます。
final float scale = getResources().getDisplayMetrics().density;
すべての x 座標と y 座標を乗算するscale
と、ポイントは画面密度に依存しません。
画像を画面 (またはView
おそらく) に合わせるには、View の幅と高さを取得します。画像の幅と高さを確認し、最大倍率を計算してください。
両方の倍率を組み合わせる (乗算する) と、画像がビューに収まるはずです。
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;
}