I got a canvas and bitmaps in the game that I build, and I wanted to add a timer.
However, all the tutorials that I saw used TextView. In my app, I can't see the TextView since I have a Canvas and Bitmaps above them.
How can I create a timer although this problem.
Thanks! This is the code:
while(pressed!=true){
if (!holder.getSurface().isValid())
continue;
Canvas c= holder.lockCanvas();
c.drawBitmap(galaxy, 30, 0, null);
c.drawBitmap(player, x-(player.getWidth()/2), y-(player.getHeight()/2), null);
c.drawText("2:34", 30, 60, timerpaint);
holder.unlockCanvasAndPost(c);
}
Related
I am working on an android application which has 2 features:-
is to remove(erase) a part of the image for a given path, just like the eraser
in photoshop.
is to restore the image for a given path.
I've implemented the erasing part
mEraserPaint = new Paint(1);
mEraserPaint.setDither(true);
mEraserPaint.setAntiAlias(true);
mEraserPaint.setFilterBitmap(true);
mEraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mEraserPaint.setStyle(Paint.Style.STROKE);
mEraserPaint.setStrokeJoin(Paint.Join.ROUND);
mEraserPaint.setStrokeCap(Paint.Cap.ROUND);
mEraserPaint.setStrokeWidth(this.strokeWidth);
mEditingCanvas.drawPath(mPath, mEraserPaint);
And, the repair feature like,
mRepairPaint = new Paint(1);
mRepairPaint.setDither(true);
mRepairPaint.setAntiAlias(true);
mRepairPaint.setFilterBitmap(true);
mRepairPaint.setStyle(Paint.Style.STROKE);
mRepairPaint.setStrokeJoin(Paint.Join.ROUND);
mRepairPaint.setStrokeCap(Paint.Cap.ROUND);
mRepairPaint.setStrokeWidth(this.strokeWidth);
mRepairPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
Bitmap overlay = Bitmap.createBitmap(mOriginalBitmap.getWidth(),
mOriginalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
canvas.drawPath(mPath, mRepairPaint);
mRepairPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(mOriginalBitmap, 0, 0, mRepairPaint);
mRepairPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
mEditingCanvas.drawBitmap(overlay, 0, 0, mRepairPaint);
The problem is that, in the repair feature alpha of the image is not preserved and the image is drawn with full opacity. I need to preserve the alpha of image.
So, I need to know how to repair an image after erasing a part of it, while preserving its alpha.
For a school project I need to make a module to shrink a bitmap. So I made a matrix for this but when I draw directly the bitmap with the matrix like this :
canvas.drawBitmap(Bmp, ShrinkMatrice, p);
This work very fine, but I need to assign this matrix into the Bmp so I used a second canvas like this :
Canvas SuperCanvas = new Canvas(Bmp);
SuperCanvas.drawBitmap(Bmp, ShrinkMatrice, p);
but when I look the result with this :
canvas.drawBitmap(Bmp, 0,0, p);
the result look like this ( see the picture on the link ), the rest of the page is not draw correctly ...
if someone can explain me this problem :/...
Thank you in advance...
(Sorry if I made some error it's my first time on stackoverflow )
I am programming little application for android in OpenGL ES 2.0. Everything was going fine, but today I implemented 2D text drawing. I just create normal canvas, write text on it and then I just load this bitmap as 2D Texture and draw it. This is method I use to change the value of the text.
public void setText(String text){
if(!this.text.equals(text)){
this.text = text;
Bitmap bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
bitmap.eraseColor(0);
Paint textPaint = new Paint();
textPaint.setTextSize(32);
textPaint.setAntiAlias(true);
textPaint.setARGB(0xff, 0x00, 0x00, 0x00);
textPaint.getTextBounds(text, 0, text.length(), bounds);
canvas.drawText(text, 0, bounds.height(), textPaint);
GLES20.glDeleteTextures(1, new int[]{textureHandle}, 0);
this.setTexture(bitmap);
bitmap.recycle();
}
}
I wanted to try it out, so I started to count amount of onDrawFrame calls. It works well, but at the 1045th call it freezes, then it continues for a few more frames and then the app just crashes.
I concluded, that it may be happening because of lack of free memory so I added GLES20.glDeleteTextures(1, new int[]{textureHandle}, 0); to free unnecessary texture from memory, but it haven't change anything.
Any ideas where might be problem?
Thanks Toneks
If setText() is being called by a different thread than the rest of your OpenGL ES code, then that is the problem. All call to OpenGL ES must be made from a single thread on Android. This article gives more detail on this:
http://software.intel.com/en-us/articles/porting-opengl-games-to-android-on-intel-atom-processors-part-1
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?
Im trying to overlay images on a canvas using the following method:
private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
bmOverlay = Bitmap.createBitmap(70, 70, Bitmap.Config.RGB_565);
canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, 0, 0, null); //line 179
canvas.drawBitmap(bmp2, 0, 0, null);
return bmOverlay;
}
However, my app keeps crashing and the log reads:
java.lang.NullPointerException
at android.graphics.Canvas.throwIfRecycled(Canvas.java:954)
at android.graphics.Canvas.drawBitmap(Canvas.java:980)
at com.MyApp.overlay(MyApp.java:179)
Can anyone help?
I had the same trowIfRecycled exception while trying to draw a bitmap to a canvas. I was trying to draw in a thread I started, before the program got around to initializing the bitmap. So in my case: bitmap was null and I had to look for a better place to do the initializing.
Although this in an old question, I found this to be the solution for me. http://nowherenearithaca.blogspot.com/2011/06/solved-bizarre-null-pointer-thrown-in.html
where they suggest
Try doing a clean in eclipse. It seems to be caching sometimes and can get confused. That seemed to solve it for this particular case.