1. Are you ready for the Galaxy S20? Here is everything we know so far!

png as Ambient Background Not Showing

Discussion in 'Android Development' started by WolfSpyryt, Dec 3, 2020.

  1. WolfSpyryt

    WolfSpyryt Lurker
    Thread Starter

    I am attempting to use bdg.png as an ambient background. Can anybody assist me with it not showing? Thank you in advance for any help.

    Code (Java):
    1. import android.content.BroadcastReceiver;
    2. import android.content.Context;
    3. import android.content.Intent;
    4. import android.content.IntentFilter;
    5. import android.graphics.Bitmap;
    6. import android.graphics.BitmapFactory;
    7. import android.graphics.Canvas;
    8. import android.graphics.Color;
    9. import android.graphics.ColorMatrix;
    10. import android.graphics.ColorMatrixColorFilter;
    11. import android.graphics.Paint;
    12. import android.graphics.Rect;
    13. import android.os.Build;
    14. import android.os.Bundle;
    15. import android.os.Handler;
    16. import android.os.Message;
    17.  
    18. import android.support.wearable.watchface.CanvasWatchFaceService;
    19. import android.support.wearable.watchface.WatchFaceService;
    20. import android.support.wearable.watchface.WatchFaceStyle;
    21. import android.view.SurfaceHolder;
    22. import android.view.View;
    23.  
    24.  
    25. import java.lang.ref.WeakReference;
    26. import java.util.Calendar;
    27. import java.util.TimeZone;
    28. import java.util.concurrent.TimeUnit;
    29.  
    30. import static android.view.View.LAYER_TYPE_SOFTWARE;
    31.  
    32.  
    33. public class MyWatchFace extends CanvasWatchFaceService {
    34.  
    35. /*
    36. * Updates rate in milliseconds for interactive mode. We update once a second to advance the
    37. * second hand.
    38. */
    39. private static final long INTERACTIVE_UPDATE_RATE_MS = TimeUnit.SECONDS.toMillis(1);
    40.  
    41. /**
    42. * Handler message id for updating the time periodically in interactive mode.
    43. */
    44. private static final int MSG_UPDATE_TIME = 0;
    45.  
    46. [USER=1021285]@override[/USER]
    47. public Engine onCreateEngine() {
    48. return new Engine();
    49. }
    50.  
    51. private static class EngineHandler extends Handler {
    52. private final WeakReference<MyWatchFace.Engine> mWeakReference;
    53.  
    54. public EngineHandler(MyWatchFace.Engine reference) {
    55. mWeakReference = new WeakReference<>(reference);
    56. }
    57.  
    58. [USER=1021285]@override[/USER]
    59. public void handleMessage(Message msg) {
    60. MyWatchFace.Engine engine = mWeakReference.get();
    61. if (engine != null) {
    62. switch (msg.what) {
    63. case MSG_UPDATE_TIME:
    64. engine.handleUpdateTimeMessage();
    65. break;
    66. }
    67. }
    68. }
    69. }
    70.  
    71. private class Engine extends CanvasWatchFaceService.Engine {
    72.  
    73.  
    74. /* Handler to update the time once a second in interactive mode. */
    75. private final Handler mUpdateTimeHandler = new EngineHandler(this);
    76. private Calendar mCalendar;
    77. private final BroadcastReceiver mTimeZoneReceiver = new BroadcastReceiver() {
    78. [USER=1021285]@override[/USER]
    79. public void onReceive(Context context, Intent intent) {
    80. mCalendar.setTimeZone(TimeZone.getDefault());
    81. invalidate();
    82. }
    83. };
    84. private boolean mRegisteredTimeZoneReceiver = false;
    85. private boolean mMuteMode;
    86. private float mCenterX;
    87. private float mCenterY;
    88.  
    89. /* Colors for all hands (hour, minute, seconds, ticks) based on photo loaded. */
    90.  
    91. private Bitmap mHourPaint;
    92. private Bitmap mHoursPaint;
    93. private Paint paint;
    94. private Bitmap mMinutesPaint;
    95. private Bitmap mMinutePaint;
    96. private Bitmap mSecondsPaint;
    97. private Bitmap mSecondPaint;
    98. private Paint mBackgroundPaint;
    99. private Bitmap mBackgroundBitmap;
    100. private Bitmap mGrayBackgroundBitmap;
    101. private boolean mLowBitAmbient;
    102. private boolean mBurnInProtection;
    103.  
    104.  
    105. [USER=1021285]@override[/USER]
    106. public void onCreate(SurfaceHolder holder) {
    107. super.onCreate(holder);
    108.  
    109. setWatchFaceStyle(new WatchFaceStyle.Builder(MyWatchFace.this)
    110. .setAcceptsTapEvents(true)
    111. .setHideStatusBar(true)
    112. .build());
    113.  
    114. mCalendar = Calendar.getInstance();
    115.  
    116. initializeBackground();
    117. initializeWatchFace();
    118. }
    119.  
    120. private void initializeBackground() {
    121. mBackgroundPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
    122. //mBackgroundPaint.setColor(Color.BLACK);
    123. mBackgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
    124. mGrayBackgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bgd);
    125.  
    126.  
    127.  
    128. }
    129.  
    130. private void initializeWatchFace() {
    131. /* Set defaults for colors */
    132.  
    133. paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    134. paint.setAntiAlias(true);
    135. paint.setShadowLayer(0, 0.0f, 0.0f, Color.BLACK);
    136.  
    137. mHoursPaint = BitmapFactory.decodeResource(getResources(), R.drawable.hourssh);
    138. mHourPaint = BitmapFactory.decodeResource(getResources(), R.drawable.hours);
    139. mMinutesPaint = BitmapFactory.decodeResource(getResources(), R.drawable.minutessh);
    140. mMinutePaint = BitmapFactory.decodeResource(getResources(), R.drawable.minutes);
    141. mSecondsPaint = BitmapFactory.decodeResource(getResources(), R.drawable.secondssh);
    142. mSecondPaint = BitmapFactory.decodeResource(getResources(), R.drawable.seconds);
    143.  
    144. }
    145.  
    146. [USER=1021285]@override[/USER]
    147. public void onDestroy() {
    148. mUpdateTimeHandler.removeMessages(MSG_UPDATE_TIME);
    149. super.onDestroy();
    150. }
    151.  
    152. [USER=1021285]@override[/USER]
    153. public void onPropertiesChanged(Bundle properties) {
    154. super.onPropertiesChanged(properties);
    155. mLowBitAmbient = properties.getBoolean(PROPERTY_LOW_BIT_AMBIENT, false);
    156. mBurnInProtection = properties.getBoolean(PROPERTY_BURN_IN_PROTECTION, false);
    157. }
    158.  
    159. [USER=1021285]@override[/USER]
    160. public void onTimeTick() {
    161. super.onTimeTick();
    162. invalidate();
    163. }
    164.  
    165. [USER=1021285]@override[/USER]
    166. public void onAmbientModeChanged(boolean inAmbientMode) {
    167. super.onAmbientModeChanged(inAmbientMode);
    168.  
    169. updateWatchHandStyle();
    170.  
    171. /* Check and trigger whether or not timer should be running (only in active mode). */
    172. updateTimer();
    173. }
    174.  
    175. private void updateWatchHandStyle() {
    176.  
    177.  
    178. }
    179.  
    180. [USER=1021285]@override[/USER]
    181. public void onInterruptionFilterChanged(int interruptionFilter) {
    182. super.onInterruptionFilterChanged(interruptionFilter);
    183. boolean inMuteMode = (interruptionFilter == WatchFaceService.INTERRUPTION_FILTER_NONE);
    184.  
    185. /* Dim display in mute mode. */
    186. if (mMuteMode != inMuteMode) {
    187. mMuteMode = inMuteMode;
    188. // mHourPaint.setAlpha(inMuteMode ? 100 : 255);
    189. // mMinutePaint.setAlpha(inMuteMode ? 100 : 255);
    190. // mSecondPaint.setAlpha(inMuteMode ? 80 : 255);
    191. invalidate();
    192. }
    193. }
    194.  
    195. [USER=1021285]@override[/USER]
    196. public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    197. super.onSurfaceChanged(holder, format, width, height);
    198.  
    199. /*
    200. * Find the coordinates of the center point on the screen, and ignore the window
    201. * insets, so that, on round watches with a "chin", the watch face is centered on the
    202. * entire screen, not just the usable portion.
    203. */
    204. mCenterX = width / 2f;
    205. mCenterY = height / 2f;
    206.  
    207. /* Scale loaded background image (more efficient) if surface dimensions change. */
    208. float scaleHoursBitmap = height / (float) mHoursPaint.getHeight();
    209. float scaleHourBitmap = height / (float) mHourPaint.getHeight();
    210. float scaleMinsBitmap = height / (float) mMinutesPaint.getHeight();
    211. float scaleMinBitmap = height / (float) mMinutePaint.getHeight();
    212. float scaleSecondsBitmap = height / (float) mSecondsPaint.getHeight();
    213. float scaleSecondBitmap = height / (float) mSecondPaint.getHeight();
    214.  
    215. mHoursPaint = Bitmap.createScaledBitmap(mHoursPaint,
    216. (int) (mHoursPaint.getWidth() * scaleHoursBitmap),
    217. (int) (mHoursPaint.getHeight() * scaleHoursBitmap), true);
    218.  
    219. mHourPaint = Bitmap.createScaledBitmap(mHourPaint,
    220. (int) (mHourPaint.getWidth() * scaleHourBitmap),
    221. (int) (mHourPaint.getHeight() * scaleHourBitmap), true);
    222.  
    223. mMinutesPaint = Bitmap.createScaledBitmap(mMinutesPaint,
    224. (int) (mMinutesPaint.getWidth() * scaleMinsBitmap),
    225. (int) (mMinutesPaint.getHeight() * scaleMinsBitmap), true);
    226.  
    227. mMinutePaint = Bitmap.createScaledBitmap(mMinutePaint,
    228. (int) (mMinutePaint.getWidth() * scaleMinBitmap),
    229. (int) (mMinutePaint.getHeight() * scaleMinBitmap), true);
    230.  
    231. mSecondsPaint = Bitmap.createScaledBitmap(mSecondsPaint,
    232. (int) (mSecondsPaint.getWidth() * scaleSecondsBitmap),
    233. (int) (mSecondsPaint.getHeight() * scaleSecondsBitmap), true);
    234.  
    235. mSecondPaint = Bitmap.createScaledBitmap(mSecondPaint,
    236. (int) (mSecondPaint.getWidth() * scaleSecondBitmap),
    237. (int) (mSecondPaint.getHeight() * scaleSecondBitmap), true);
    238.  
    239.  
    240. /* Scale loaded background image (more efficient) if surface dimensions change. */
    241. float scale = ((float) width) / (float) mBackgroundBitmap.getWidth();
    242.  
    243. mBackgroundBitmap = Bitmap.createScaledBitmap(mBackgroundBitmap,
    244. (int) (mBackgroundBitmap.getWidth() * scale),
    245. (int) (mBackgroundBitmap.getHeight() * scale), true);
    246.  
    247. /*
    248. * Create a gray version of the image only if it will look nice on the device in
    249. * ambient mode. That means we don't want devices that support burn-in
    250. * protection (slight movements in pixels, not great for images going all the way to
    251. * edges) and low ambient mode (degrades image quality).
    252. *
    253. * Also, if your watch face will know about all images ahead of time (users aren't
    254. * selecting their own photos for the watch face), it will be more
    255. * efficient to create a black/white version (png, etc.) and load that when you need it.
    256. */
    257. if (!mBurnInProtection && !mLowBitAmbient) {
    258. //initGrayBackgroundBitmap();
    259. }
    260. }
    261.  
    262. private void initGrayBackgroundBitmap() {
    263. mGrayBackgroundBitmap = Bitmap.createBitmap(
    264. mBackgroundBitmap.getWidth(),
    265. mBackgroundBitmap.getHeight(),
    266. Bitmap.Config.ARGB_8888);
    267. Canvas canvas = new Canvas(mGrayBackgroundBitmap);
    268. Paint grayPaint = new Paint();
    269. ColorMatrix colorMatrix = new ColorMatrix();
    270. colorMatrix.setSaturation(0);
    271. ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
    272. grayPaint.setColorFilter(filter);
    273. canvas.drawBitmap(mBackgroundBitmap, 0, 0, grayPaint);
    274. }
    275.  
    276. /**
    277. * Captures tap event (and tap type). The {@link WatchFaceService#TAP_TYPE_TAP} case can be
    278. * used for implementing specific logic to handle the gesture.
    279. */
    280. [USER=1021285]@override[/USER]
    281. public void onTapCommand(int tapType, int x, int y, long eventTime) {
    282. switch (tapType) {
    283. case TAP_TYPE_TOUCH:
    284. // The user has started touching the screen.
    285. break;
    286. case TAP_TYPE_TOUCH_CANCEL:
    287. // The user has started a different gesture or otherwise cancelled the tap.
    288. break;
    289. case TAP_TYPE_TAP:
    290. // The user has completed the tap gesture.
    291. // TODO: Add code to handle the tap gesture.
    292. /*Toast.makeText(getApplicationContext(), R.string.message, Toast.LENGTH_SHORT)
    293. .show();*/
    294. break;
    295. }
    296. invalidate();
    297. }
    298.  
    299. [USER=1021285]@override[/USER]
    300. public void onDraw(Canvas canvas, Rect bounds) {
    301. long now = System.currentTimeMillis();
    302. mCalendar.setTimeInMillis(now);
    303.  
    304. drawBackground(canvas);
    305. drawWatchFace(canvas);
    306. }
    307.  
    308. private void drawBackground(Canvas canvas) {
    309.  
    310. /* if ((mLowBitAmbient || mBurnInProtection)) {
    311. canvas.drawColor(Color.BLACK);
    312. } else {
    313. canvas.drawBitmap(mBackgroundBitmap, 0, 0, mBackgroundPaint);
    314. }*/
    315.  
    316. canvas.drawBitmap(mBackgroundBitmap, 0,0, mBackgroundPaint);
    317. }
    318.  
    319. private void drawWatchFace(Canvas canvas) {
    320.  
    321.  
    322.  
    323. /*
    324. * These calculations reflect the rotation in degrees per unit of time, e.g.,
    325. * 360 / 60 = 6 and 360 / 12 = 30.
    326. */
    327.  
    328. final float secondss =
    329. (mCalendar.get(Calendar.SECOND) + mCalendar.get(Calendar.MILLISECOND) / 1000f);
    330. final float secondssRotation = secondss * 6f;
    331.  
    332. final float seconds =
    333. (mCalendar.get(Calendar.SECOND) + mCalendar.get(Calendar.MILLISECOND) / 1000f);
    334. final float secondsRotation = seconds * 6f;
    335.  
    336. final float minutessRotation = mCalendar.get(Calendar.MINUTE) * 6f;
    337.  
    338. final float minutesRotation = mCalendar.get(Calendar.MINUTE) * 6f;
    339.  
    340. final float hourHandsOffset = mCalendar.get(Calendar.MINUTE) / 2f;
    341. final float hourssRotation = (mCalendar.get(Calendar.HOUR) * 30) + hourHandsOffset;
    342.  
    343. final float hourHandOffset = mCalendar.get(Calendar.MINUTE) / 2f;
    344. final float hoursRotation = (mCalendar.get(Calendar.HOUR) * 30) + hourHandOffset;
    345.  
    346. /*
    347. * Save the canvas state before we can begin to rotate it.
    348. */
    349. canvas.save();
    350.  
    351.  
    352. canvas.rotate(hourssRotation, mCenterX, mCenterY);
    353. canvas.drawBitmap(mHoursPaint, 0,0, paint);
    354.  
    355. // canvas.rotate(hoursRotation, mCenterX, mCenterY);
    356. canvas.drawBitmap(mHourPaint, 0,0, paint);
    357.  
    358. canvas.rotate(minutessRotation-hourssRotation,mCenterX, mCenterY);
    359. canvas.drawBitmap(mMinutesPaint, 0,0, paint);
    360.  
    361. // canvas.rotate(minutesRotation-hoursRotation,mCenterX, mCenterY);
    362. canvas.drawBitmap(mMinutePaint, 0,0 , paint);
    363.  
    364. canvas.rotate(secondssRotation-minutessRotation, mCenterX, mCenterY);
    365. canvas.drawBitmap(mSecondsPaint, 0, 0, paint);
    366.  
    367. // canvas.rotate(secondsRotation-minutesRotation, mCenterX, mCenterY);
    368. canvas.drawBitmap(mSecondPaint, 0, 0 , paint);
    369.  
    370.  
    371. canvas.restore();
    372. }
    373.  
    374. [USER=1021285]@override[/USER]
    375. public void onVisibilityChanged(boolean visible) {
    376. super.onVisibilityChanged(visible);
    377.  
    378. if (visible) {
    379. registerReceiver();
    380. /* Update time zone in case it changed while we weren't visible. */
    381. mCalendar.setTimeZone(TimeZone.getDefault());
    382. invalidate();
    383. } else {
    384. unregisterReceiver();
    385. }
    386.  
    387. /* Check and trigger whether or not timer should be running (only in active mode). */
    388. updateTimer();
    389. }
    390.  
    391. private void registerReceiver() {
    392. if (mRegisteredTimeZoneReceiver) {
    393. return;
    394. }
    395. mRegisteredTimeZoneReceiver = true;
    396. IntentFilter filter = new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED);
    397. MyWatchFace.this.registerReceiver(mTimeZoneReceiver, filter);
    398. }
    399.  
    400. private void unregisterReceiver() {
    401. if (!mRegisteredTimeZoneReceiver) {
    402. return;
    403. }
    404. mRegisteredTimeZoneReceiver = false;
    405. MyWatchFace.this.unregisterReceiver(mTimeZoneReceiver);
    406. }
    407.  
    408. /**
    409. * Starts/stops the {@link #mUpdateTimeHandler} timer based on the state of the watch face.
    410. */
    411. private void updateTimer() {
    412. mUpdateTimeHandler.removeMessages(MSG_UPDATE_TIME);
    413. if (shouldTimerBeRunning()) {
    414. mUpdateTimeHandler.sendEmptyMessage(MSG_UPDATE_TIME);
    415. }
    416. }
    417.  
    418. /**
    419. * Returns whether the {@link #mUpdateTimeHandler} timer should be running. The timer
    420. * should only run in active mode.
    421. */
    422. private boolean shouldTimerBeRunning() {
    423. return isVisible();
    424. }
    425.  
    426. /**
    427. * Handle updating the time periodically in interactive mode.
    428. */
    429. private void handleUpdateTimeMessage() {
    430. invalidate();
    431. if (shouldTimerBeRunning()) {
    432. long timeMs = System.currentTimeMillis();
    433. long delayMs = INTERACTIVE_UPDATE_RATE_MS
    434. - (timeMs % INTERACTIVE_UPDATE_RATE_MS);
    435. mUpdateTimeHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIME, delayMs);
    436. }
    437. }
    438. }
    439.  
    440.  
    441.  
    442.  
    443.  
    444. }
    .................................................................................

    Thank you in advance for your help!
     



    1. Download the Forums for Android™ app!


      Download

       
  2. WolfSpyryt

    WolfSpyryt Lurker
    Thread Starter

    Anyone who can help, please.
     
Loading...
Similar Threads - png Ambient Background
  1. carlosnino
    Replies:
    6
    Views:
    579
  2. VikingGlen
    Replies:
    11
    Views:
    644
  3. marieamana
    Replies:
    0
    Views:
    264
  4. Bhavin Lathia
    Replies:
    4
    Views:
    756
  5. MackOS
    Replies:
    0
    Views:
    521
  6. JamzApplications
    Replies:
    1
    Views:
    364
  7. App Update
    Replies:
    0
    Views:
    464
  8. ProNextMik
    Replies:
    1
    Views:
    424
  9. Predrag Nikolic
    Replies:
    0
    Views:
    713
  10. idon
    Replies:
    15
    Views:
    7,574

Share This Page

Loading...