Menu
  • HOME
  • TAGS

How to draw the outline of circle within it, in Android

drawing,android-canvas

After a long search and hit and trial, I found solution to this problem, Hope it will help others- This problem got solved, when I defined my circle in the xml file instead of creating it by code. Add this file, say empty_circle.xml and write this- <?xml version="1.0" encoding="UTF-8"?> <shape...

How to make a drawable fits canvas

android,svg,android-canvas,android-drawable

You could render the SVG to a Picture, and then render that directly to the Canvas, with something along the lines of: public Bitmap renderToBitmap(SVG svg, int requiredWidth){ if(requiredWidth < 1) requiredWidth = (int)svg.getIntrinsicWidth(); float scaleFactor = (float)requiredWidth / (float)svg.getIntrinsicWidth(); int adjustedHeight = (int)(scaleFactor * svg.getIntrinsicHeight()); Bitmap bitmap = Bitmap.createBitmap(requiredWidth,...

Programmatically draw lines between buttons on android

android,android-canvas

based on your provide width and height of your button in layout. I use translation to do that. See the below code and the ugly Image for explain @@! LinearLayout nodesImages = (LinearLayout) findViewById(R.id.nodesImages); View v = new View(this); //this, because I write this code in the Activity class //yah,...

Android matrix rotation of bitmap change center

android,bitmap,rotation,android-canvas,ondraw

So ideal way to solve this problem is translate matrix before rotation and translate it back. Code example: Matrix m = new Matrix(); float px = getWidth()/2; float py = getHeight()/2; m.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2); m.postRotate(angle); m.postTranslate(px, py); canvas.drawBitmap(bitmap, m, null); ...

How to position bitmap using drawBitmap in android canvas

android,android-canvas,android-bitmap

You calculated your second destination rectangle wrong Instead of dst1.set(fruit1Bitmap.getWidth() , 0, this.getWidth()/2, this.getHeight()/2); It should be something like: dst1.set(fruit1Bitmap.getWidth(), 0, fruit1Bitmap.getWidth() + fruit2Bitmap.getWidth(), this.getHeight()/2); Watch out for your right coordinate. This will draw the second fruit next to the first, possibly cropping it if it's too large. If...

How to get Collision details of 2 objects Android

android,bitmap,android-canvas,collision-detection

you have to use range in conditions. It may happen that your speed of moving objects is not 1. so in that case this condition never satisfy. suppose you have 2 objects then source and dest then condition will be as below: // use below condition for x if(source.x >=dest.x...

Sprite movement speed and distance not same on every device

android,sprite,android-canvas,movement,gravity

I have found another solution. This one works well. y= y-(gravity * game.getResources().getDisplayMetrics().density); ...

Android: Drawing stretched/squished in bitmap/canvas

android,bitmap,android-canvas

Yayyyy!! I figured it out! In onDraw(Canvas canvas): Old code: canvas.scale((float) width, (float) height); New Code: // Scale square, so nothing gets stretched or squished canvas.scale((float) width, (float) width); // Crops the canvas canvas.clipRect(0f, 0f, 1f, .33f); Then I just make sure that all my rects are only using the...

Android getX and getY for a custom view: strange behavior

android,xml,android-layout,android-canvas,android-custom-view

pskink is totally right, you set a padding to your "base" linearlayout your plot view so when you call getX() it returns you position of plotview in its parent layout (your base linearlayout) landmark (something corresponding to 16dp). Then when you draw a circle you set circle center in plotview...

android.graphics.Picture not being drawn in API 14+

android,graphics,android-canvas,cyanogenmod

If targetSdkVersion is 14 (Ice Cream Sandwich) or higher then hardware acceleration is enabled by default for the whole app. Unfortunately Canvas.drawPicture() is not supported if hardware acceleration is enabled. There are two way to fix this: Add android:hardwareAccelerated="false" to the application tag in your AndroidManifest.xml. This will disable hardware...

Android weird drawing canvas bug?

android,android-canvas

I don't think it's a software bug. My guess would be that that's the way the display is rendering colors.

How to convert canvas to bitmap in android?

android,bitmap,android-canvas

Your bitmap is black because you didn't draw anything on it. To have some drawing in the bitmap you must indeed create it with this call : bitmap = Bitmap.createBitmap(getDrawingCache()); Then you have a stackoverflow exception simply because : getDrawingCache() call onDraw(Canvas) onDraw(Canvas) call getDrawnMessage(Canvas canvas) getDrawnMessage(Canvas canvas) call getDrawingCache()...

Filling intersection area of two circles in Android

android,android-layout,android-canvas

Use 'Canvas.clipPath' For Example: Path pathA = new Path(); pathA.addCircle(xCenterA, yCenterA, radiusA, Direction.CW); Path pathB = new Path(); pathB.addCircle(xCenterB, yCenterB, radiusB, Direction.CW); canvas.clipPath(pathA); canvas.clipPath(pathB, Region.Op.DIFFERENCE); ...

Android not drawing anything on canvas

android,android-canvas

Try setting a background color on your views. setBackgroundColor(Color.TRANSPARENT);. There is an optimization that it won't draw views that it thinks won't actually draw.

Update Canvas Preserving Old Canvas Details

android,persistence,android-canvas

A Boolean array that tracks the touch state of each rectangle would do the trick. I can't see the external code, but this could be an additional field in your GameView class, and updated appropriately in your handleTouches method. A perhaps less efficient, less elegant solution would be to just...

Fractal generator for android Bitmap / Canvas

android,android-canvas

You can use canvas.drawPoint(): To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing). For...

Draw image inside a View without subclassing

android,canvas,bitmap,android-canvas,android-bitmap

You cannot do that directly -- calling draw(Canvas) on a view will cause that View to perform its drawing commands into the canvas. So in your case that would draw the TextView onto the Canvas you provided (not the other way around). If you needed to do so, you could...

Making game using image views and layout (not canvas)

android,android-canvas

It all depends on the kind of game you want to make. If you are making a grid based game, ImageViews could be more useful. However, almost no one makes Android games using layouts. Most people choose to use SurfaceView because it allows much more to be done, and in...

How can i fill all the screen with canvas

android,colors,background,android-canvas

Modify the layout as below: The issues is due to paddings it is not filling totally <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layout1" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.simmaro.pruebas_canvas_2.MainActivity" > </RelativeLayout> ...

How to draw a cirle in center in android

android,android-layout,android-fragments,android-linearlayout,android-canvas

Use some fake layouts and set the visibility to invisible. This should do the trick. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.example.dd" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:orientation="vertical" > <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="fill_parent"...

Creating a drawable & zoomable image view in android

android,android-canvas,android-imageview,android-view,android-drawable

Answer updated with zoom enable/disable and drawableview activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:id="@+id/enable_zoom" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="disable zoom"/> <com.rbt.zoomdraw.CustomImageView android:id="@+id/zoom_iv"...

Algorithms used in android.graphics.Canvas

android-ndk,android-canvas

Summarizing the comments: There are at least two implementations of the drawing functions, one purely in software (the Skia library), and one that uses the GPU when hardware acceleration is enabled. The Skia implementation is written in C++, not Java. Because it has to take into account Paint features like...

How to set text characters in cubes horizontally and vertically both

android,android-canvas

Do not increase start parameter. canvas.drawText(characterToString, x, m * height + y, foreground); ...

saving drawing to sdcard from a custom view android

android,bitmap,drawing,android-canvas,android-custom-view

On the assumption you want to save the file to the devices storage, here is the solution i use... Add permission to manifest: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> Here is how I am doing it: // Variables i needed private String mFileName; private Bitmap mBitmap; // apply this listener to your button private...

Paint 4 rectangles 16px from the 4 corners

android,android-canvas,drawrect

Here is some code I did pretty fast that ought to help you out & you should be able to optimize int squareSize = 30; int offset = 16; // top left canvas.drawRect(offset, offset, offset+squareSize, offset+squareSize, paint); // top right canvas.drawRect(getWidth() - offset - squareSize, offset, getWidth() - offset ,...

Canvas draw text verticaly

java,android,text,android-canvas,draw

First, you need Paint Paint paint = new Paint(); // your paint settings such as stroke thickness and such Then, rotate your canvas canvas.rotate(yourTextAngle, originX, originY); Then, you draw your text canvas.drawText("Your text", originX, originY, paint); The text should be drawn vertically based on the angle you supplied. Then if...

What to use for drawing in Android - View or SurfaceView?

android,view,android-canvas,draw,surfaceview

If you want to use Canvas, you are increasingly better off with a custom View, rather than a SurfaceView. The simple reason is that Canvas rendering on a View can be hardware accelerated, while Canvas rendering on a SurfaceView's surface is always done in software (still true as of Android...

Place view in activity programmatically

android,android-canvas,paint

You're adding DrawView with LayoutParams.WRAP_CONTENT, which means the system needs to ask the view its width and height. You should implement onMeasure() for that. But I've never done that so don't know the details. Another way is to just use LayoutParams.MATCH_PARENT and draw your stuff in onDraw() centered yourself. In...

An EditText full of horizontal lines

android,android-edittext,android-canvas

Instead of using getLineCount(), maybe you could do something like this: private int getDrawableLineCount() { return getMeasuredHeight() % getLineHeight(); } The line height may need adjusting, but that should get you started....

Android - Drawing on SurfaceView not appearing

android,android-canvas,surfaceview

You need to set zOrder in order to handle ordering of the view drawn on SurfaceView. Try setting setZOrderOnTop for your view : transparentView.setZOrderOnTop(true); Hope it helps ツ...

canvas.drawBitmap bad Location and size (not placing image correctly)

java,c#,android,bitmap,android-canvas

I found the solution using Matrix for set location and scale x,y Bitmap image1=BitmapUtils.decodeBase64(Lie.GeFondo().GetImagen()); Bitmap image2=BitmapUtils.getResizedBitmap(BitmapUtils.decodeBase64(Utilidades.getImagenTomadabase64()),Foto.GetTamano().GetWidth(),Foto.GetTamano().GeHeight()); Bitmap image3=BitmapUtils.getResizedBitmap(BitmapUtils.decodeBase64(Lie.GetBanner().GetImagen()),Lie.GetBanner().GetTamano().GetWidth(),Lie.GetBanner().GetTamano().GeHeight()); Bitmap result =...

Use gpu to draw on canvas

android,performance,opengl-es,android-canvas

It looks like you want to offload some heavy pixel manipulations to the GPU. On Android, you have two major options: OpenCL, but OpenCL support on Android is cumbersome RenderScript But don't underestimate the power that a CPU has, when you use vectorization instructions well. This might require hand-coding the...

Can I draw alpha using just one bitmap?

android,android-canvas

I played with this for some time and I realized that the solution is as simple as switching the two commands in onDraw. Instead of canvas.drawBitmap(bitmap, 0, 0, null); canvas.drawPath(path, paint); use canvas.drawPath(path, paint); canvas.drawBitmap(bitmap, 0, 0, null); Drawing the bitmap last solves the problem. It is still too dark...

2D Moving player with canvas

java,android,android-canvas

Threads! However the handler is not the root of your problem judging by how slow your animation is running and how choppy the fast one is, you are probably using a canvas tied into an ImageView? This is not very fast at all. You should look into using a SurfaceView...

How can I get the X, and Y, coordinates of a tap on a view in Android?

android,coordinates,android-canvas

myView.setOnTouchListener(new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event){ Log.d("coords", event.getX() + " : " + event.getY(); if(..){ // Do stuff.. } } }); ...

Custom View: onMeasure Height vs Canvas.height

android,android-layout,android-canvas

Using canvas.getClipBounds() should give the appropriate sizes, even if the canvas width and height are actually larger.

android-How to border canvas

java,android,android-canvas

You can draw a border using Paint.STYLE.STROKE. You need to do two separate calls to drawCircle(): paint.setStyle(Paint.Style.FILL); paint.setColor(Color.parseColor("#BAB399")); // set fill color canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f, sbmp.getWidth() / 2+0.1f, paint); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(10); // set stroke width paint.setColor(Color.parseColor("#ffffff")); // set stroke color canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f, sbmp.getWidth()...

Draw multiple rectangles with dividers in-between

java,android,android-layout,android-activity,android-canvas

here is full fledged answer it is perfect for you, <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" > <View android:layout_width="5dp" android:layout_height="wrap_content" android:background="#CC3333" /> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#808080" >...

Android color code WHITE conversion to hexadecimal

android,android-canvas,paint,android-custom-view,android-ui

Since color is actually an integer, you can easily convert it to hexadecimal with String.format. It seems you want to ignore the alpha channel so you can filter it out: String.format("#%06X", color & 0xffffff); ...

Android - ImageView vs Canvas

android,android-canvas,android-imageview

Comparing View and Canvas is comparing apples and oranges. Anything that goes onto the screen is in a View. A Canvas just provides a way to draw things; it's used internally by all View types, including ImageView. If you implement a custom View type, a Canvas is the argument to...

Android canvas change background color

android,android-canvas

Is not what I wanted to achieve but is a workaround and maybe is helpful for somebody, I am putting in invisible the second canvas, and then when is ready, I put it visible back. @Override public void lock(String message) { runOnUiThread(new Runnable() { @Override public void run() { canvasFront.setReadyToDraw(false);...

How to overlay bitmap over another bitmap at particular XY position

android,bitmap,android-canvas

Use below code private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) { Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig()); Canvas canvas = new Canvas(bmOverlay); canvas.drawBitmap(bmp1, new Matrix(), null); canvas.drawBitmap(bmp2, x,y, null); return bmOverlay; } Where x and y are actual positions where you have to draw the overlay bitmap....

Bitmap not showing up on canvas

android,android-canvas,surfaceview,android-bitmap

Change your calculation from this: canvasWidth*(memoryManiaHeight/memoryManiaWidth) to this: (canvasWidth*memoryManiaHeight)/memoryManiaWidth If you evaluate (memoryManiaHeight/memoryManiaWidth) first then it will evaluate to zero due to integer division....

Drawing a path in android progressively

android-canvas,android-view,objectanimator

I somehow figured it out how to achieve it. I, however could not directly draw a canvas line with animation though but found out a work around and that actually worked for me. I used a horizontal progress bar instead with a negligible height which will look like a line...

Optimizing `Canvas.drawCircle` with predefined `Path`

android,performance,animation,android-canvas

You are not using a Path here at all, so do not worry about predefined paths. You do not need to optimize your code, unless you see it becomes slow, which should not be the case. One possible optimization is if you draw many times a circle of the same...

Draw on the image instead of image view in android

android,android-canvas,android-view,android-custom-view,android-drawable

You should possibly use SurfaceView instead. It is more advanced compared to ImageView. Refer to this for drawing: http://examples.javacodegeeks.com/android/core/ui/surfaceview/android-surfaceview-example/ This is for zoom: http://android-innovation.blogspot.co.nz/2013/07/how-to-implement-pinch-and-pan-zoom-on.html...

Android: Use postInvalidate() or a direct call to onDraw?

java,android,animation,android-canvas,ondraw

Yes. Calling postInvalidate sets up the canvas to the screen and passes it to the onDraw function, as well as various other pieces of logic. Calling onDraw directly only makes sense if you want to draw a view to somewhere other than the screen. In addition, postInvalidate will cause it...

Draw element along a round rect path Android

android,canvas,android-canvas

This should get you started. //Path for a rounded rectangle. path = new Path(); path.moveTo(rx, 0); path.lineTo(width - rx, 0); path.quadTo(width, 0, width,ry); path.lineTo(width, height-ry); path.quadTo(width, height, width-rx,height); path.lineTo(rx, height); path.quadTo(0,height, 0, height-ry); path.lineTo(0,ry); path.quadTo(0, 0, rx, 0); //valid APIs less than 21. float centerX = (x+width)/2; float centerY =...

Android Drawing Line with 2 points

java,android,android-canvas

Use customview by extending the view class to achieve this: Let's call your custom class say LineView. So this is what Line should look like. LineView.java import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; public class LineView extends View { Paint paint = new Paint(); public LineView(Context context,...

Draw a shape beside another

java,android,android-canvas

There are 3 ways you could accomplish this off the top of my head. The first is to define the second RecfF as 50 pixels further to the right. new RectF(50, 0, 100, 50); The next is to translate the canvas before drawing the second. mRedRect = new RectF(0, 0,...

Android line path morphing animation

android,android-animation,android-canvas,android-view

I suddenly started to look into object animators. The solution is to use one that animates from my current percentage to the new percentage. The result is exactly what I wanted. Here is the updated view: public class ProgressDownload extends View { private static final String LOG_TAG = ProgressDownload.class.getSimpleName(); private...

Divide a Line with Canvas - Android

java,android,android-canvas

The equation of line is not needed. If you want to divide the line into k equal parts, you will need k-1 points. ...

Custom View: Canvas draw methods give API error

android,android-studio,android-canvas,android-view

The drawOval() method that you are using from canvas was added in API 21. public void drawOval (float left, float top, float right, float bottom, Paint paint) - API 21. You should try using the drawOval() with RectF parameter, instead. public void drawOval (RectF oval, Paint paint) - API 1...

Android : How can I keep all drawings?

android,android-canvas,android-view,android-drawable

You should keep track of the lines within a Path component. Add to the path when you have a new line to draw. Then you can draw the path within the onDraw() or clear the path when you need to for whatever reason. Path | Android Developers One example might...

Android - How is Canvas Used

android,android-canvas,surfaceview

I think there is no other ways to draw instead of canvas. You can use SurfaceView to draw any shape even a cricle also. First you ahev to get SurfaceHolder object and using that you can draw anything. Yo can follow these links- first second

Multiple RelativeSizeSpan on same line

android,android-layout,android-canvas,spannablestring

Setting setTextAlign(Paint.Align.CENTER) on TextPaint might be interfering with your spans. Instead, use Layout.Alignment.CENTER on the StaticLayout which should center the text as a whole. mCenterTextLayout = new StaticLayout(mText, textPaint, (int) maxTextWidth, Layout.Alignment.ALIGN_CENTER, 0.85f, 0, false); ...

Save canvas then restore, why is that?

android,android-canvas

What's the point of undoing what we just did! You're not, though. If you're just going off the words, it does sound like that's what might happen, but it actually isn't. Think of it like this: You have a series of really complex translations and rotations you want to...

Best way to track a point on canvas [duplicate]

java,android,android-canvas

This is a trigonometry problem Understand that in your original non-rotated canvas, the point is at a certain angle and a certain length from center... Polar coordinates. Then when the canvas is rotated, it is still at that same location, relative to the canvas, so it's absolute position has the...

Android - calculate arc angle

android,math,canvas,android-canvas,ondraw

You can calculate its rotation using sin and cos. Lets assume that you have zero point A and want to rotate it to point B which is rotated for 30°. Something like this: Basically new point is at (cx+x,cy+y). In this particular case definition of sin and cos would be...

How to set bitmap to canvas?

android,canvas,android-canvas

I believe you are looking for canvas.drawBitmap

Android Game Development, Low FPS on tablet/high FPS on phones

java,android,bitmap,android-canvas

You could check if hardware acceleration is used on your tablet. https://developer.android.com/guide/topics/graphics/hardware-accel.html

Android draw a part of scaled bitmap using canvas

android,android-canvas

do it this way Rect rs = new Rect(); Rect rd = new Rect(); rs.left = rs.top = 0; rs.right = 480; rs.bottom = 800; <calculate destination rectangle from device size> canvas.drawBitmap(myBitmap, rs, rd, null); You can also scale and translate (shift) the entire canvas canvas.scale(float scaleX, float scaleY); canvas.translate(float...

How can I change Canvas at RecycleViewer?

android,android-canvas,android-recyclerview

Use invalidate() method for calling onDraw method. @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.viewx.setText(mopenPrice.get(position)); if(condition){ holder.viewx.setCustomColor(Color.GREEN); holder.viewx.invalidate(); } } public class MyView extends View { private int color = Color.WHITE; public MyView(Context cxt, AttributeSet attrs) { super(cxt, attrs); setMinimumHeight(100); setMinimumWidth(100); } @Override protected void onDraw(Canvas cv) { cv.drawColor(color);...

android- how to change the color of canvas in a view?

android,android-canvas

You can make a setCircleColor to change the color of the circle and call invalidate that will call the View onDraw method. You can also check for invalidate(Drawable drawable). public class MyCustomView extends View { MyCustomView myView; private Paint myCircle; public MyCustomView(Context context){ super(context); initView(); } private void initView(){ myView...

How to resize bitmap when drawing in canvas?

android,bitmap,android-canvas

You can call public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) for this function (Android Doc). The code below should accomplish what you are trying to do: canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvasWidth, canvasHeight), null); ...

How to make the android canvas re-draw every 5 seconds

java,android,android-canvas

You don't really can get the control on when the onDraw will be called: when a view is invalidate then the onDraw will be called at some point in future. There is a fundamental design flaw in your code: the position of player is modified during the execution of the...

Animate rotation of an image in Android

java,android,android-layout,android-canvas,android-wear

In your onCreate() do Matrix matrix = new Matrix(); And in onDraw float angle = (float) (System.currentTimeMillis() % ROTATE_TIME_MILLIS) / ROTATE_TIME_MILLIS * 360; matrix.reset(); matrix.postTranslate(-source.getWidth() / 2, -source.getHeight() / 2); matrix.postRotate(angle); matrix.postTranslate(centerX, centerY) canvas.drawBitmap(source, matrix, null); invalidate(); // Cause a re-draw ROTATE_TIME_MILLIS is the full circle time, e.g. 2000 is...

Appcelerator module not loading (android canvas)

android,canvas,android-canvas,appcelerator

Download and recompile the module and it works fine... hope all is well, ping me if you run into additional issues. ...

How to draw segmented circle like this with certain requirements

android,android-canvas

@Override public void draw(Canvas canvas) { float size = Math.min(getWidth(),getHeight()); paint.setStrokeWidth(size/4); paint.setStyle(Paint.Style.STROKE); final RectF oval = new RectF(0, 0, getWidth(), getHeight()); oval.inset(size/8,size/8); paint.setColor(Color.RED); Path redPath = new Path(); redPath.arcTo(oval, 0, 120, true); canvas.drawPath(redPath, paint); paint.setColor(Color.GREEN); Path greenPath = new Path(); greenPath.arcTo(oval, 120, 120, true); canvas.drawPath(greenPath, paint); paint.setColor(Color.BLUE); Path bluePath =...

How to save objects previously drawn to the Canvas on a redraw?

java,android,graphics,android-canvas,android-bitmap

Draw with a Bitmap: Bitmap mDrawBitmap; Canvas mBitmapCanvas; Paint mDrawPaint = new Paint(); @Override public void onDraw(Canvas canvas) { if (mDrawBitmap == null) { mDrawBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); mBitmapCanvas = new Canvas(mDrawBitmap); } // draw on the btimapCanvas //... and more mBitmapCanvas.drawWhatever(...); // after drawing with the bitmapcanvas, //all...

How do I make multiple drawables from the same resource unique?

java,android,android-canvas,android-drawable

What if you create clones of the Drawable and modify each as required? See Flavio's answer here: Android: Cloning a drawable in order to make a StateListDrawable with filters

Android AnalogClock

java,android,android-canvas,clock

You can rotate the line (startX, startY) and (endX, endY) individually or you can rotate the canvas itself (which I think it is easier). Using your example: Paint myPaint = new Paint(); myPaint.setColor(Color.rgb(0, 0, 0)); myPaint.setStrokeWidth(10); canvas.drawCircle(50, 100, 50, myPaint); Paint p = new Paint(); p.setColor(Color.rgb(250, 250, 250)); p.setStrokeWidth(2); float...

Android: draw a canvas within a fragment

android,android-fragments,canvas,android-canvas

(Just to make this proper): Add the rect RelativeLayout in your fragment_show_profils.xml file and not in fragment_show_rooms.xml. :-)...

WebView.draw(canvas) sometimes crashes in Android Lollipop 5.0+ on some devices - Fatal signal 11 (SIGSEGV)

android,android-ndk,android-webview,android-canvas,android-5.0-lollipop

The issue came from a race condition that sometimes caused my code to draw onto a Canvas that belonged to a recycled Bitmap. The crash can be reproduced with the following code: Bitmap bmp = Bitmap.createBitmap(someRelativeLayoutContainingWebView.getWidth(), someRelativeLayoutContainingWebView.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); bmp.recycle(); someRelativeLayoutContainingWebView.draw(canvas); // SIGSEGV will happen here...

Path draw in opposite direction at canvas when rotate android

android,android-canvas,eraser

I fixed my problem. Actually when i rotate canvas the event.getX() and event.getY() were not map to current rotation of matrix so by adding this line in mMatrix.invert(tempMatrix); in OnDraw() and also map current x,y in OnTouch() by adding this in OnTouch() method . float[] coords = new float[] {...

Canvas object must be the same instance that was previously returned by lockCanvas

java,android,android-canvas,surfaceview

Move the unlockCanvasAndPost() inside the if (myCanvas != null) { statement. My guess is lockCanvas() is returning null, and you're attempting to unlock a null reference. Looking at the source code, the test for "is it the same" comes before the test for "is it locked at all" -- and...

draw text on item menu actionbar

android,android-canvas

I do not know It will work for you or not but try this way,because I have button,add custom item.. <item android:id="@+id/badge" android:actionLayout="@layout/feed_update_count" android:icon="@drawable/shape_notification" android:showAsAction="always"> </item> Button <Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/notif_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="32dp" android:minHeight="32dp"...

Android: Color looks different on canvas vs views

android,android-canvas

The problem is the SurfaceView is not being rendered with 32 bit colors. You will need to get the SurfaceHolder for the SurfaceView. This can be achieved through getHolder() on the view itself. Then just put it into 32 bit mode with: getHolder().setFormat(PixelFormat.RGBA_8888);...

Background just comes up black when using Canvas

android,android-canvas

Add some more code after the line canvas.drawColor(Color.TRANSPARENT); or change the color. Painting a transparent color will show nothing different on the screen. Also, you are missing the super call to onDraw. Here is the android docs for Canvas where you will find many methods for drawing paths, shapes and...

Draw object's trails on a SurfaceView

android,android-canvas,surfaceview

Sort of. The SurfaceView's Surface uses multiple buffers. If it's double-buffered, and you don't clear the screen every frame, then you'll have the rendering from all the odd-numbered frames in one buffer, and all the even-numbered frames in the other. Every time you draw a new frame, it'll flip to...

Move bitmap along a changing path

java,android,bitmap,android-canvas

If you are facing a similar problem to mine then check out Rebound, courtesy of pskink. It solved my issues.

Android custom image view shape

android,imageview,android-canvas

I was looking for the best approach for a long time. Your solution is pretty heavy and doesn't work well with animations. The clipPath approach doesn't use antialiasing and doesn't work with hardware acceleration on certain versions of Android (4.0 and 4.1?). Seems like the best approach (animation friendly, antialiased,...

for loop does not “grow” the circle. I need to “grow” a circle

java,android,animation,android-canvas,circle

Call this.invalidate() to force redraw on next frame: private int x = -1; private int y = -1; private int r = -1; private int stepsLeft = 300; @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (x < 0) { // generate new circle int[] xy = generateXY(); x =...

Update Rect Position on Canvas, Android

android,android-canvas,android-imageview

You can use canvas.translate(x,y); and then draw. Here is some example code: //Clear the canvas otherwise previous drawing will still be there canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); //save the current default drawing position canvas.save(); canvas.translate(value,0); canvas.drawBitmap(starBitmap, 0, 0, null); //restore the default drawing position, otherwise will translate from the last traslated to position...

Draw bitmap and save/share it

android,android-intent,bitmap,android-canvas

The attachment is not readable by other apps because you are saving it to getFilesDir(). Simply change getFilesDir() to Environment.getExternalStorageDir() and other apps will be able to handle the saved image. This also requires you to add the following permission in your AndroidManifest: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> I don't know why...

What is the proper way to use a custom View with the GridView?

android,android-canvas,android-gridview

Your error is that you never called super.onDraw(canvas) @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); //<== this line is needed to begin drawing on the canvas paint.setColor(Color.RED); paint.setStyle(Paint.Style.FILL); paint.setStrokeWidth(1); canvas.drawRect(0, 0, canvas.getWidth()-1, canvas.getHeight()-1, paint); } you need to explicitly set the derived View dimensions using setMinimumWidth() and setMinimumHeight()...

Change color on state change for custom view

android,android-layout,android-canvas,android-view,android-styles

Well, the easiest way to do it would be to change Paint object's color in on touch method of your custom view. You could do it more less like this: @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: paint.setColor(mPressedColor); invalidate(); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: paint.setColor(mNormalColor); invalidate(); break;...

Bitmaps overlap when using canvas.drawBitmap

java,android,android-canvas

Before you draw anything on the canvas: //clear the canvas canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); And now your canvas is clear and you can draw your character. If your onDraw method was in an expanded view class and was triggered by you calling invalidate(), then the canvas would automatically clear itself. But you...

Setting the opacity of drawing canvas in android

android,canvas,bitmap,android-canvas,opacity

The ball shapes are where each line segment overlaps the previous one. You can fix this by using a second image overlaid on top of the image that you are editing. Initialize the overlay image to completely transparent and make it the same size as the image you are editing....

How can we specify the text size, type, density when creating bitmap

android,android-canvas,paint,text-size,drawbitmap

The problem with density was the size of the bitmap itself, while it's small, the printed texts cannot be smooth! Text size: paint.setTextSize(fontSize); density is the same problem like smoothing, so just make it bigger. type: paint.setTypeface(Typeface.create(Typeface.DEFAULT_BOLD, Typeface.BOLD)); color: paint.setColor(Color.BLACK); background color: canvas.drawColor(Color.WHITE); ...