android-canvas

How can I get the bitmap of the canvas I get in onDraw?

故事扮演 提交于 2019-11-28 13:16:51
How can I create the bitmap from the canvas of custom view. There is no way to extract the Bitmap out of a Canvas . The only way you can access it is to pass it yourself when creating the canvas like this new Canvas(myBitmap) and keep the reference. EDIT2: see @Alex comment blow - the approach of passing a Bitmap to the Canvas does not seem to work for more recent versions of Android. EDIT : If you don't create the Canvas yourself, you could create a screen-sized Bitmap (or whatever size you need) and then pass it to the Canvas in onDraw calls like this: canvas.setBitmap(myBitmap) . While

Clear canvas in button click

六月ゝ 毕业季﹏ 提交于 2019-11-28 12:16:43
问题 I used following code to draw. I want to clear the previously drawn lines if the clear button is clicked. public class MainActivity extends Activity { private ArrayList<Path> _graphics = new ArrayList<Path>(); private Paint mPaint; Activity activity; View mView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); activity = this; mView = new DrawingView(this); activity.addContentView(mView, new LayoutParams

How to draw path with variable width in canvas

爱⌒轻易说出口 提交于 2019-11-28 11:49:13
I am using the following line of code to draw path on a Canvas, So far everything works fine and i can easily draw path using this code. But now our requirement is to draw path with variable width, means the path user draw is based on the pressure applied by the user, I mean to say if the user applied light pressure the path will be thin and if the user applied high pressure the path will be thick and so on. So far i succeeded in drawing path with variable width also, but the line drawn are not smooth. Why its happening so, is anything i miss in my code Help me to short this out. Code that I

How to outline a TextView?

 ̄綄美尐妖づ 提交于 2019-11-28 11:34:24
What I want to do? (blue will be changed as white) What I did? I have found a class which extends TextView that able to outline textview very close to what I want. The problem is that I could not change stroke color to any color, it draws always as black. How to set border color as white? What is my output: Where are my codes? public class TypeFaceTextView extends TextView { private static Paint getWhiteBorderPaint(){ Paint p = new Paint(Color.WHITE); return p; } private static final Paint BLACK_BORDER_PAINT = getWhiteBorderPaint(); static { BLACK_BORDER_PAINT.setXfermode(new

How to draw large sized text on a canvas?

不问归期 提交于 2019-11-28 11:33:11
I want to draw on canvas month's text vertical along screen height. Paint init: this.paint = new Paint(); this.paint.setAntiAlias(true); this.paint.setDither(true); this.paint.setSubpixelText(true); this.paint.setColor(color_text_dark); this.paint.setTextAlign(Align.RIGHT); Drawing: // Set the scale to the widest month float scale = getHeight() / this.max_month_width; String month_string = FULL_MONTH_NAME_FORMATTER. format(active_month_calendar.getTime()); canvas.save(); canvas.translate(getWidth(), 0); canvas.rotate(-90); canvas.scale(scale, scale); canvas.drawText(month_string, 0, 0, this

Saving canvas to bitmap on Android

与世无争的帅哥 提交于 2019-11-28 11:31:31
I'm having some difficulty with regards to placing the contents of a Canvas into a Bitmap. When I attempt to do this, the file gets written with a file size of around 5.80KB but it appears to be completely empty (every pixel is '#000'). The canvas draws a series of interconnected lines that are formed by handwriting. Below is my onDraw for the View. (I'm aware that it's blocking the UI thread / bad practices/ etc.., however I just need to get it working) Thank you. @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); if (IsTouchDown) { //

How to make a bitmap using canvas clickable?

泄露秘密 提交于 2019-11-28 10:14:54
How do i make the bitmap created clickable? Below is the code which i have used to create a bitmap using canvas. public class DrawView extends View implements OnClickListener { public DrawView(Context context) { super(context); paint = new Paint(); image = BitmapFactory.decodeResource(getResources(), R.drawable.andmrktsmall); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int canWidth = canvas.getWidth(); int canHeight = canvas.getHeight(); int width = (canWidth - 200) / 2; int height = (canHeight - 100) / 2; Bitmap indexcanvas = Bitmap.createScaledBitmap(image, 200,

How to draw filled triangle on android Canvas

跟風遠走 提交于 2019-11-28 09:03:22
I have class MyView that extends View class. MyView should draw filled triangle. I drew a triangle but I cannot get it filled. This is my onDraw() method: @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); paint.setColor(android.graphics.Color.BLACK); canvas.drawPaint(paint); paint.setStrokeWidth(4); paint.setColor(android.graphics.Color.RED); paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setAntiAlias(true); Point a = new Point(0, 0); Point b = new Point(0, 100); Point c = new Point(87, 50); Path path = new Path(); path.setFillType(FillType

Android Bitmap/Canvas offset after scale

∥☆過路亽.° 提交于 2019-11-28 08:48:57
If I have a canvas, on which I draw a Bitmap like this: canvas.drawBitmap(bmLargeImage, srcRect, destRect, paint); and I scale the bitmap: canvas.scale(1.5f, 1.5f, 450, 250); I want to get the position of the Bitmap after the scale. If the position before scale was (0, 0), after scale there is a offset and I need that offset.. how can I get it? Thanks and sorry for the simple question, newbie here... Ok lets try to work out the best formula for this canvas.scale(scaleX, scaleY, pivotX, pivotY); if (scaleX >= 1){ objectNewX = objectOldX + (objectOldX - pivotX)*(scaleX - 1); }else{ objectNewX =

Canvas.clipPath(Path) not clipping as expected

不打扰是莪最后的温柔 提交于 2019-11-28 08:18:52
I'm trying to clip a canvas drawing operation to an arc-shaped wedge. However, I'm not getting the intended result after setting the clipping path to the canvas. For illustration, here is what I'm doing: path.reset(); //Move to point #1 path.moveTo(rect.centerX(), rect.centerY()); //Per the documentation, this will draw a connecting line from the current //position to the starting position of the arc (at 0 degrees), add the arc //and my current position now lies at #2. path.arcTo(rect, 0, -30); //This should then close the path, finishing back at the center point (#3) path.close(); This works,