Mobile Computing Systems


(a) Canvas of a Custom View


Download 10.9 Kb.
bet4/4
Sana18.06.2023
Hajmi10.9 Kb.
#1563379
1   2   3   4

2(a) Canvas of a Custom View

  • Good for low frame-rate applications (e.g. chess or snake game).
  • You define a new View and add it in the layout XML file (like you do for a TextView, ImageView etc.)
  • Android provides you the Canvas as you extend a View and override its onDraw() method.
  • To request a redraw, use: invalidate(). Outside main Activity’s thread, use postInvalidate().

2(a) Canvas of a Custom View


public class CustomView extends View {
//Declare all four types of constructors here. @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);
//Use canvas.draw*() }
}
<edu.unc.nirjon.projectname.CustomView android:id="@+id/mycustomview" android:layout_width="match_parent" android:layout_height="match_parent" />

Code Practice:

  • Create 2 Buttons: Up and Down
  • Create a Custom View
    • Draw a circle at location (X, Y)
  • Every time the buttons are clicked, the point will move. (Hint: use invalidate() to force redraw).

Download 10.9 Kb.

Do'stlaringiz bilan baham:
1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling