Contenu connexe Similaire à Introduction to Android Wear (20) Plus de Peter Friese (20) Introduction to Android Wear15. Intent viewIntent = new Intent(context, DummyActivity.class);
PendingIntent viewPendingIntent = PendingIntent.getActivity(context, 0, viewIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setSmallIcon(R.drawable.plane)
.setContentTitle(String.format("Flight AW123 is ready to board", notificationId))
.setContentText("Please proceed to gate C 17 to board. Have a nice flight!")
.setContentIntent(viewPendingIntent)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId++, notification);
sendNotification()
Simple Notifications
19. Intent viewIntent = new Intent(context, DummyActivity.class);
PendingIntent viewPendingIntent = PendingIntent.getActivity(context, 0, viewIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setSmallIcon(R.drawable.plane)
.setContentTitle(String.format("Flight AW123 is ready to board", notificationId))
.setContentText("Please proceed to gate C 17 to board. Have a nice flight!")
.setStyle(
new NotificationCompat.BigPictureStyle()
.bigPicture(BitmapFactory.decodeResource(context.getResources(),
R.drawable.sanfrancisco))
.setBigContentTitle("Flight AW123 is ready to board.")
.setSummaryText("Please proceed to gate C 17 to board. Have a nice flight!"))
.setContentIntent(viewPendingIntent)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId++, notification);
sendNotification()
BigPictureStyle
20. Intent viewIntent = new Intent(context, DummyActivity.class);
PendingIntent viewPendingIntent = PendingIntent.getActivity(context, 0, viewIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setSmallIcon(R.drawable.plane)
.setContentTitle(String.format("Flight AW123 is ready to board", notificationId))
.setContentText("Please proceed to gate C 17 to board. Have a nice flight!")
.setStyle(
new NotificationCompat.BigPictureStyle()
.bigPicture(BitmapFactory.decodeResource(context.getResources(),
R.drawable.sanfrancisco))
.setBigContentTitle("Flight AW123 is ready to board.")
.setSummaryText("Please proceed to gate C 17 to board. Have a nice flight!"))
.setContentIntent(viewPendingIntent)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId++, notification);
sendNotification()
BigPictureStyle
Create BigPictureStyle
22. ArrayList<Notification> pages = new ArrayList<Notification>();
pages.add(new NotificationCompat.Builder(context)
.setContentTitle("Your seat")
.setContentText("17A")
.extend(new NotificationCompat.WearableExtender()
.setBackground(BitmapFactory.decodeResource(context.getResources(),
R.drawable.a380_seat)))
.build());
sendNotification()
Pages
23. ArrayList<Notification> pages = new ArrayList<Notification>();
pages.add(new NotificationCompat.Builder(context)
.setContentTitle("Your seat")
.setContentText("17A")
.extend(new NotificationCompat.WearableExtender()
.setBackground(BitmapFactory.decodeResource(context.getResources(),
R.drawable.a380_seat)))
.build());
sendNotification()
Pages
Create page with title
24. ArrayList<Notification> pages = new ArrayList<Notification>();
pages.add(new NotificationCompat.Builder(context)
.setContentTitle("Your seat")
.setContentText("17A")
.extend(new NotificationCompat.WearableExtender()
.setBackground(BitmapFactory.decodeResource(context.getResources(),
R.drawable.a380_seat)))
.build());
sendNotification()
Pages
Set background image
27. ArrayList<Notification> pages = new ArrayList<Notification>();
pages.add(new NotificationCompat.Builder(context)
// ... (set properties)
.build());
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.plane)
.setContentTitle(String.format("Flight AW123 is ready to board", notificationId))
.setContentIntent(viewPendingIntent)
.extend(new NotificationCompat.WearableExtender()
.addPages(pages))
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId++, notification);
sendNotification()
Adding Pages to Notifications
28. ArrayList<Notification> pages = new ArrayList<Notification>();
pages.add(new NotificationCompat.Builder(context)
// ... (set properties)
.build());
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.plane)
.setContentTitle(String.format("Flight AW123 is ready to board", notificationId))
.setContentIntent(viewPendingIntent)
.extend(new NotificationCompat.WearableExtender()
.addPages(pages))
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId++, notification);
sendNotification()
Adding Pages to Notifications
Build pages
29. ArrayList<Notification> pages = new ArrayList<Notification>();
pages.add(new NotificationCompat.Builder(context)
// ... (set properties)
.build());
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.plane)
.setContentTitle(String.format("Flight AW123 is ready to board", notificationId))
.setContentIntent(viewPendingIntent)
.extend(new NotificationCompat.WearableExtender()
.addPages(pages))
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId++, notification);
sendNotification()
Adding Pages to Notifications
Add pages to notification
31. // Feedback intent
Intent replyIntent = new Intent(context, DummyActivity.class);
PendingIntent replyPendingIntent = PendingIntent.getActivity(context, 0, replyIntent, 0);
String replyLabel = context.getResources().getString(R.string.reply_label);
String[] cannedResponses = context.getResources().getStringArray(R.array.canned_responses);
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(replyLabel)
.setChoices(cannedResponses)
.build();
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(
R.drawable.chatbubble_working,
replyLabel,
replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setSmallIcon(R.drawable.plane)
sendNotification()
Voice Input
32. // Feedback intent
Intent replyIntent = new Intent(context, DummyActivity.class);
PendingIntent replyPendingIntent = PendingIntent.getActivity(context, 0, replyIntent, 0);
String replyLabel = context.getResources().getString(R.string.reply_label);
String[] cannedResponses = context.getResources().getStringArray(R.array.canned_responses);
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(replyLabel)
.setChoices(cannedResponses)
.build();
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(
R.drawable.chatbubble_working,
replyLabel,
replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setSmallIcon(R.drawable.plane)
sendNotification()
Voice Input
Create pending intent
33. // Feedback intent
Intent replyIntent = new Intent(context, DummyActivity.class);
PendingIntent replyPendingIntent = PendingIntent.getActivity(context, 0, replyIntent, 0);
String replyLabel = context.getResources().getString(R.string.reply_label);
String[] cannedResponses = context.getResources().getStringArray(R.array.canned_responses);
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(replyLabel)
.setChoices(cannedResponses)
.build();
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(
R.drawable.chatbubble_working,
replyLabel,
replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setSmallIcon(R.drawable.plane)
sendNotification()
Voice Input
Create RemoteInput with canned responses
34. // Feedback intent
Intent replyIntent = new Intent(context, DummyActivity.class);
PendingIntent replyPendingIntent = PendingIntent.getActivity(context, 0, replyIntent, 0);
String replyLabel = context.getResources().getString(R.string.reply_label);
String[] cannedResponses = context.getResources().getStringArray(R.array.canned_responses);
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(replyLabel)
.setChoices(cannedResponses)
.build();
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(
R.drawable.chatbubble_working,
replyLabel,
replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setSmallIcon(R.drawable.plane)
sendNotification()
Voice Input
Create wearable action
35. String[] cannedResponses = context.getResources().getStringArray(R.array.canned_responses);
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(replyLabel)
.setChoices(cannedResponses)
.build();
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(
R.drawable.chatbubble_working,
replyLabel,
replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setSmallIcon(R.drawable.plane)
.setContentTitle(String.format("Flight AW123 is ready to board", notificationId))
.setContentText("Please proceed to gate C 17 to board. Have a nice flight!")
.extend(new NotificationCompat.WearableExtender()
.addPages(pages)
.addAction(replyAction))
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId++, notification);
sendNotification()
Voice Input
Create notification
36. String[] cannedResponses = context.getResources().getStringArray(R.array.canned_responses);
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(replyLabel)
.setChoices(cannedResponses)
.build();
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(
R.drawable.chatbubble_working,
replyLabel,
replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setSmallIcon(R.drawable.plane)
.setContentTitle(String.format("Flight AW123 is ready to board", notificationId))
.setContentText("Please proceed to gate C 17 to board. Have a nice flight!")
.extend(new NotificationCompat.WearableExtender()
.addPages(pages)
.addAction(replyAction))
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId++, notification);
sendNotification()
Voice Input
Send notification
37. Intent intent = getIntent();
if (intent != null) {
Bundle remoteInputResults = RemoteInput.getResultsFromIntent(intent);
if (remoteInputResults != null) {
CharSequence utterance =
remoteInputResults.getCharSequence(Constants.EXTRA_VOICE_REPLY);
Toast.makeText(this, utterance, Toast.LENGTH_LONG).show();
}
}
ReceivingActivity.onCreate()
Receiving Voice Input
38. Intent intent = getIntent();
if (intent != null) {
Bundle remoteInputResults = RemoteInput.getResultsFromIntent(intent);
if (remoteInputResults != null) {
CharSequence utterance =
remoteInputResults.getCharSequence(Constants.EXTRA_VOICE_REPLY);
Toast.makeText(this, utterance, Toast.LENGTH_LONG).show();
}
}
ReceivingActivity.onCreate()
Receiving Voice Input
Get remote input
39. Intent intent = getIntent();
if (intent != null) {
Bundle remoteInputResults = RemoteInput.getResultsFromIntent(intent);
if (remoteInputResults != null) {
CharSequence utterance =
remoteInputResults.getCharSequence(Constants.EXTRA_VOICE_REPLY);
Toast.makeText(this, utterance, Toast.LENGTH_LONG).show();
}
}
ReceivingActivity.onCreate()
Receiving Voice Input
Unpack voice reply
41. Intent mapIntent = new Intent(Intent.ACTION_VIEW);
Uri geoUri = Uri.parse("geo:0,0?q=" + Uri.encode("London Heathrow"));
mapIntent.setData(geoUri);
PendingIntent mapPendingIntent = PendingIntent.getActivity(context, 0, mapIntent, 0);
sendNotification()
Actions
42. Intent mapIntent = new Intent(Intent.ACTION_VIEW);
Uri geoUri = Uri.parse("geo:0,0?q=" + Uri.encode("London Heathrow"));
mapIntent.setData(geoUri);
PendingIntent mapPendingIntent = PendingIntent.getActivity(context, 0, mapIntent, 0);
NotificationCompat.Action walkingDirectionsAction =
new NotificationCompat.Action.Builder(
R.drawable.ic_full_directions_walking, "Directions to gate", mapPendingIntent)
.build();
sendNotification()
Actions
43. Intent mapIntent = new Intent(Intent.ACTION_VIEW);
Uri geoUri = Uri.parse("geo:0,0?q=" + Uri.encode("London Heathrow"));
mapIntent.setData(geoUri);
PendingIntent mapPendingIntent = PendingIntent.getActivity(context, 0, mapIntent, 0);
NotificationCompat.Action walkingDirectionsAction =
new NotificationCompat.Action.Builder(
R.drawable.ic_full_directions_walking, "Directions to gate", mapPendingIntent)
.build();
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setSmallIcon(R.drawable.plane)
.setContentTitle(String.format("Flight AW123 is ready to board", notificationId))
.setContentText("Please proceed to gate C 17 to board. Have a nice flight!")
.addAction(walkingDirectionsAction)
.extend(new NotificationCompat.WearableExtender()
.addPages(pages)
.addAction(replyAction)
.addAction(walkingDirectionsAction))
.build();
sendNotification()
Actions
51. <?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.wearable.view.CircledImageView
android:id="@+id/image"
android:alpha="0.5"
android:layout_height="52dp"
android:layout_marginLeft="16dp"
android:layout_width="52dp"
app:circle_border_color="#FFFFFFFF"
app:circle_border_width="2dp"
app:circle_color="#00000000"
/>
<TextView
android:id="@+id/text"
android:alpha="0.5"
android:fontFamily="sans-serif-condensed-light"
android:gravity="center_vertical"
android:layout_height="52dp"
android:layout_marginLeft="72dp"
checkin_listview_item.xml
Layout - Item
52. <?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.wearable.view.CircledImageView
android:id="@+id/image"
android:alpha="0.5"
android:layout_height="52dp"
android:layout_marginLeft="16dp"
android:layout_width="52dp"
app:circle_border_color="#FFFFFFFF"
app:circle_border_width="2dp"
app:circle_color="#00000000"
/>
<TextView
android:id="@+id/text"
android:alpha="0.5"
android:fontFamily="sans-serif-condensed-light"
android:gravity="center_vertical"
android:layout_height="52dp"
android:layout_marginLeft="72dp"
android:layout_marginRight="16dp"
android:layout_width="wrap_content"
android:textColor="@color/white"
android:textSize="14sp"
/>
</merge>
Layout - Item
checkin_listview_item.xml
53. private final class MyItemView extends FrameLayout implements
WearableListView.OnCenterProximityListener {
final CircledImageView image;
final TextView text;
public MyItemView(Context context) {
super(context);
View.inflate(context, R.layout.checkin_listview_item, this);
image = (CircledImageView) findViewById(R.id.image);
text = (TextView) findViewById(R.id.text);
}
@Override
public void onCenterPosition(boolean b) {
image.animate().scaleX(1f).scaleY(1f).alpha(1);
text.animate().scaleX(1f).scaleY(1f).alpha(1);
}
@Override
public void onNonCenterPosition(boolean b) {
image.animate().scaleX(0.8f).scaleY(0.8f).alpha(0.6f);
CheckInActivity.java
MyViewItem
54. private final class MyItemView extends FrameLayout implements
WearableListView.OnCenterProximityListener {
final CircledImageView image;
final TextView text;
public MyItemView(Context context) {
super(context);
View.inflate(context, R.layout.checkin_listview_item, this);
image = (CircledImageView) findViewById(R.id.image);
text = (TextView) findViewById(R.id.text);
}
@Override
public void onCenterPosition(boolean b) {
image.animate().scaleX(1f).scaleY(1f).alpha(1);
text.animate().scaleX(1f).scaleY(1f).alpha(1);
}
@Override
public void onNonCenterPosition(boolean b) {
image.animate().scaleX(0.8f).scaleY(0.8f).alpha(0.6f);
CheckInActivity.java
MyViewItem
55. private final class MyItemView extends FrameLayout implements
WearableListView.OnCenterProximityListener {
final CircledImageView image;
final TextView text;
public MyItemView(Context context) {
super(context);
View.inflate(context, R.layout.checkin_listview_item, this);
image = (CircledImageView) findViewById(R.id.image);
text = (TextView) findViewById(R.id.text);
}
@Override
public void onCenterPosition(boolean b) {
image.animate().scaleX(1f).scaleY(1f).alpha(1);
text.animate().scaleX(1f).scaleY(1f).alpha(1);
}
@Override
public void onNonCenterPosition(boolean b) {
image.animate().scaleX(0.8f).scaleY(0.8f).alpha(0.6f);
text.animate().scaleX(0.8f).scaleY(0.8f).alpha(0.6f);
}
}
CheckInActivity.java
MyViewItem
56. public class CheckInActivity extends Activity implements WearableListView.ClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkin);
MyListAdapter adapter = new MyListAdapter();
WearableListView listView = (WearableListView) findViewById(R.id.checkin_list);
listView.setAdapter(adapter);
listView.setClickListener(CheckInActivity.this);
}
@Override
public void onClick(WearableListView.ViewHolder viewHolder) {
Toast.makeText(this, String.format("You selected item #%s",
viewHolder.getPosition()), Toast.LENGTH_SHORT).show();
}
@Override
public void onTopEmptyRegionClick() {
CheckInActivity.java
Activity
57. public class CheckInActivity extends Activity implements WearableListView.ClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkin);
MyListAdapter adapter = new MyListAdapter();
WearableListView listView = (WearableListView) findViewById(R.id.checkin_list);
listView.setAdapter(adapter);
listView.setClickListener(CheckInActivity.this);
}
@Override
public void onClick(WearableListView.ViewHolder viewHolder) {
Toast.makeText(this, String.format("You selected item #%s",
viewHolder.getPosition()), Toast.LENGTH_SHORT).show();
}
@Override
public void onTopEmptyRegionClick() {
Toast.makeText(this, "You tapped into the empty area above the list",
Toast.LENGTH_SHORT).show();
}
CheckInActivity.java
Activity
62. • Square vs round
• Ambient mode / low-bit
Design Principles
63. • Square vs round
• Ambient mode / low-bit
• Legibility
Design Principles
56° - Cloudy
56° - Cloudy
66. • Start using a sample
• Add a new wearable module
Developing
Watch Faces
70. @Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
setWatchFaceStyle(new WatchFaceStyle.Builder(AnalogWatchFaceService.this)
.setCardPeekMode(WatchFaceStyle.PEEK_MODE_SHORT)
.setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
.setShowSystemUiTime(false)
.build());
Resources resources = AnalogWatchFaceService.this.getResources();
Drawable backgroundDrawable = resources.getDrawable(R.drawable.bg);
mBackgroundBitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();
mHourPaint = new Paint();
mHourPaint.setARGB(255, 200, 200, 200);
mHourPaint.setStrokeWidth(5.f);
mHourPaint.setAntiAlias(true);
mHourPaint.setStrokeCap(Paint.Cap.ROUND);
onCreate
Lifecycle - initialise watch face elements
71. @Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
setWatchFaceStyle(new WatchFaceStyle.Builder(AnalogWatchFaceService.this)
.setCardPeekMode(WatchFaceStyle.PEEK_MODE_SHORT)
.setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
.setShowSystemUiTime(false)
.build());
Resources resources = AnalogWatchFaceService.this.getResources();
Drawable backgroundDrawable = resources.getDrawable(R.drawable.bg);
mBackgroundBitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();
mHourPaint = new Paint();
mHourPaint.setARGB(255, 200, 200, 200);
mHourPaint.setStrokeWidth(5.f);
mHourPaint.setAntiAlias(true);
mHourPaint.setStrokeCap(Paint.Cap.ROUND);
onCreate
Lifecycle - initialise watch face elements
Single-line peek card
Do not show system time
Show background briefly for
interruptive cards
72. @Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
setWatchFaceStyle(new WatchFaceStyle.Builder(AnalogWatchFaceService.this)
.setCardPeekMode(WatchFaceStyle.PEEK_MODE_SHORT)
.setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
.setShowSystemUiTime(false)
.build());
Resources resources = AnalogWatchFaceService.this.getResources();
Drawable backgroundDrawable = resources.getDrawable(R.drawable.bg);
mBackgroundBitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();
mHourPaint = new Paint();
mHourPaint.setARGB(255, 200, 200, 200);
mHourPaint.setStrokeWidth(5.f);
mHourPaint.setAntiAlias(true);
mHourPaint.setStrokeCap(Paint.Cap.ROUND);
onCreate
Lifecycle - initialise watch face elements
Load background image
73. @Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
setWatchFaceStyle(new WatchFaceStyle.Builder(AnalogWatchFaceService.this)
.setCardPeekMode(WatchFaceStyle.PEEK_MODE_SHORT)
.setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
.setShowSystemUiTime(false)
.build());
Resources resources = AnalogWatchFaceService.this.getResources();
Drawable backgroundDrawable = resources.getDrawable(R.drawable.bg);
mBackgroundBitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();
mHourPaint = new Paint();
mHourPaint.setARGB(255, 200, 200, 200);
mHourPaint.setStrokeWidth(5.f);
mHourPaint.setAntiAlias(true);
mHourPaint.setStrokeCap(Paint.Cap.ROUND);
onCreate
Lifecycle - initialise watch face elements
Create styles for graphic objects
74. @Override
public void onDraw(Canvas canvas, Rect bounds) {
mTime.setToNow();
canvas.drawBitmap(mBackgroundScaledBitmap, 0, 0, null);
float centerX = width / 2f;
float centerY = height / 2f;
float innerTickRadius = centerX - 10;
float outerTickRadius = centerX;
for (int tickIndex = 0; tickIndex < 12; tickIndex++) {
float tickRot = (float) (tickIndex * Math.PI * 2 / 12);
float innerX = (float) Math.sin(tickRot) * innerTickRadius;
float innerY = (float) -Math.cos(tickRot) * innerTickRadius;
float outerX = (float) Math.sin(tickRot) * outerTickRadius;
float outerY = (float) -Math.cos(tickRot) * outerTickRadius;
canvas.drawLine(centerX + innerX, centerY + innerY,
centerX + outerX, centerY + outerY, mTickPaint);
}
float secRot = mTime.second / 30f * (float) Math.PI;
onDraw
Lifecycle - draw the watch face
75. @Override
public void onDraw(Canvas canvas, Rect bounds) {
mTime.setToNow();
canvas.drawBitmap(mBackgroundScaledBitmap, 0, 0, null);
float centerX = width / 2f;
float centerY = height / 2f;
float innerTickRadius = centerX - 10;
float outerTickRadius = centerX;
for (int tickIndex = 0; tickIndex < 12; tickIndex++) {
float tickRot = (float) (tickIndex * Math.PI * 2 / 12);
float innerX = (float) Math.sin(tickRot) * innerTickRadius;
float innerY = (float) -Math.cos(tickRot) * innerTickRadius;
float outerX = (float) Math.sin(tickRot) * outerTickRadius;
float outerY = (float) -Math.cos(tickRot) * outerTickRadius;
canvas.drawLine(centerX + innerX, centerY + innerY,
centerX + outerX, centerY + outerY, mTickPaint);
}
float secRot = mTime.second / 30f * (float) Math.PI;
onDraw
Lifecycle - draw the watch face
Update time
Draw background image
76. @Override
public void onDraw(Canvas canvas, Rect bounds) {
mTime.setToNow();
canvas.drawBitmap(mBackgroundScaledBitmap, 0, 0, null);
float centerX = width / 2f;
float centerY = height / 2f;
float innerTickRadius = centerX - 10;
float outerTickRadius = centerX;
for (int tickIndex = 0; tickIndex < 12; tickIndex++) {
float tickRot = (float) (tickIndex * Math.PI * 2 / 12);
float innerX = (float) Math.sin(tickRot) * innerTickRadius;
float innerY = (float) -Math.cos(tickRot) * innerTickRadius;
float outerX = (float) Math.sin(tickRot) * outerTickRadius;
float outerY = (float) -Math.cos(tickRot) * outerTickRadius;
canvas.drawLine(centerX + innerX, centerY + innerY,
centerX + outerX, centerY + outerY, mTickPaint);
}
float secRot = mTime.second / 30f * (float) Math.PI;
onDraw
Lifecycle - draw the watch face
Draw the ticks
77. float centerX = width / 2f;
float centerY = height / 2f;
float innerTickRadius = centerX - 10;
float outerTickRadius = centerX;
for (int tickIndex = 0; tickIndex < 12; tickIndex++) {
float tickRot = (float) (tickIndex * Math.PI * 2 / 12);
float innerX = (float) Math.sin(tickRot) * innerTickRadius;
float innerY = (float) -Math.cos(tickRot) * innerTickRadius;
float outerX = (float) Math.sin(tickRot) * outerTickRadius;
float outerY = (float) -Math.cos(tickRot) * outerTickRadius;
canvas.drawLine(centerX + innerX, centerY + innerY,
centerX + outerX, centerY + outerY, mTickPaint);
}
float secRot = mTime.second / 30f * (float) Math.PI;
int minutes = mTime.minute;
float minRot = minutes / 30f * (float) Math.PI;
float hrRot = ((mTime.hour + (minutes / 60f)) / 6f ) * (float) Math.PI;
float secLength = centerX - 20;
float minLength = centerX - 40;
float hrLength = centerX - 80;
if (!isInAmbientMode()) {
float secX = (float) Math.sin(secRot) * secLength;
float secY = (float) -Math.cos(secRot) * secLength;
canvas.drawLine(centerX, centerY, centerX + secX, centerY + secY, mSecondPaint);
}
onDraw
Lifecycle - draw the watch face
Determine hands geometry
78. float outerX = (float) Math.sin(tickRot) * outerTickRadius;
float outerY = (float) -Math.cos(tickRot) * outerTickRadius;
canvas.drawLine(centerX + innerX, centerY + innerY,
centerX + outerX, centerY + outerY, mTickPaint);
}
float secRot = mTime.second / 30f * (float) Math.PI;
int minutes = mTime.minute;
float minRot = minutes / 30f * (float) Math.PI;
float hrRot = ((mTime.hour + (minutes / 60f)) / 6f ) * (float) Math.PI;
float secLength = centerX - 20;
float minLength = centerX - 40;
float hrLength = centerX - 80;
if (!isInAmbientMode()) {
float secX = (float) Math.sin(secRot) * secLength;
float secY = (float) -Math.cos(secRot) * secLength;
canvas.drawLine(centerX, centerY, centerX + secX, centerY + secY, mSecondPaint);
}
float minX = (float) Math.sin(minRot) * minLength;
float minY = (float) -Math.cos(minRot) * minLength;
canvas.drawLine(centerX, centerY, centerX + minX, centerY + minY, mMinutePaint);
float hrX = (float) Math.sin(hrRot) * hrLength;
float hrY = (float) -Math.cos(hrRot) * hrLength;
canvas.drawLine(centerX, centerY, centerX + hrX, centerY + hrY, mHourPaint);
}
onDraw
Lifecycle - draw the watch face
Don’t draw seconds when in
ambient mode
Draw minutes hand
Draw hours hand