2017-01-07 3 views
0

私は独自のカスタムビューを作成しようとしていますが、onMeasureは2つの値を示しています。 カスタムビューandroid onMeasureメソッドは、レイアウトに設定された値の2倍を表示します。

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
     int parentHeight = MeasureSpec.getSize(heightMeasureSpec);//shows 200 
     this.setMeasuredDimension(parentWidth, parentHeight); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 

私はparentHeightまたはparentWidthログ

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:custom="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<com.customview.PercentageCircle 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    custom:currentValue="0" 
    custom:maxValue="100" 
    custom:thickNess="80" 
    custom:fillBackgroundColor="@color/light_gray" 
    custom:fillColor="@color/red" 
    /> 

は、私は二重の指定value.I得ることの原因を理解することはできませんよ。

+0

あなたは 'android:layout_width =" match_parent "'を持っていますので、どのように「倍増」することができますか? – pskink

+1

dpに100dpを設定すると、ピクセル値が表示されます。 –

+0

はい、match_parentとwrap_contentが2倍になります。 –

答えて

0

Canvas関数への入力はすべてピクセル単位です。 ファンクションパラメータonMeasureもピクセル単位です。 デバイス間でUIを操作するピクセルを使用する場合、高解像度の画面では小さすぎます。

フロートheightInPixel =のgetHeight()/ getResources()、画素にDPへ変換する

getDisplayMetrics()密度。

関連する問題