• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

png as Ambient Background Not Showing

WolfSpyryt

Lurker
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.

Java:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.support.wearable.watchface.CanvasWatchFaceService;
import android.support.wearable.watchface.WatchFaceService;
import android.support.wearable.watchface.WatchFaceStyle;
import android.view.SurfaceHolder;
import android.view.View;


import java.lang.ref.WeakReference;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

import static android.view.View.LAYER_TYPE_SOFTWARE;


public class MyWatchFace extends CanvasWatchFaceService {

/*
* Updates rate in milliseconds for interactive mode. We update once a second to advance the
* second hand.
*/
private static final long INTERACTIVE_UPDATE_RATE_MS = TimeUnit.SECONDS.toMillis(1);

/**
* Handler message id for updating the time periodically in interactive mode.
*/
private static final int MSG_UPDATE_TIME = 0;

[USER=1021285]@override[/USER]
public Engine onCreateEngine() {
return new Engine();
}

private static class EngineHandler extends Handler {
private final WeakReference<MyWatchFace.Engine> mWeakReference;

public EngineHandler(MyWatchFace.Engine reference) {
mWeakReference = new WeakReference<>(reference);
}

[USER=1021285]@override[/USER]
public void handleMessage(Message msg) {
MyWatchFace.Engine engine = mWeakReference.get();
if (engine != null) {
switch (msg.what) {
case MSG_UPDATE_TIME:
engine.handleUpdateTimeMessage();
break;
}
}
}
}

private class Engine extends CanvasWatchFaceService.Engine {


/* Handler to update the time once a second in interactive mode. */
private final Handler mUpdateTimeHandler = new EngineHandler(this);
private Calendar mCalendar;
private final BroadcastReceiver mTimeZoneReceiver = new BroadcastReceiver() {
[USER=1021285]@override[/USER]
public void onReceive(Context context, Intent intent) {
mCalendar.setTimeZone(TimeZone.getDefault());
invalidate();
}
};
private boolean mRegisteredTimeZoneReceiver = false;
private boolean mMuteMode;
private float mCenterX;
private float mCenterY;

/* Colors for all hands (hour, minute, seconds, ticks) based on photo loaded. */

private Bitmap mHourPaint;
private Bitmap mHoursPaint;
private Paint paint;
private Bitmap mMinutesPaint;
private Bitmap mMinutePaint;
private Bitmap mSecondsPaint;
private Bitmap mSecondPaint;
private Paint mBackgroundPaint;
private Bitmap mBackgroundBitmap;
private Bitmap mGrayBackgroundBitmap;
private boolean mLowBitAmbient;
private boolean mBurnInProtection;


[USER=1021285]@override[/USER]
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);

setWatchFaceStyle(new WatchFaceStyle.Builder(MyWatchFace.this)
.setAcceptsTapEvents(true)
.setHideStatusBar(true)
.build());

mCalendar = Calendar.getInstance();

initializeBackground();
initializeWatchFace();
}

private void initializeBackground() {
mBackgroundPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
//mBackgroundPaint.setColor(Color.BLACK);
mBackgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
mGrayBackgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bgd);



}

private void initializeWatchFace() {
/* Set defaults for colors */

paint = new Paint(Paint.FILTER_BITMAP_FLAG);
paint.setAntiAlias(true);
paint.setShadowLayer(0, 0.0f, 0.0f, Color.BLACK);

mHoursPaint = BitmapFactory.decodeResource(getResources(), R.drawable.hourssh);
mHourPaint = BitmapFactory.decodeResource(getResources(), R.drawable.hours);
mMinutesPaint = BitmapFactory.decodeResource(getResources(), R.drawable.minutessh);
mMinutePaint = BitmapFactory.decodeResource(getResources(), R.drawable.minutes);
mSecondsPaint = BitmapFactory.decodeResource(getResources(), R.drawable.secondssh);
mSecondPaint = BitmapFactory.decodeResource(getResources(), R.drawable.seconds);

}

[USER=1021285]@override[/USER]
public void onDestroy() {
mUpdateTimeHandler.removeMessages(MSG_UPDATE_TIME);
super.onDestroy();
}

[USER=1021285]@override[/USER]
public void onPropertiesChanged(Bundle properties) {
super.onPropertiesChanged(properties);
mLowBitAmbient = properties.getBoolean(PROPERTY_LOW_BIT_AMBIENT, false);
mBurnInProtection = properties.getBoolean(PROPERTY_BURN_IN_PROTECTION, false);
}

[USER=1021285]@override[/USER]
public void onTimeTick() {
super.onTimeTick();
invalidate();
}

[USER=1021285]@override[/USER]
public void onAmbientModeChanged(boolean inAmbientMode) {
super.onAmbientModeChanged(inAmbientMode);

updateWatchHandStyle();

/* Check and trigger whether or not timer should be running (only in active mode). */
updateTimer();
}

private void updateWatchHandStyle() {


}

[USER=1021285]@override[/USER]
public void onInterruptionFilterChanged(int interruptionFilter) {
super.onInterruptionFilterChanged(interruptionFilter);
boolean inMuteMode = (interruptionFilter == WatchFaceService.INTERRUPTION_FILTER_NONE);

/* Dim display in mute mode. */
if (mMuteMode != inMuteMode) {
mMuteMode = inMuteMode;
// mHourPaint.setAlpha(inMuteMode ? 100 : 255);
// mMinutePaint.setAlpha(inMuteMode ? 100 : 255);
// mSecondPaint.setAlpha(inMuteMode ? 80 : 255);
invalidate();
}
}

[USER=1021285]@override[/USER]
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);

/*
* Find the coordinates of the center point on the screen, and ignore the window
* insets, so that, on round watches with a "chin", the watch face is centered on the
* entire screen, not just the usable portion.
*/
mCenterX = width / 2f;
mCenterY = height / 2f;

/* Scale loaded background image (more efficient) if surface dimensions change. */
float scaleHoursBitmap = height / (float) mHoursPaint.getHeight();
float scaleHourBitmap = height / (float) mHourPaint.getHeight();
float scaleMinsBitmap = height / (float) mMinutesPaint.getHeight();
float scaleMinBitmap = height / (float) mMinutePaint.getHeight();
float scaleSecondsBitmap = height / (float) mSecondsPaint.getHeight();
float scaleSecondBitmap = height / (float) mSecondPaint.getHeight();

mHoursPaint = Bitmap.createScaledBitmap(mHoursPaint,
(int) (mHoursPaint.getWidth() * scaleHoursBitmap),
(int) (mHoursPaint.getHeight() * scaleHoursBitmap), true);

mHourPaint = Bitmap.createScaledBitmap(mHourPaint,
(int) (mHourPaint.getWidth() * scaleHourBitmap),
(int) (mHourPaint.getHeight() * scaleHourBitmap), true);

mMinutesPaint = Bitmap.createScaledBitmap(mMinutesPaint,
(int) (mMinutesPaint.getWidth() * scaleMinsBitmap),
(int) (mMinutesPaint.getHeight() * scaleMinsBitmap), true);

mMinutePaint = Bitmap.createScaledBitmap(mMinutePaint,
(int) (mMinutePaint.getWidth() * scaleMinBitmap),
(int) (mMinutePaint.getHeight() * scaleMinBitmap), true);

mSecondsPaint = Bitmap.createScaledBitmap(mSecondsPaint,
(int) (mSecondsPaint.getWidth() * scaleSecondsBitmap),
(int) (mSecondsPaint.getHeight() * scaleSecondsBitmap), true);

mSecondPaint = Bitmap.createScaledBitmap(mSecondPaint,
(int) (mSecondPaint.getWidth() * scaleSecondBitmap),
(int) (mSecondPaint.getHeight() * scaleSecondBitmap), true);


/* Scale loaded background image (more efficient) if surface dimensions change. */
float scale = ((float) width) / (float) mBackgroundBitmap.getWidth();

mBackgroundBitmap = Bitmap.createScaledBitmap(mBackgroundBitmap,
(int) (mBackgroundBitmap.getWidth() * scale),
(int) (mBackgroundBitmap.getHeight() * scale), true);

/*
* Create a gray version of the image only if it will look nice on the device in
* ambient mode. That means we don't want devices that support burn-in
* protection (slight movements in pixels, not great for images going all the way to
* edges) and low ambient mode (degrades image quality).
*
* Also, if your watch face will know about all images ahead of time (users aren't
* selecting their own photos for the watch face), it will be more
* efficient to create a black/white version (png, etc.) and load that when you need it.
*/
if (!mBurnInProtection && !mLowBitAmbient) {
//initGrayBackgroundBitmap();
}
}

private void initGrayBackgroundBitmap() {
mGrayBackgroundBitmap = Bitmap.createBitmap(
mBackgroundBitmap.getWidth(),
mBackgroundBitmap.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mGrayBackgroundBitmap);
Paint grayPaint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
grayPaint.setColorFilter(filter);
canvas.drawBitmap(mBackgroundBitmap, 0, 0, grayPaint);
}

/**
* Captures tap event (and tap type). The {@link WatchFaceService#TAP_TYPE_TAP} case can be
* used for implementing specific logic to handle the gesture.
*/
[USER=1021285]@override[/USER]
public void onTapCommand(int tapType, int x, int y, long eventTime) {
switch (tapType) {
case TAP_TYPE_TOUCH:
// The user has started touching the screen.
break;
case TAP_TYPE_TOUCH_CANCEL:
// The user has started a different gesture or otherwise cancelled the tap.
break;
case TAP_TYPE_TAP:
// The user has completed the tap gesture.
// TODO: Add code to handle the tap gesture.
/*Toast.makeText(getApplicationContext(), R.string.message, Toast.LENGTH_SHORT)
.show();*/
break;
}
invalidate();
}

[USER=1021285]@override[/USER]
public void onDraw(Canvas canvas, Rect bounds) {
long now = System.currentTimeMillis();
mCalendar.setTimeInMillis(now);

drawBackground(canvas);
drawWatchFace(canvas);
}

private void drawBackground(Canvas canvas) {

/* if ((mLowBitAmbient || mBurnInProtection)) {
canvas.drawColor(Color.BLACK);
} else {
canvas.drawBitmap(mBackgroundBitmap, 0, 0, mBackgroundPaint);
}*/

canvas.drawBitmap(mBackgroundBitmap, 0,0, mBackgroundPaint);
}

private void drawWatchFace(Canvas canvas) {



/*
* These calculations reflect the rotation in degrees per unit of time, e.g.,
* 360 / 60 = 6 and 360 / 12 = 30.
*/

final float secondss =
(mCalendar.get(Calendar.SECOND) + mCalendar.get(Calendar.MILLISECOND) / 1000f);
final float secondssRotation = secondss * 6f;

final float seconds =
(mCalendar.get(Calendar.SECOND) + mCalendar.get(Calendar.MILLISECOND) / 1000f);
final float secondsRotation = seconds * 6f;

final float minutessRotation = mCalendar.get(Calendar.MINUTE) * 6f;

final float minutesRotation = mCalendar.get(Calendar.MINUTE) * 6f;

final float hourHandsOffset = mCalendar.get(Calendar.MINUTE) / 2f;
final float hourssRotation = (mCalendar.get(Calendar.HOUR) * 30) + hourHandsOffset;

final float hourHandOffset = mCalendar.get(Calendar.MINUTE) / 2f;
final float hoursRotation = (mCalendar.get(Calendar.HOUR) * 30) + hourHandOffset;

/*
* Save the canvas state before we can begin to rotate it.
*/
canvas.save();


canvas.rotate(hourssRotation, mCenterX, mCenterY);
canvas.drawBitmap(mHoursPaint, 0,0, paint);

// canvas.rotate(hoursRotation, mCenterX, mCenterY);
canvas.drawBitmap(mHourPaint, 0,0, paint);

canvas.rotate(minutessRotation-hourssRotation,mCenterX, mCenterY);
canvas.drawBitmap(mMinutesPaint, 0,0, paint);

// canvas.rotate(minutesRotation-hoursRotation,mCenterX, mCenterY);
canvas.drawBitmap(mMinutePaint, 0,0 , paint);

canvas.rotate(secondssRotation-minutessRotation, mCenterX, mCenterY);
canvas.drawBitmap(mSecondsPaint, 0, 0, paint);

// canvas.rotate(secondsRotation-minutesRotation, mCenterX, mCenterY);
canvas.drawBitmap(mSecondPaint, 0, 0 , paint);


canvas.restore();
}

[USER=1021285]@override[/USER]
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);

if (visible) {
registerReceiver();
/* Update time zone in case it changed while we weren't visible. */
mCalendar.setTimeZone(TimeZone.getDefault());
invalidate();
} else {
unregisterReceiver();
}

/* Check and trigger whether or not timer should be running (only in active mode). */
updateTimer();
}

private void registerReceiver() {
if (mRegisteredTimeZoneReceiver) {
return;
}
mRegisteredTimeZoneReceiver = true;
IntentFilter filter = new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED);
MyWatchFace.this.registerReceiver(mTimeZoneReceiver, filter);
}

private void unregisterReceiver() {
if (!mRegisteredTimeZoneReceiver) {
return;
}
mRegisteredTimeZoneReceiver = false;
MyWatchFace.this.unregisterReceiver(mTimeZoneReceiver);
}

/**
* Starts/stops the {@link #mUpdateTimeHandler} timer based on the state of the watch face.
*/
private void 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 in active mode.
*/
private boolean shouldTimerBeRunning() {
return isVisible();
}

/**
* Handle updating the time periodically in interactive mode.
*/
private void handleUpdateTimeMessage() {
invalidate();
if (shouldTimerBeRunning()) {
long timeMs = System.currentTimeMillis();
long delayMs = INTERACTIVE_UPDATE_RATE_MS
- (timeMs % INTERACTIVE_UPDATE_RATE_MS);
mUpdateTimeHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIME, delayMs);
}
}
}





}

.................................................................................

Thank you in advance for your help!
 
Back
Top Bottom