Revision: 2429
http://sourceforge.net/p/swingme/code/2429
Author: yuranet
Date: 2021-03-27 14:12:05 +0000 (Sat, 27 Mar 2021)
Log Message:
-----------
hopefully actually fixed the messed up density of bitmaps on samsung bug
Modified Paths:
--------------
AndroidME/src_MIDP/javax/microedition/lcdui/Graphics.java
AndroidME/src_MIDP/javax/microedition/lcdui/Image.java
Modified: AndroidME/src_MIDP/javax/microedition/lcdui/Graphics.java
===================================================================
--- AndroidME/src_MIDP/javax/microedition/lcdui/Graphics.java 2021-03-19 11:44:06 UTC (rev 2428)
+++ AndroidME/src_MIDP/javax/microedition/lcdui/Graphics.java 2021-03-27 14:12:05 UTC (rev 2429)
@@ -148,7 +148,7 @@
// in targetAPI 28+ android seems to have stopped respecting the density of the bitmap for app resources
// this is probably a bug: https://issuetracker.google.com/issues/181887424
// we cant compare image with canvas.getDensity() as that seems to just return 0, and we also cant set it
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && getTargetDensity() != image.getBitmap().getDensity()) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && Image.getTargetDensity() != image.getBitmap().getDensity()) {
canvas.drawBitmap(image.getBitmap(),null, new Rect(x, y, x + image.getWidth(), y + image.getHeight()), paint);
}
else {
@@ -157,14 +157,6 @@
paint.setAlpha(a);
}
- private int getTargetDensity() {
- int density = canvas.getDensity();
- if (density == Bitmap.DENSITY_NONE) {
- return Image.getTargetDensity();
- }
- return density;
- }
-
public void drawLine(int x1, int y1, int x2, int y2) {
if (x1 > x2) {
x1++;
@@ -443,7 +435,8 @@
paint.setAlpha(0xFF); // we do not support alpha when drawing images
int bitmapDensity = src.getBitmap().getDensity();
- int currentDensity = getTargetDensity();
+ // the canvas has its own density but we can NEVER trust it, android can put random numbers there
+ int currentDensity = Image.getTargetDensity();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && currentDensity != bitmapDensity) {
srcRect.set(Image.scaleFromDensity(srcRect.left, currentDensity, bitmapDensity),
Image.scaleFromDensity(srcRect.top, currentDensity, bitmapDensity),
Modified: AndroidME/src_MIDP/javax/microedition/lcdui/Image.java
===================================================================
--- AndroidME/src_MIDP/javax/microedition/lcdui/Image.java 2021-03-19 11:44:06 UTC (rev 2428)
+++ AndroidME/src_MIDP/javax/microedition/lcdui/Image.java 2021-03-27 14:12:05 UTC (rev 2429)
@@ -123,8 +123,8 @@
error = e;
buffInput.reset();
buffInput.mark(1024);
- options.inScaled = true;
- options.inSampleSize = 0;
+ options.inScaled = true; // this prob does nothing
+ options.inSampleSize = 0; // this prob does nothing
bitmap = BitmapFactory.decodeStream(buffInput,null,options);
}
@@ -132,25 +132,19 @@
throw new IOException("BitmapFactory.decodeStream returned null " + stream + " available=" + available + " " + options.outMimeType + " " + options.outWidth + "x" + options.outHeight + " " + error);
}
- // for testing/emulating loading images at low res as some Samsungs sometimes do
- //bitmap.setDensity(getTargetDensity() / options.inSampleSize);
-
- // some Samsungs will scale the image to be smaller (bitmap=315 system=420)
+ // some Samsungs will set density to a random number, so we fix it so it draws correctly
if (bitmap.getDensity() != Bitmap.DENSITY_NONE && bitmap.getDensity() != getTargetDensity()) {
- System.err.println("unexpected bitmap density=" + bitmap.getDensity() + " (system=" + getTargetDensity()+")");
+ int systemDensity = getTargetDensity();
+ int bitmapDensity = bitmap.getDensity();
+ int w = bitmap.getWidth();
+ int h = bitmap.getHeight();
+ System.err.println("unexpected bitmap density=" + bitmapDensity + " " + w + "x" + h +
+ " (system=" + systemDensity + " " + scaleFromDensity(w, bitmapDensity, systemDensity) + "x" + scaleFromDensity(h, bitmapDensity, systemDensity) + ")" +
+ " for " + stream);
+ bitmap.setDensity(systemDensity);
}
}
- if (bitmap.getDensity() != Bitmap.DENSITY_NONE && bitmap.getDensity() != getTargetDensity()) {
- int systemDensity = getTargetDensity();
- int bitmapDensity = bitmap.getDensity();
- int w = bitmap.getWidth();
- int h = bitmap.getHeight();
- System.out.println("bitmap density=" + bitmapDensity + " " + w + "x" + h +
- " (system=" + systemDensity + " " + scaleFromDensity(w, bitmapDensity, systemDensity) + "x" + scaleFromDensity(h, bitmapDensity, systemDensity) + ")" +
- " for " + stream);
- }
-
return new Image(bitmap);
}
@@ -174,9 +168,16 @@
}
if (bitmap.getDensity() != Bitmap.DENSITY_NONE && bitmap.getDensity() != getTargetDensity()) {
- System.err.println("unexpected new bitmap density=" + bitmap.getDensity() + " (system=" + getTargetDensity()+")");
+ int systemDensity = getTargetDensity();
+ // android sets this number to random numbers sometimes, so we just set it back to the same as the display
+ System.out.println("unexpected new bitmap density=" + bitmap.getDensity() + " (system=" + systemDensity + ")");
+ // we need to fix this, otherwise it will draw funny, as draw code has to take density into account as some app resource images have specific densities.
+ bitmap.setDensity(systemDensity);
}
+ if (bitmap.getWidth() != width || bitmap.getHeight() != height) {
+ System.err.println("unexpected new bitmap size=" + bitmap.getWidth() + "x" + bitmap.getHeight()+" (createImage=" + width + "x" + height + ')');
+ }
return new Image(bitmap);
}
@@ -457,6 +458,9 @@
* @return a app resource image, in API-28+ this can come back with a DPI different to the current display dpi
*/
public Bitmap getBitmap() {
+ // if we use this, the image will be scaled to the target density,
+ // but we dont really need this as can scale it ourselves when drawing
+ // Bitmap bitmap = BitmapFactory.decodeResource(res, resId, null);
return ((BitmapDrawable)getDrawable()).getBitmap();
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|