未绘制Android Wear背景图像

时间:2016-09-07 11:33:53

标签: java android android-canvas wear-os

我从Google Sample Watchface复制了大部分代码用于数字表盘,到目前为止它工作正常。 但是当我想添加背景图像时,我只看到灰色背景而不是我在drawable(bg.png)下存储的图像。 我做错了什么?

*** ****更新 好吧,灰色显然是我的背景图像,但不知何故,分辨率完全关闭。背景图像是320x320(来自谷歌样本的狗图像)。 我可以观察到预览图像的相同行为......我从我的Watchface制作了一个截图,但是我没有看到适当的预览,只能看到背景颜色。

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();
    }
}
}

1 个答案:

答案 0 :(得分:0)

好的所以代码很好,但是图片位于“anydpi-folder”中,显然它没有显示。我将图像移动到“nodpi-folder”并且工作正常。 这是nodpi和anydpi之间不同的链接

What Is The Difference Between -anydpi And -nodpi?