0
我有一個自定義視圖,該繪製方法應該繪製一個覆蓋屏幕的紅色橢圓,但沒有任何事情發生。我檢查了我的自定義視圖的繪製方法被調用,它是。繪製方法不繪製任何東西
這是類:
public class CustomView extends View {
Paint red = new Paint();
RectF ovalRec = new RectF(0,0,MainActivity.dUWidth,MainActivity.dUHeight);
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
red.setARGB(255, 255, 0, 0);
}
protected void onDraw(Canvas canvas) {
Log.i("log_tag", "DRAWING THE OVAL");
canvas.drawOval(ovalRec, red);
super.onDraw(canvas);
}
}
在這裏你可以看到什麼dUWidth和dUHeight是。
public class MainActivity extends AppCompatActivity {
public static int dUWidth;
public static int dUHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Auto-gen code
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting height & width of USABLE display and setting it to Point variable.
Display display = getWindowManager().getDefaultDisplay();
Point sizeOfDisplay = new Point();
display.getSize(sizeOfDisplay);
dUWidth = sizeOfDisplay.x;
dUHeight = sizeOfDisplay.y;
}
這裏是它在XML聲明:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<com.XXX.targetexercise.CustomView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
我在我的繪製方法的末尾放置了一個invalidate(),所以它會被重複調用。但屏幕仍然是白色的。也許是因爲視圖失效太快,我看不到橢圓形? – Roymunson
您剛剛重複失效,但ovelRec仍然是(0,0,0,0)。你必須更新ovelRec然後調用invalidate() –