java - Android Wear Background Image is not drawn -


i copied of code google sample watchface digital watchface, , works fine far. when want add background image, see grey background instead of image store under drawable (bg.png). doing wrong?

***update**** ok apparantly grey apparantly background image, somehow resolution off. background image 320x320 (the dog image google sample). can observe same behaviour preview images ... made screenshot my watchface, instead of proper preview can see background color.

public class wearwatchfaceservice extends canvaswatchfaceservice { private static final string tag = "digitalwatchfaceservice";  private static final typeface bold_typeface =         typeface.create(typeface.sans_serif, typeface.bold); private static final typeface normal_typeface =         typeface.create(typeface.sans_serif, typeface.normal);  /**  * update rate in milliseconds normal (not ambient , not mute) mode. update twice  * second blink colons.  */ private static final long normal_update_rate_ms = 500;  /**  * update rate in milliseconds mute mode. update every minute, in ambient mode.  */ private static final long mute_update_rate_ms = timeunit.minutes.tomillis(1);  @override public engine oncreateengine() {     return new engine(); }  private class engine extends canvaswatchfaceservice.engine {     static final string colon_string = ":";      /** alpha value drawing time when in mute mode. */     static final int mute_alpha = 100;      /** alpha value drawing time when not in mute mode. */     static final int normal_alpha = 255;      static final int msg_update_time = 0;      /** how {@link #mupdatetimehandler} ticks in milliseconds. */     long minteractiveupdateratems = normal_update_rate_ms;      /** handler update time periodically in interactive mode. */     final handler mupdatetimehandler = new handler() {         @override         public void handlemessage(message message) {             switch (message.what) {                 case msg_update_time:                     if (log.isloggable(tag, log.verbose)) {                         log.v(tag, "updating time");                     }                     invalidate();                     if (shouldtimerberunning()) {                         long timems = system.currenttimemillis();                         long delayms =                                 minteractiveupdateratems - (timems % minteractiveupdateratems);                         mupdatetimehandler.sendemptymessagedelayed(msg_update_time, delayms);                     }                     break;             }         }     };       paint mbackgroundpaint;     bitmap mbackgroundbitmap;     paint mdatepaint;     paint mhourpaint;     paint mminutepaint;     paint msecondpaint;     paint mampmpaint;     paint mcolonpaint;     float mcolonwidth;     boolean mmute;      calendar mcalendar;     date mdate;     simpledateformat mdayofweekformat;     java.text.dateformat mdateformat;      boolean mshoulddrawcolons;     float mxoffset;     float myoffset;     float mlineheight;      int minteractivebackgroundcolor =             r.color.default_bg;     int minteractivehourdigitscolor =             r.color.default_hour;     int minteractiveminutedigitscolor =             r.color.default_minute;     int minteractiveseconddigitscolor =             r.color.default_second;      /**      * whether display supports fewer bits each color in ambient mode. when true,      * disable anti-aliasing in ambient mode.      */     boolean mlowbitambient;      @override     public void oncreate(surfaceholder holder) {         if (log.isloggable(tag, log.debug)) {             log.d(tag, "oncreate");         }         super.oncreate(holder);          // sets timeformat german ... removed if android wear device initiated german tablet/phone         locale locale = new locale("de");         locale.setdefault(locale);         configuration config = new configuration();         config.locale = locale;         getbasecontext().getresources().updateconfiguration(config,                 getbasecontext().getresources().getdisplaymetrics());           setwatchfacestyle(new watchfacestyle.builder(wearwatchfaceservice.this)                 .setcardpeekmode(watchfacestyle.peek_mode_variable)                 .setbackgroundvisibility(watchfacestyle.background_visibility_interruptive)                 .setshowsystemuitime(false)                 .build());         resources resources = wearwatchfaceservice.this.getresources();         myoffset = resources.getdimension(r.dimen.digital_y_offset);         mlineheight = resources.getdimension(r.dimen.digital_line_height);           setdefaultcolors();          // not sure why text color needs set here again ... should set in setdefaultcolors()!         mdatepaint.setcolor(getcolor(r.color.digital_date));         mhourpaint.setcolor(getcolor(r.color.default_hour));         mminutepaint.setcolor(getcolor(r.color.default_minute));         msecondpaint.setcolor(getcolor(r.color.default_second));         mcolonpaint.setcolor(getcolor(r.color.digital_colons));          if(!isinambientmode()) {             mbackgroundbitmap = bitmapfactory.decoderesource(wearwatchfaceservice.this.getresources(), r.drawable.bg);         }           mcalendar = calendar.getinstance();         mdate = new date();         initformats();     }      public void setdefaultcolors() {         mbackgroundpaint = new paint();         mbackgroundpaint.setcolor(getcolor(minteractivebackgroundcolor));         mdatepaint = createtextpaint(r.color.digital_date);         mhourpaint = createtextpaint(minteractivehourdigitscolor, bold_typeface);         mminutepaint = createtextpaint(minteractiveminutedigitscolor);         msecondpaint = createtextpaint(minteractiveseconddigitscolor);         mcolonpaint = createtextpaint(r.color.digital_colons);     }     @override     public void ondestroy() {         mupdatetimehandler.removemessages(msg_update_time);         super.ondestroy();     }      private paint createtextpaint(int defaultinteractivecolor) {         return createtextpaint(defaultinteractivecolor, normal_typeface);     }      private paint createtextpaint(int defaultinteractivecolor, typeface typeface) {         paint paint = new paint();         paint.setcolor(defaultinteractivecolor);         paint.settypeface(typeface);         paint.setantialias(true);         return paint;     }      @override     public void onvisibilitychanged(boolean visible) {         if (log.isloggable(tag, log.debug)) {             log.d(tag, "onvisibilitychanged: " + visible);         }         super.onvisibilitychanged(visible);          // whether timer should running depends on whether we're visible (as         // whether we're in ambient mode), may need start or stop timer.         updatetimer();     }      private void initformats() {         mdayofweekformat = new simpledateformat("eeee", locale.getdefault());         mdayofweekformat.setcalendar(mcalendar);         mdateformat = dateformat.getdateformat(wearwatchfaceservice.this);         mdateformat.setcalendar(mcalendar);     }       @override     public void onapplywindowinsets(windowinsets insets) {         if (log.isloggable(tag, log.debug)) {             log.d(tag, "onapplywindowinsets: " + (insets.isround() ? "round" : "square"));         }         super.onapplywindowinsets(insets);          // load resources have alternate values round watches.         resources resources = wearwatchfaceservice.this.getresources();         boolean isround = insets.isround();         mxoffset = resources.getdimension(isround                 ? r.dimen.digital_x_offset_round : r.dimen.digital_x_offset);         float textsize = resources.getdimension(isround                 ? r.dimen.digital_text_size_round : r.dimen.digital_text_size);           mdatepaint.settextsize(resources.getdimension(r.dimen.digital_date_text_size));          mhourpaint.settextsize(textsize);         mminutepaint.settextsize(textsize);         msecondpaint.settextsize(textsize);         mcolonpaint.settextsize(textsize);          mcolonwidth = mcolonpaint.measuretext(colon_string);      }      @override     public void onpropertieschanged(bundle properties) {         super.onpropertieschanged(properties);          boolean burninprotection = properties.getboolean(property_burn_in_protection, false);         mhourpaint.settypeface(burninprotection ? normal_typeface : bold_typeface);          mlowbitambient = properties.getboolean(property_low_bit_ambient, false);          if (log.isloggable(tag, log.debug)) {             log.d(tag, "onpropertieschanged: burn-in protection = " + burninprotection                     + ", low-bit ambient = " + mlowbitambient);         }     }      @override     public void ontimetick() {         super.ontimetick();         if (log.isloggable(tag, log.debug)) {             log.d(tag, "ontimetick: ambient = " + isinambientmode());         }         invalidate();     }      @override     public void onambientmodechanged(boolean inambientmode) {         super.onambientmodechanged(inambientmode);         if (!isinambientmode()) {             mbackgroundpaint = new paint();             mbackgroundpaint.setcolor(getcolor(r.color.default_bg));             mdatepaint.setcolor(getcolor(r.color.digital_date));             mhourpaint.setcolor(getcolor(r.color.default_hour));             mminutepaint.setcolor(getcolor(r.color.default_minute));             msecondpaint.setcolor(getcolor(r.color.default_second));             mcolonpaint.setcolor(getcolor(r.color.digital_colons));         }         else {             mbackgroundpaint = new paint();             mbackgroundpaint.setcolor(getcolor(r.color.ambient_bg));             mdatepaint.setcolor(getcolor(r.color.digital_date));             mhourpaint.setcolor(getcolor(r.color.ambient_hour));             mminutepaint.setcolor(getcolor(r.color.ambient_minute));             msecondpaint.setcolor(getcolor(r.color.ambient_second));             mcolonpaint.setcolor(getcolor(r.color.digital_colons));         }          log.d("xxx", "onambientmodechanged: " + inambientmode);           if (mlowbitambient) {             boolean antialias = !inambientmode;             mdatepaint.setantialias(antialias);             mhourpaint.setantialias(antialias);             mminutepaint.setantialias(antialias);             msecondpaint.setantialias(antialias);             mampmpaint.setantialias(antialias);             mcolonpaint.setantialias(antialias);         }         invalidate();          // whether timer should running depends on whether we're in ambient mode (as         // whether we're visible), may need start or stop timer.         updatetimer();     }      @override     public void oninterruptionfilterchanged(int interruptionfilter) {         if (log.isloggable(tag, log.debug)) {             log.d(tag, "oninterruptionfilterchanged: " + interruptionfilter);         }         super.oninterruptionfilterchanged(interruptionfilter);          boolean inmutemode = interruptionfilter == watchfaceservice.interruption_filter_none;         // need update once minute in mute mode.         setinteractiveupdateratems(inmutemode ? mute_update_rate_ms : normal_update_rate_ms);          if (mmute != inmutemode) {             mmute = inmutemode;             int alpha = inmutemode ? mute_alpha : normal_alpha;             mdatepaint.setalpha(alpha);             mhourpaint.setalpha(alpha);             mminutepaint.setalpha(alpha);             mcolonpaint.setalpha(alpha);             mampmpaint.setalpha(alpha);             invalidate();         }     }      public void setinteractiveupdateratems(long updateratems) {         if (updateratems == minteractiveupdateratems) {             return;         }         minteractiveupdateratems = updateratems;          // stop , restart timer new update rate takes effect immediately.         if (shouldtimerberunning()) {             updatetimer();         }     }      private string formattwodigitnumber(int hour) {         return string.format("%02d", hour);     }      @override     public void ondraw(canvas canvas, rect bounds) {         long = system.currenttimemillis();          int width = bounds.width();         int height = bounds.height();         mcalendar.settimeinmillis(now);         mdate.settime(now);         boolean is24hour = dateformat.is24hourformat(wearwatchfaceservice.this);          // draw background.         canvas.drawrect(0, 0, bounds.width(), bounds.height(), mbackgroundpaint);          //draw background image         if (mbackgroundbitmap == null                 || mbackgroundbitmap.getwidth() != width                 || mbackgroundbitmap.getheight() != height) {             mbackgroundbitmap = bitmap.createscaledbitmap(mbackgroundbitmap,                     width, height, true /* filter */);         }          if (isinambientmode() && (mlowbitambient)) {             canvas.drawcolor(color.black);         } else if (isinambientmode()) {             canvas.drawbitmap(mbackgroundbitmap, 0, 0, mbackgroundpaint);         } else {             canvas.drawbitmap(mbackgroundbitmap, 0, 0, mbackgroundpaint);         }           // show colons first half of each second colons blink on when time updates.         mshoulddrawcolons = (system.currenttimemillis() % 1000) < 500;            // draw hours.         float x = mxoffset;         string hourstring;         if (is24hour) {             hourstring = formattwodigitnumber(mcalendar.get(calendar.hour_of_day));         } else {             int hour = mcalendar.get(calendar.hour);             if (hour == 0) {                 hour = 12;             }             hourstring = string.valueof(hour);         }         canvas.drawtext(hourstring, x, myoffset, mhourpaint);         x += mhourpaint.measuretext(hourstring);          // in ambient , mute modes, draw first colon. otherwise, draw         // first colon first half of each second.         if (isinambientmode() || mmute || mshoulddrawcolons) {             canvas.drawtext(colon_string, x, myoffset, mcolonpaint);         }         x += mcolonwidth;          // draw minutes.         string minutestring = formattwodigitnumber(mcalendar.get(calendar.minute));         canvas.drawtext(minutestring, x, myoffset, mminutepaint);         x += mminutepaint.measuretext(minutestring);          // in unmuted interactive mode, draw second blinking colon followed seconds.         // otherwise, if we're in 12-hour mode, draw am/pm         if (!isinambientmode() && !mmute) {             if (mshoulddrawcolons) {                 canvas.drawtext(colon_string, x, myoffset, mcolonpaint);             }             x += mcolonwidth;             canvas.drawtext(formattwodigitnumber(                     mcalendar.get(calendar.second)), x, myoffset, msecondpaint);         } else if (!is24hour) {             x += mcolonwidth;          }          // render day of week , date if there no peek card, not bleed         // each other in ambient mode.         if (getpeekcardposition().isempty()) {             // day of week             canvas.drawtext(                     mdayofweekformat.format(mdate),                     mxoffset, myoffset + mlineheight, mdatepaint);             // date             canvas.drawtext(                     mdateformat.format(mdate),                     mxoffset, myoffset + mlineheight * 2, mdatepaint);         }     }      /**      * starts {@link #mupdatetimehandler} timer if should running , isn't      * or stops if shouldn't running is.      */     private void updatetimer() {         if (log.isloggable(tag, log.debug)) {             log.d(tag, "updatetimer");         }         mupdatetimehandler.removemessages(msg_update_time);         if (shouldtimerberunning()) {             mupdatetimehandler.sendemptymessage(msg_update_time);         }     }      /**      * returns whether {@link #mupdatetimehandler} timer should running. timer should      * run when we're visible , in interactive mode.      */     private boolean shouldtimerberunning() {         return isvisible() && !isinambientmode();     } } } 

ok code fine, image in "anydpi-folder" apparantly not show. moved image "nodpi-folder" , worked fine. here link different between nodpi , anydpi

what difference between -anydpi , -nodpi?


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -