私は円を描くカスタムビューを作成しました。 xmlから円の数が必要です。親ビューに従ってサイズ変更
は、例えば、その画面全体に10円を生成します。
<com.dd.view.MyShape
android:layout_width="match_parent"
android:layout_height="60dp"
app:shape_count="10"/>
<LinearLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.dd.view.MyShape
android:layout_width="100dp"
android:layout_height="60dp"
app:shape_count="3"/>
</LinearLayout>
しかし、私は小さなレイアウトにこのビューを入れると、円はビューの幅に従って生成されます。親ビューに従って生成したい。
onMeasureメソッドをオーバーライドしようとしましたが、正しくできませんでした。
そして、ここに私のonDraw方法:あなたの答えのための
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int totalWidth=getMeasuredWidth();
int major = totalWidth/circleCount;
int radius = major/2;
float startPoint = totalWidth/(circleCount * 2);
for (int i = 0; i < circleCount; i++) {
if (i % 2 == 0) paint.setColor(Color.GREEN);
else paint.setColor(Color.BLUE);
canvas.drawCircle(startPoint + major * i, radius,radius, paint);
}
}
おかげで今、それは次のようになります。
ありがとうございますが、私はJavaソリューションを探しています。私が必要とするのは親のサイズを見つけることだけです – user3792834