2016-09-07 46 views
1

Digital WatchfaceのGoogleサンプルウォッチフェイスからコードの大部分をコピーしました。 しかし、背景画像を追加したいときは、私がdrawable(bg.png)の下に保存している画像の代わりに灰色の背景が表示されます。 私は何が間違っていますか?Android Wearの背景画像が描画されていません

***アップデート**** 灰色であることは確かに私の背景イメージですが、どういうわけか解像度は完全にオフです。背景画像は320x320(Googleのサンプルからの犬の画像)です。 プレビュー画像でも同じ動作を観察できます...私のウォッチフェイスからスクリーンショットを作成しましたが、適切なプレビューの代わりに背景色のみを見ることができます。

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 for normal (not ambient and not mute) mode. We update twice 
* a second to blink the colons. 
*/ 
private static final long NORMAL_UPDATE_RATE_MS = 500; 

/** 
* Update rate in milliseconds for mute mode. We update every minute, like 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 for drawing time when in mute mode. */ 
    static final int MUTE_ALPHA = 100; 

    /** Alpha value for drawing time when not in mute mode. */ 
    static final int NORMAL_ALPHA = 255; 

    static final int MSG_UPDATE_TIME = 0; 

    /** How often {@link #mUpdateTimeHandler} ticks in milliseconds. */ 
    long mInteractiveUpdateRateMs = NORMAL_UPDATE_RATE_MS; 

    /** Handler to update the 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 the display supports fewer bits for each color in ambient mode. When true, we 
    * 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); 

     // This sets the Timeformat to German ... could be removed if the Android Wear Device is initiated from a 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 the text color needs to be set here again ... it should be 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 the timer should be running depends on whether we're visible (as well as 
     // whether we're in ambient mode), so we may need to start or stop the 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 that have alternate values for 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 the timer should be running depends on whether we're in ambient mode (as well 
     // as whether we're visible), so we may need to start or stop the 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; 
     // We only need to update once a 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 and restart the timer so the 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 now = System.currentTimeMillis(); 

     int width = bounds.width(); 
     int height = bounds.height(); 
     mCalendar.setTimeInMillis(now); 
     mDate.setTime(now); 
     boolean is24Hour = DateFormat.is24HourFormat(WearWatchFaceService.this); 

     // Draw the background. 
     canvas.drawRect(0, 0, bounds.width(), bounds.height(), mBackgroundPaint); 

     //Draw the 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 for the first half of each second so the colons blink on when the time updates. 
     mShouldDrawColons = (System.currentTimeMillis() % 1000) < 500; 



     // Draw the 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 and mute modes, always draw the first colon. Otherwise, draw the 
     // first colon for the first half of each second. 
     if (isInAmbientMode() || mMute || mShouldDrawColons) { 
      canvas.drawText(COLON_STRING, x, mYOffset, mColonPaint); 
     } 
     x += mColonWidth; 

     // Draw the minutes. 
     String minuteString = formatTwoDigitNumber(mCalendar.get(Calendar.MINUTE)); 
     canvas.drawText(minuteString, x, mYOffset, mMinutePaint); 
     x += mMinutePaint.measureText(minuteString); 

     // In unmuted interactive mode, draw a second blinking colon followed by the 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; 

     } 

     // Only render the day of week and date if there is no peek card, so they do not bleed 
     // into 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 the {@link #mUpdateTimeHandler} timer if it should be running and isn't currently 
    * or stops it if it shouldn't be running but currently 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 the {@link #mUpdateTimeHandler} timer should be running. The timer should 
    * only run when we're visible and in interactive mode. 
    */ 
    private boolean shouldTimerBeRunning() { 
     return isVisible() && !isInAmbientMode(); 
    } 
} 
} 

答えて

0

これでコードは問題ありませんが、画像は表示されない「anydpi-folder」にあります。画像を「nodpi-folder」に移動して正常に動作しました。ここ はnodpiとanydpi

What Is The Difference Between -anydpi And -nodpi?

の間で異なっへのリンクです
関連する問題