Android surface canvas.drawRect has a decaying shadow effect - android

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?

Related

Trying to draw on canvas using canvas.clipPath

I am trying to draw circle on darken background, trying to achive smth like this -
This actually worked on my Samsung s4 and Samsung tab 3, but won't work on s2 and some emulator (all sorrounding viewgroup is darken, and inside oval too, seems like it does not see my circleSelectionPath). Help me please to find the way to make it work on every device
final Paint paint = new Paint();
paint.setColor(Color.parseColor("#77000000"));
Path circleSelectionPath = new Path();
mRectF.set(l, t, r, b);
circleSelectionPath.addOval(mRectF, Path.Direction.CW);
canvas.clipPath(circleSelectionPath, Region.Op.XOR);
canvas.drawRect(bitmapRect.left, bitmapRect.top, bitmapRect.right, bitmapRect.bottom, paint );
canvas.restore();
bitmapRect contains my viewgroup dimens (for example : 0,0, 500,500)
Got it. Android has a bug with canvas.clipRect, they have optimized it, but on some android apis it simple don't work after optimization ) I found an issue.
So fix - disable hrdware acceleration for this view
(Build.VERSION.SDK_INT <= 19 && mCropShape == CropImageView.CropShape.OVAL) {
//TURN off hardware acceleration
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

how to create a timer- and show it in the canvas

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);
}

why detected object jumps using OpenCV

Ok I have a strange problem. I'll try to describe it as best as I can.
I've learned my app to detect a car when looking on it from the side
Imgproc.cvtColor(aInputFrame, grayscaleImage, Imgproc.COLOR_RGBA2RGB);
MatOfRect objects = new MatOfRect();
// Use the classifier to detect cars
if (cascadeClassifier != null) {
cascadeClassifier.detectMultiScale(grayscaleImage, objects, 1.1, 1,
2, new Size(absoluteObjectSize, absoluteObjectSize),
new Size());
}
for (int i = 0; i < dataArray.length; i++) {
Core.rectangle(aInputFrame, dataArray[i].tl(), dataArray[i].br(),
new Scalar(0, 255, 0, 255), 3);
mRenderer.setCameraPosition(-5, 5, 60f);
}
Now, this code works nice. I mean that i detects cars and it marks them with green rectangle. The problem is that the marked rectangle jumps like hell. I mean even when the phone is hold still the rectangle jumps from left to right to middle. There is never one still rectangle. I hope I've described the problem properly. I would like to stabilizy the marking cause I want to draw an overlay based on it and I can't make it to jump like this
See(1) the parameters for detectMultiScale and it expects
image of type CV_8U. You will need to convert to gray scale image
with COLOR_RGBA2GRAY instead of COLOR_RGBA2RGB
In detectMultiScale, increase the number of neighbours parameter to avoid false positives.
Suggestion: If input is a video stream, don't run
detectMultiScale on every frame. It is slow even if you use LBP
cascades. Try detection in one frame, followed by tracking techniques.

Android drawCircle compatibility problems

I've got custom widget - blinking circle in custom frequency. In my onDraw method i paint a circle.
#Override
protected void onDraw(Canvas c) {
super.onDraw(c);
if (data[mBitIndex] == '1'){
c.drawCircle(c.getWidth() / 2, c.getHeight() / 2, c.getWidth() / 2, mPaint);
}
}
This works perfect on android 4.2.1, but if i try this on android 2.3, no circle shown. I've done some experiments to determine the problem
#Override
protected void onDraw(Canvas c) {
super.onDraw(c);
if (data[mBitIndex] == '1'){
c.drawColor(mPaint.getColor());
}
}
This works as i expected on both versions of android. But, it's a square, not circle, which i want.
How is it even possible to draw square, but not circle? I've searching on the internet, but noone has this problem before or it's only my problem. I don't get this, can anyone explain me where could be mistake?
On Honeycomb (API 13) or earlier, the hardware acceleration is buggy and cannot draw your circle.
Disable hardware acceleration for your view:
setLayerType(View.LAYER_TYPE_SOFTWARE,null);
This worked for me.
Or set targetSDKVersion to 14 or higher (which might cause other issues).

Confusion over Canvas/OpenGl Techniques

I'm a completely newbie to android programming, having done some java for my computing levels but nothing too complex!
I'm working on a game where an object falls down the screen and has to be sorted into the relevant 'box' when it reaches the bottom.
I've got a surface view running with a thread etc, using canvas draw methods, however, i can't for the life of me see how i will be able to make the falling object reach a speed where it'll present a challenge to the user.
Running the thread with a change of 1 in the y direction causes the object to crawl down the screen. Greater changes in Y lead to jumpy graphics.
Would OpenGL make any difference or are there other canvas methods i can implement?
Hope that makes sense!
Thanks in advance
----Thread------
public void run()
{
Canvas canvas;
while(running)
{
canvas = null;
try{
canvas = this.surfaceholder.lockCanvas();
synchronized(surfaceholder)
{
gamepanel.Check();
this.gamepanel.onDraw(canvas);
}
}finally
{
if(canvas != null)
{
surfaceholder.unlockCanvasAndPost(canvas);
}
}
}
}
----SurfaceView-------
protected void onDraw(Canvas canvas){
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gamebackground), 0, 0, null);
SortItOut.sortitout.Letter.draw(canvas);
}
-----Letter----- (Each object is a different letter)
public static void draw(Canvas canvas)
{
y += 1;
canvas.drawBitmap(LetterObject, x, y, null);
}
Those are the methods i would believe are relevant (The Check method is simply to check whether the object has reached the bottom of the screen).
You must load all your bitmaps in the constructor for the SurfaceView, never in onDraw()
Aside from the bitmap loading problem, you can make it fall faster my increasing the rate of the y change. If you do it too much, the box will appear to jump, but I bet you could get away with up to 10 pixel changes before that would happen (experiment).
You would only need to do OpenGL in this case if performance was slowing you down. I don't think that's the case. Although, I would stop loading the bitmap in the onDraw method and put it in the onCreate or some constructor. onDraw gets called hundreds of times and that's killing your app.

Categories

Resources