1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-13 03:07:32 +01:00

added widget alpha channel

This commit is contained in:
dakhnod 2020-01-13 03:35:06 +01:00
parent e66c70ece8
commit fd8800607c
3 changed files with 86 additions and 53 deletions

View File

@ -149,6 +149,8 @@ public class FossilHRWatchAdapter extends FossilWatchAdapter {
overwriteButtons(null);
loadBackground();
loadWidgets();
renderWidgets();
// dunno if there is any point in doing this at start since when no watch is connected the QHybridSupport will not receive any intents anyway
@ -169,7 +171,9 @@ public class FossilHRWatchAdapter extends FossilWatchAdapter {
}
private void loadBackground(){
Bitmap backgroundBitmap = BitmapFactory.decodeFile("");
Bitmap backgroundBitmap = BitmapFactory
.decodeFile("/sdcard/DCIM/Camera/IMG_20191129_200726.jpg");
try {
this.backGroundImage = AssetImageFactory.createAssetImage(backgroundBitmap, false, 0, 0, 0);
} catch (IOException e) {
@ -193,9 +197,9 @@ public class FossilHRWatchAdapter extends FossilWatchAdapter {
dateWidget.addElement(new CustomTextWidgetElement("date", "-", CustomWidgetElement.X_CENTER, CustomWidgetElement.Y_LOWER_HALF));
CustomWidget helloWidget = new CustomWidget(180, 63);
helloWidget.addElement(new CustomTextWidgetElement("Hello", CustomWidgetElement.X_CENTER, CustomWidgetElement.Y_UPPER_HALF));
helloWidget.addElement(new CustomTextWidgetElement("Reddit", CustomWidgetElement.X_CENTER, CustomWidgetElement.Y_LOWER_HALF));
// helloWidget.addElement(new CustomBackgroundWidgetElement("/sdcard/reddit.png"));
// helloWidget.addElement(new CustomTextWidgetElement("Hello", CustomWidgetElement.X_CENTER, CustomWidgetElement.Y_UPPER_HALF));
// helloWidget.addElement(new CustomTextWidgetElement("Reddit", CustomWidgetElement.X_CENTER, CustomWidgetElement.Y_LOWER_HALF));
helloWidget.addElement(new CustomBackgroundWidgetElement("reddit", "/sdcard/reddit.png"));
this.widgets = new CustomWidget[]{
ethWidget,
btcWidget,
@ -212,19 +216,43 @@ public class FossilHRWatchAdapter extends FossilWatchAdapter {
widgetImages.add(this.backGroundImage);
}
for (int i = 0; i < this.widgets.length; i++) {
CustomWidget widget = widgets[i];
Bitmap widgetBitmap = Bitmap.createBitmap(76, 76, Bitmap.Config.ARGB_4444);
Bitmap widgetBitmap = Bitmap.createBitmap(76, 76, Bitmap.Config.ARGB_8888);
Canvas widgetCanvas = new Canvas(widgetBitmap);
Paint circlePaint = new Paint();
circlePaint.setColor(Color.WHITE);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeWidth(3);
boolean backgroundDrawn = false;
widgetCanvas.drawCircle(38, 38, 37, circlePaint);
Paint circlePaint = new Paint();
if(!backgroundDrawn){
circlePaint.setColor(Color.BLACK);
circlePaint.setStyle(Paint.Style.FILL);
circlePaint.setStrokeWidth(3);
widgetCanvas.drawCircle(38, 38, 37, circlePaint);
circlePaint.setColor(Color.WHITE);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeWidth(3);
widgetCanvas.drawCircle(38, 38, 37, circlePaint);
}
for (CustomWidgetElement element : widget.getElements()) {
if (element.getWidgetElementType() == CustomWidgetElement.WidgetElementType.TYPE_BACKGROUND) {
Bitmap imageBitmap = BitmapFactory.decodeFile(element.getValue());
Bitmap scaledBitmap = Bitmap.createScaledBitmap(imageBitmap, 76, 76, false);
widgetCanvas.drawBitmap(
scaledBitmap,
0,
0,
null);
backgroundDrawn = true;
break;
}
}
for (CustomWidgetElement element : widget.getElements()) {
if (element.getWidgetElementType() == CustomWidgetElement.WidgetElementType.TYPE_TEXT) {
@ -240,23 +268,6 @@ public class FossilHRWatchAdapter extends FossilWatchAdapter {
Bitmap imageBitmap = BitmapFactory.decodeFile(element.getValue());
widgetCanvas.drawBitmap(imageBitmap, element.getX() - imageBitmap.getWidth() / 2f, element.getY() - imageBitmap.getHeight() / 2f, null);
}else if(element.getWidgetElementType() == CustomWidgetElement.WidgetElementType.TYPE_BACKGROUND) {
Bitmap imageBitmap = BitmapFactory.decodeFile(element.getValue());
imageBitmap.setConfig(Bitmap.Config.ARGB_4444);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(imageBitmap, 76, 76, false);
// new Canvas(scaledBitmap).drawColor(Color.GRAY);
widgetCanvas.drawBitmap(
scaledBitmap,
0,
0,
paint);
}
}
widgetImages.add(AssetImageFactory.createAssetImage(

View File

@ -1,11 +1,14 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.image;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.ColorSpace;
import androidx.annotation.ColorInt;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class AssetImageFactory {
@ -14,31 +17,49 @@ public class AssetImageFactory {
}
public static AssetImage createAssetImage(Bitmap fileData, boolean RLEencode, int angle, int distance, int indexZ) throws IOException {
int height = fileData.getHeight();
int width = fileData.getWidth();
// if(fileData.getConfig() != Bitmap.Config.ALPHA_8) throw new RuntimeException("Bitmap is not ALPHA_8");
int[] pixels = new int[height * width];
fileData.getPixels(pixels, 0, width, 0, 0, width, height);
byte[] pixelBytes = new byte[width * height];
for(int i = 0; i < pixels.length; i++){
int monochrome = convertToMonochrome(pixels[i]);
monochrome >>= 6;
pixelBytes[i] = (byte) monochrome;
}
if(RLEencode){
int height = fileData.getHeight();
int width = fileData.getWidth();
// if(fileData.getConfig() != Bitmap.Config.ALPHA_8) throw new RuntimeException("Bitmap is not ALPHA_8");
int[] pixels = new int[height * width];
fileData.getPixels(pixels, 0, width, 0, 0, width, height);
byte[] pixelBytes = new byte[width * height];
for(int i = 0; i < pixels.length; i++){
int monochrome = convertToMonochrome(pixels[i]);
monochrome >>= 6;
int alpha = Color.alpha(pixels[i]);
monochrome |= (~((alpha & 0xFF) >> 4) & 0b00001100);
pixelBytes[i] = (byte) monochrome;
}
return new AssetImage(ImageConverter.encodeToRLEImage(pixelBytes, height, width), angle, distance, indexZ);
}else{
// applies only to big background
int width = 240;
int height = 240;
byte[] pixelBytes = new byte[width * height];
float jumpX = fileData.getWidth() / (float) width;
float jumpY = fileData.getHeight() / (float) height;
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
int pixel = fileData.getPixel((int)(x * jumpX), (int)(y * jumpY));
int monochrome = convertToMonochrome(pixel);
pixelBytes[pixelBytes.length - 1 - (y * width + x)] = (byte) monochrome;
}
}
return new AssetImage(ImageConverter.encodeToRawImage(pixelBytes), angle, distance, indexZ);
}
return null;
}
private static @ColorInt int convertToMonochrome(@ColorInt int color){

View File

@ -26,16 +26,17 @@ public class ImageConverter {
return bos.toByteArray();
}
public static byte[] encodeToRawImage(byte[] monochromeImage, int height, int width){
int pixelCount = height * width;
public static byte[] encodeToRawImage(byte[] monochromeImage){
int imageSize = monochromeImage.length;
byte[] result = new byte[pixelCount / 4]; // 4 pixels per byte e.g. 2 bits per pixel
byte[] result = new byte[imageSize / 4]; // 4 pixels per byte e.g. 2 bits per pixel
for(int i = 0; i < pixelCount; i++){
for(int i = 0; i < imageSize; i++){
int resultPixelIndex = i / 4;
int shiftIndex = i % 4 * 2;
int shiftIndex = 6 - i % 4 * 2;
result[resultPixelIndex] = (byte) ((monochromeImage[i] >> 6) << shiftIndex);
result[resultPixelIndex] |= (byte) (((monochromeImage[i] & 0xFF) >> 6) << shiftIndex);
assert Boolean.TRUE;
}
return result;