I have developed a drawing program in Android Studio with kotlin and I have placed a seekbar to change the width of the lines, but when I change the seekbar, the lines I have already drawn also change, which is not desirable.
I will upload any part of the code you need.
Just a wild guess here, I'll edit my answer if we get more info.
I'm assuming that you are using the onDraw(Canvas) function or something similar and in this function you are using a Paint to draw the lines each frame.
The problem is probably there, the Paint you are using isn't instanced. It seems that you are using the same object for all the lines, what you have to do is to have a different Paint object for each line or for each state of the seekbar.
Related
we had problems to draw nice & smooth images in java, because they were jagged.
This code solved our problem:
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
but now we are making the same app for android, but we have no clue how to apply similar interpolation or antialiasing to an android app.
(we use eclipse emulator)
Use a Paint object.
create a Paint object p
set p.setFilterBitmap(true)
pass the paint p in your Canvas's draw methods
This will activate (bilinear) filtering that will smoothen the "content" of the bitmap. You can also try to activate anti-aliasing with Paint.setAntiAlias(true), but that will affect only the outer edges of the bitmap (it is pretty handy for text though).
I let my user draw geometries on it. So I store all the data(ie. coordinates) required for each geometries(ie. line, triangle, quads, etc). Now, I'm implementing a paint/brush on it which I don't care about all the points that was inputted. My problem is I need to call canvas.drawColor(Color.WHITE); every time the user modify the geometry resulting to clearing also the paint which is gone now and I don't have access to.
Is there any way to actually separate the two in two canvas and combining it later? I mean, the paint is directly draw on one canvas and the geometry on other canvas and combine it.
Yout can set the backgroundcolor of your View to White.
mView.setBackgroundColor(Color.WHITE);
This way you dont need to use
canvas.drawColor(Color.WHITE);
Check out TouchPaint.java in the API demos included in the Android SDK, for an example of how you can do what you want to do.
I am working on application in which i am drawing a image on the canvas now i want o draw the second image in that canvas.
for example see the image ......
the first image s looking as follow ....
when i click on some place then it will show as follow
but the other thing which is shown in the image (thymin) it should display as fadein in the canvas
how to do it i am not getting any thing can any one help me .....
That's quite complicated by the sounds of it though not impossible to achieve. I think it would take more than the standard View class to achieve this, more like you would need to use the SurfaceView class where you have greater control over what gets drawn and where and when. If you're not familiar with Android 2D graphics then a good place to start would be here.
For drawing the bitmap, you could use canvasObject.drawBitmap() method of Canvas class. drawBitmap takes a paint object as one of its parameters, you could programmatically vary the alpha value of the paint object to create a fade-in effect. Paint class has a setAlpha(value) method to do this. I haven't tried this out myself but this should work.
I have been searching all evening for some way to implement an eraser function for my drawing app. The most common answer is to simply paint the background color or image in, but this solution will not work for my application because I am implementing multiple layers (Gimp/Photoshop style).
The user should be able to draw a line with the brush tools provided in as many layers as they like (the onDraw method of my drawingview draws layer0...layerX on top of each other). Then if they choose the eraser tool it should cause any area of the current layer that they trace over to become transparent.
I cannot seem to find an appropriate class/function built in and am unsure how I could write it myself. I tried to do something like
Paint paint = new Paint();
paint.setAlpha(0);
and then use that Paint object to draw with, but that only draws an 'invisible' line.
I also attempted to use
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
but that just seemed to draw as normal and without effect. I'm probably using it incorrectly, but the Android documentation does not contain a clear description of what it does. I just happened to see it in some examples about modifying bitmaps.
I can supply code as needed, I am just not sure what would be helpful to solve my problem. Being pointed in the right direction would be the biggest help as I have not been successful with Google.
You can find the implementation of eraser function, create new canvas, brush function and save function, on this link :
create android drawing interface
it's have very good tutorial for making drawing app on android using motion event.
I am following the below tutorial:
http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_2D.html
In it,
Canvas canvas.drawOval();
is called in order to have a default circle drawn, that bounces around. My normal way of learning a new graphics framework is to build on this, and then upgrade to images. Normally, this is very simple, but I"m having trouble here. There is no equivalent "drawImage" to the drawOval command (which I'm more used to).
Instead, I'm trying to figure out "drawables".
So, following another tutorial (specifically the default Android "snake" game), I tried doing:
Resources r = this.getContext().getResources();
in my view, then passed the resource object to my ball object, to get:
img = r.getDrawable(R.drawable.ball);
where ball is a file stored in /res/drawable/ball.png
In the ball objects draw method (which I call in the view's onDraw method), I have:
img.draw(canvas);
canvas is passed from onDraw, which is itself passed a canvas. I don't really understand who is calling onDraw (I assume some internal process), but this differs from the snake example where they make their own canvas?
Either way, this doesn't seem to work. I am not getting any image, or any errors. I know that img is at least populated (its not null or anything), but other than that I don't really know what to do...
If this were Ruby (my most familiar language), I'd call inspect on the img to see if actually has anything in it...is there an equivalent for Java (or should I fool around with break points)?
Is there anything obvious that I'm doing wrong? Can I not use the default canvas I'm being passed, even though I clearly can for drawing simple shapes?
Ah, I figured it out:
With the draw oval method, I needed to set the bounds like so:
RectF bounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
But for the drawable object, I have to go one step further and say
img.setBounds(bounds);
and make bounds be a Rect instead of a RectF.
Once that is done, voila, things are rendering.
It didn't occur to me at first that the bounds are how things know where to render themselves, and while you pass the bounds to an oval, you have to SET them to a drawable.