How to draw Skewed textview Android - android

If there any way to draw skewed text in Android? I've tried to play with Canvas.skew(dx, dy) and Matrix.preSkew(...) and Matrix.postSkew(...) in onDraw() method of my textview. But with no results. Here is an example of what I have:
And here is an example of what I want to implement:

You should use the Paint.setTextSkewX(float skewX) method. Here is an example.
// call in onDraw(Canvas canvas)
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSkewX(-0.2f);
canvas.drawText("some text", 0, 0, paint);

Related

Android - Skewing a TextView

I want to skew / rotate text to look like the following
I image I would manipulate the canvas in onDraw, but I'm unsure where to start.
You can use the setTextSkewX method of the Paint class:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSkewX(-0.2f);
canvas.drawText("Make It Supreme", 0, 0, paint);
}

DrawText does not work

I can not understand why it does not work the DrawText method. I have to write a text on a photo that was previously cropped. This is the code:
mImageView.buildDrawingCache();
Bitmap bmap = mImageView.getDrawingCache();
Canvas c = new Canvas (bmap);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
c.drawPaint(paint);
paint.setTextSize(20);
c.drawText("Some Text", 0, 0, paint);
the picture is cropped correctly, but I see no text on it. How so?
The text is drawn above the y-coordinate. In your case, the text is drawn above the canvas.
Try changing your draw line to:
c.drawText("Some Text", 0, 25, paint);
You might need to do some calculations to get the coordinates right.

How i can split a textview?

My question in my opinion is basic.
However i don't find information about that and how i can do this.
It is possible split a textview? Put a line in the middle of the textview?
**TextView**
________________
| |
|________________|
| |
|________________|
Other thing.... Imagine that textview have height=100dip. It is possible color only the first 10% of textview? Color only the first 10dp??
Anyone can help?
Thank you four your time and help.
First, You can't split a TextView. But you can achieve by setting a right image as android:setDrawableBottom="yourImage"
You are able to customise a View in Android by overrideing the onDraw method of that View.
Something you might consider would be:
#Override
protected void onDraw(Canvas canvas) {
// paint a line through the centre
Paint paint = new Paint();
canvas.drawLine(0, canvas.getWidth(), canvas.getHeight()/2,
canvas.getHeight()/2, paint);
super.onDraw(canvas);
}
This would draw a line through the centre of the View (in your case a TextView). You could use the same method for your 10%/90% colouring.
Eg.
#Override
protected void onDraw(Canvas canvas) {
// paint a region blue
Paint paint = new Paint();
paint.setColor(Color.BLUE);//or whatever colour you want
canvas.drawRect(0, canvas.getHeight()/10, canvas.getWidth(),
canvas.getHeight(), paint)
super.onDraw(canvas);
}
Draw rect takes the arguments:
canvas.drawRect(left, top, right, bottom, paint)
And there are alternatives where you can pass in the actual drawing Rectangle etc.

Canvas Larger Than Screen

I am drawing a grid and I want it to be larger than the screen size so that a user can drag the screen left/right/up/down to get to the rest of the grid.
What is the best way to do that? I've tried drawing a larger bitmap to the canvas, but didn't get anywhere.
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
Bitmap testBitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888);
canvas.drawBitmap(testBitmap, 0, 0, paint);
canvas.drawPaint(paint);
//other grid drawing code here
}
I used the View's scrollBy() method in the onTouch method of the Activity. It worked.
You can probably use the canvas.translate(x, y) method. That will adjust the origin for your canvas in relation to the screen. So canvas.translate(10, 10) will make you canvas origin (0, 0) be at the point of (10, 10) on the screen. Use a negative translation to scroll the screen.

How to set a background bitmap to a rect or rectF?

Is there a way to set a background image for a rectangle drawn in a canvas ?
For exemple i have the following onDraw method :
protected void onDraw(Canvas canvas) {
this.setBackgroundGradient();
RectF rect = new RectF();
rect.set(0, 0, canvas.getWidth(), 50);
canvas.drawRoundRect(rect, 0, 0, this.paint);
}
private void setBackgroundGradient()
{
this.paint.setShader(new LinearGradient(0, 0,0, getHeight(), 0xff919191, 0xff424242, Shader.TileMode.MIRROR));
}
I would like to change my gradient by a background image (repeatable if possible).
Note : i would rather to keep rectangle and not use drawBitmap.
A Rect is not a drawable, it is a convenience class and only holds the four values that define the rect. Canvas knows how to draw a rect with the Paint object you give it.
If you want to have a background (image) instead of a rect, then you either use drawBitmap on the canvas or have a (bitmap)drawable that you pass the canvas to when drawing.

Categories

Resources