I had a canvas that was correctly displayed on older APIs. However, when I ran it on my newer tablet, the graphics weren't being drawn.
Here is a minimal test example:
protected void onDraw(Canvas canvas) {
// .....
// skipped paint initialization code ...
canvas.drawRect(0, 0, 100, 100, redPaint);
canvas.drawRect(100, 100, 200, 0, greenPaint);
canvas.drawRect(200, 0, 300, 100, bluePaint);
canvas.drawRect(300, 0, 400, 100, redPaint);
canvas.drawRect(500, 0, 400, 100, bluePaint);
canvas.drawRect(500, 0, 600, 100, greenPaint);
}
Running this on older and new tablets will give very different results.
Older devices:
[RED][GREEN][BLUE][RED][BLUE][GREEN]
Newer devices:
[RED][SPACE][BLUE][RED][SPACE][GREEN]
It seems that drawRect no longer works with negative values in newer APIs. To fix the problem, make sure values always go from lower to higher unless you have a specific reason for not wanting them to draw on new APIs.
Related
I using the AndroidPlot graphing library to make a bar graph, like so:
However, the months Jan and Dec are getting cut off. Does AndroidPlot have any kind of mechanism to offset this so that there's whitespace before Jan and after Dec?
I'm already using these methods to set various other margins and paddings in the graph:
mySimpleXYPlot.setBorderStyle(Plot.BorderStyle.NONE, null, null);
mySimpleXYPlot.setPlotMargins(0, 0, 0, 0);
mySimpleXYPlot.setPlotPadding(0, 0, 0, 0);
mySimpleXYPlot.setGridPadding(0, 0, 0, 0);
mySimpleXYPlot.getGraphWidget().setPadding(40, 20, 30, 30);
mySimpleXYPlot.getGraphWidget().setSize(new SizeMetrics(0, SizeLayoutType.FILL, 0, SizeLayoutType.FILL));
but changing the values in these methods has no effect on offsetting Jan or Dec. Any ideas?
Had this same problem, but found thatMyXYPLot.setGridPadding(100, 0, 100, 0); helped.
I'm writing an Android app which uses custom draw on SurfaceView and I'm getting a decaying shadow effect when i move the object (currently a rect). Below is portion of my code.
canvas = _surfaceHolder.lockCanvas(null);
if (canvas != null) {
synchronized (_surfaceHolder) {
// Starts of actual drawing code
if (paintFg != null) {
canvas.drawARGB(55, 55, 55, 55);
canvas.drawRect(x, y, x+100, y+100, paintFg);
}
}
}
}
When I change x and y, the box is drawn elsewhere, but the existing place have a decaying effect. I would like disable that.
Btw, I'm testing this on a Galaxy S3 with JellyBean. Not sure if ProjectButter that causes this, and I've get to tried on a older phone.
I found that if I change canvas.drawARGB(55, 55, 55, 55); to canvas.drawRGB(0, 0, 0); it solved the problem. But I'm still not sure what's the cause. Anyone care to point out?
I'm rendering simple scene with library GLESv1_CM on Android with NDK.
void render(Rect viewport)
{
glViewport(viewport.left, viewport.top, viewport.Width(), viewport.Height());
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(viewport.left, viewport.right, viewport.top, viewport.bottom, -1, 1);
GLfloat points[] = { 5, 50, 1, 1, 1, 1,
7, 50, 1, 1, 1, 1,
7, 50, 1, 0, 0, 1,
7, 52, 1, 0, 0, 1,
8, 50, 0, 1, 0, 1,
10, 50, 0, 1, 0, 1};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(2,GL_FLOAT,sizeof(GLfloat)*6,&points);
glColorPointer(4,GL_FLOAT,sizeof(GLfloat)*6,&points[2]);
glDrawArrays(GL_LINES,0,6);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
I wish to have 3 crossed lines, but when I run it on device with android 2.2 or 3.2 i have this:
Even more, on Android emulator with Android 4 (update: same on emulator with 2.3) i have this:
Is it possible to make it looks like on this picture on all devices with all Android versions without using opengl2?
Thank you
According to the OpenGL FAQ for this type of projection:
If exact pixelization is required, you might want to put a small
translation in the ModelView matrix, as shown below:
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.375, 0.375, 0.);
I imagine this reduces the likelihood of rounding issues.
I've searched the web for help and although it looked like a solution it turned out to not work so good. For starters I'd like to say I've just jumped into android programming (this is my first day) I really learn by trial and error I'd like it that if you could help me you'd give me hints rather than paste the code in front of me.
my tileset
http://img217.imageshack.us/img217/4654/tileseth.png
the result
http://img199.imageshack.us/img199/7913/resultx.png
the issues I'm having is 1. it is obviously not splitting the image in 32 by 32 bits. is what I'm trying to achieve is take my big image and split it into 9* smaller images of 32 by 32 portions. Secondary the image quality gets distorted and I can't work out why.
*I don't want to use a 9 patch as there will be more then 9 images soon just a fluke that atm I have 9 images
my code (evidently plagiarized from the internet)
tilesetSliced = Bitmap.createScaledBitmap(bmp, 96, 96, true);
tileset[0] = Bitmap.createBitmap(tilesetSliced, 0, 0, 32, 32);
tileset[1] = Bitmap.createBitmap(tilesetSliced, 32, 0, 32, 32);
tileset[2] = Bitmap.createBitmap(tilesetSliced, 64, 0, 32, 32);
tileset[3] = Bitmap.createBitmap(tilesetSliced, 0, 32, 32, 32);
tileset[4] = Bitmap.createBitmap(tilesetSliced, 32, 32, 32, 32);
tileset[5] = Bitmap.createBitmap(tilesetSliced, 64, 32, 32, 32);
I'll make it more efficent once I got it working >.< any help would be great
the on draw
public void onDraw(Canvas canvas) {
//update();
for(int x=0; x<= mapWidth; x++){
for(int y = 0; y <= mapHeight; y++){
canvas.drawBitmap(tileset[map[x][y]], x *32, y*32, null);
}
}
}
o.k some more debugging has shead light on something 1. I removed the scaledbitmap that stopped the quality being destroyed (orginally ahd it due to bugs) however I found out that for some reason it thinks the width of my tileset is 64 when its 96 any help would be nice on this.
You may have more luck with a Bitmap Factory to generate your tilesetSliced. Within it is an Options class that allows you to set the sample size (inSampleSize) which can be used to scale down your image. It may not be precise enough for your needs, however.
Your images are likely distorted due to the scaling down process. Are you able to create these images with the right dimensions or pre-scale them?
I am trying to load a texture on to my android app display, where I am using code from this github.
I get my pixels messed up completely on the screen, And I have no idea, what's going on. The only thing I change in that code is I have memcpy, which copied uint8_t buffer into s_pixels instead of render_pixels in glbuffer.c file. My frame pixels are in rgb565 format.
Is it somekind of configuration problem or any problem with the way I copy pixels?
EDIT
Below is the code :
pictureQ is as below
pictureQ {
uint8_t *data;
int size;
}
memcpy(s_pixels,&(pictureQ[qFirst].data[0]) , 307200);
//render_pixels(s_pixels);
glClear(GL_COLOR_BUFFER_BIT);
// glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 480, 320, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, &(pictureRGBQ[qFirst].data[0]));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 480, 320, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, s_pixels);
check_gl_error("glTexSubImage2D");
glDrawTexiOES(0, 0, 0, s_w, s_h);
check_gl_error("glDrawTexiOES");
memset(s_pixels, 0, 307200);
Ok, It was my mistake, I was passing the data for the pixels instead of the data. Thanks for your response Reuben Scatton.