android-canvas

How to draw a circle with radial gradient in a canvas?

随声附和 提交于 2019-12-03 11:42:53
问题 I created a circle button that can change his color when I call a function. What I want is to create another one, that creates the same circle button but with a radial gradient that starts in the middle with the color selected and that goes to transparent when you go out of the circle. I created a similar code using the one posted at How to set gradient style to paint object? but don't worked. The code that I tried is to this porpuse is: mPaint.setShader(new RadialGradient(0, 0, height/3,

How to properly scale a game on Android

落花浮王杯 提交于 2019-12-03 10:39:11
问题 for the sake of simplicity let's assume that I'm making a simple Pong clone game for Android. Let's assume that it would only be playable in the landscape mode. (ignore square phones for now). I'd like the game to look in the same scale on each phone, to the extent that if you took a screenshot of the game on QVGA phone, and resized the screenshot to WVGA size, it would look almost the same as would the game look on WVGA phone. In other words, the paddle's width should always be 1/32 of the

Canvas' drawLine and drawRect not including end position?

时间秒杀一切 提交于 2019-12-03 10:37:36
To my surprise I've just discovered that drawLine and drawRect don't include the ending position, i.e.: canvas.drawLine(100, 100, 100, 100, paint); or RectF rect = new RectF(100, 100, 100, 100); canvas.drawRect(rect, paint); won't draw anything. My paint is defined as follows: Paint paint = new Paint(); paint.setAntiAlias(false); paint.setStyle(Paint.Style.FILL); return paint; I've tried defining my paint as FILL_AND_STROKE, but it wouldn't help. Android's drawPaint() javadoc don't even list the stopX and stopY params! So, if I want to draw a vertical line that goes exactly from beginY to endY

How can I move an image from one point to another using Android Canvas

十年热恋 提交于 2019-12-03 10:21:55
I'm developing a game, and in this game I have to move an image on a Canvas from one point to another in any direction, not just vertical or horizontal. How can I move that image in this manner? After getting a math lecture, it turns out that this is easy to solve. First, we need to get the angle which the target will be moving at. float deltaX = targetX - startX; float deltaY = targetY - startY; float angle = Math.atan2( deltaY, deltaX ); startX/Y can be current X/Y. Now that we have calculated the angle, we can apply it to the current coordinates: currentX += speed * Math.cos(angle);//Using

How to make draw path smooth while drawing with variable width stroke

雨燕双飞 提交于 2019-12-03 10:09:11
问题 I created a sample drawing app where user can draw using variable width stroke, So far drawing path with variable stroke is working, but the lines drawn are not smooth. The code i used to achieve that is shown below. Help me to sort this out as m stuck on this from last two days. Code to draw path using variable stroke width public class FingerPaint extends GraphicsActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new

On Android how do I make oddly shaped clipping areas?

南楼画角 提交于 2019-12-03 10:04:41
Here is how to create a clipping area the shape of a circle: Path path = new Path(); path.addCircle(200,200,100,Direction.CW); c.clipPath(path); // c is a Canvas Now there's a clipping area on the Canvas which prevents drawing anything outside the bounds of that circle. But, what if I want to have the clipping area be shaped like a donut (or whatever)? I tried playing around with creating a second Path and using toggleInverseFillType on it and then adding that to the original path, but that doesn't seem to work. Alternatively, instead of using a Path, is it possible to just create a Bitmap to

How to magnify/zoom part of image

﹥>﹥吖頭↗ 提交于 2019-12-03 09:59:02
问题 I'm making an app where user will be able to click on part of the image and get a magnified version in the corner of WebView . I managed to make a Paint that would make a zoom version, but it displays wrong location, like there's some offset. I know this question has been asked a lot of times and was already answered, but it appears non of those solutions helped. Here's code I've used: @Override public boolean onTouchEvent(@NonNull MotionEvent event) { zoomPos = new PointF(); zoomPos.x =

Relationship between Surface and Canvas: Android

依然范特西╮ 提交于 2019-12-03 09:54:42
What exactly is the relationship between a Surface and Canvas. Please explain. A surface is a buffer. A Canvas holds the drawing. Views are not attached to the Canvas nor the Surface. The window is tied to a Surface and the ViewRoot asks the Surface for a Canvas that is then used by the Views to draw onto. For a detailled anwser, you can read this whole discussion , really interesting. 来源: https://stackoverflow.com/questions/3370212/relationship-between-surface-and-canvas-android

Composite operations in Android Canvas

时光怂恿深爱的人放手 提交于 2019-12-03 08:26:34
I'm just starting with Android development and I'm coming from JavaScript/HTML world so I'm currently investigating the possibilities of the Android SDK. The HTML 5 canvas supports composite operations (See here ). Is this possible in an Android Canvas? I scanned the API of the Canvas class but couldn't find anything useful. I need at least the composite operation "source-in" or (if this isn't possible) "source-atop". Casebash Composition is handled by drawing on a Canvas with a Paint that uses a PorterDuffXfermode . Paint p=new Paint(); p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode

Undo and redo in Canvas for Android

孤街浪徒 提交于 2019-12-03 08:17:55
I am using a customized version of FingerPaint for Android with some other features, like inserting images and moving them. I decided to implement an Undo&Redo, since it will make life just easier. In order to implement it, I finally decided to use a Stack where I push the Drawing Cache of the view, and from where I push the content every time I want to go back to a previous state. So, using the FingerPaint as a basis, I have the following: private void touch_up() { mPath.lineTo(mX, mY); // commit the path to our offscreen mCanvas.drawPath(mPath, mPaint); // I enable the set drawing cache...