Android - Skewing a TextView - android

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);
}

Related

Diamond Shaped Button with transparent borders

I used a customView class for create a diamond shape button.
In onDraw method of this class:
#Override
protected void onDraw(Canvas canvas) {
mPath.moveTo(mWidth/2 , 0);
mPath.lineTo(mWidth , mHigh/2);
mPath.lineTo(mWidth /2 , mHigh);
mPath.lineTo(0 , mHigh/2);
mPath.lineTo( mWidth/2 ,0);
canvas.drawPath(mPath ,mBorderPaint);
super.onDraw(canvas);
}
And borderPaint defined like this:
mBorderPaint = new Paint();
mBorderPaint.setColor(mBorderColor);
mBorderPaint.setStyle(Paint.Style.FILL_AND_STROKE);
borderPaint.setStrokeWidth(mBorderWidth);
But I want my diamond button has a transparent border. What should I do?
You have to draw the path twice, first to draw the fill and then to draw the stroke.
//initialize the paint object before onDraw method is called
mBorderPaint = new Paint();
#Override
protected void onDraw(Canvas canvas) {
mPath.moveTo(mWidth/2 , 0);
mPath.lineTo(mWidth , mHeight/2);
mPath.lineTo(mWidth /2 , mHeight);
mPath.lineTo(0 , mHeight/2);
mPath.lineTo( mWidth/2 ,0);
//setup the paint for fill
mBorderPaint.setAlpha(255);
mBorderPaint.setColor(mBorderColor);
mBorderPaint.setStyle(Paint.Style.FILL);
borderPaint.setStrokeWidth(mBorderWidth);
//draw it
canvas.drawPath(mPath ,mBorderPaint);
//setup the paint for stroke
mBorderPaint.setAlpha(51);
mBorderPaint.setStyle(Paint.Style.STROKE);
//draw it again
canvas.drawPath(mPath ,mBorderPaint);
super.onDraw(canvas);
}

How to draw Skewed textview 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);

android programmatically add rectangle in

Hello i added rectangle in my canvas now i want to add textview or some other view in that rectangle.Suggest me some tutorial also. Thanks in advance
public class DrawView extends View {
public DrawView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect rect = new Rect();
rect.set(20 ,10 ,canvas.getWidth()/2, canvas.getHeight()/2);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
canvas.drawRect(rect, paint);
}
}
You can draw text on your canvas.
Paint mpaint= new Paint();
mpaint.setColor(Color.RED);//set red color for rectangle
mpaint.setStyle(Paint.Style.FILL);//mpaint will fill the rectangle
Paint paint2 = new Paint();
paint2.setColor(Color.GREEN);//green color for text
paint2.setTextSize(30f);//set text size. you can change the stroke width also
#Override
protected void onDraw(Canvas canvas)
{
canvas.drawRect(30, 30, 600, 600, mpaint);
canvas.drawText("hello", 150, 150, paint2);//change x and y according to your needs
}
Resulting snapshot on samsung galaxy s3
Adding a View of any kind is out of question here since you are simply drawing on a canvas. But Canvas.drawText(...) might be exactly what you are looking for.

How do I erase (make transparent) one custom region in a Android's canvas?

I'm overriding Android's ImageView in order to make the corners of my image transparent. I can accomplish that clipping the canvas in my onDraw(Canvas canvas):
#Override
protected void onDraw(Canvas canvas) {
Path clipPath = new Path();
int w = this.getWidth();
int h = this.getHeight();
clipPath.addRoundRect(new RectF(0,0,w,h), 10.0f, 10.0f, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
Unfortunately, it's not possible to antialias this round rectangle, and the result are ugly corners like this:
I know I can clear parts of my canvas with antialiasing using Paint and PorterDuff.Mode.CLEAR, what I don't know is to specify the round corners as the region to be erased. I'm looking for something like this:
#Override
protected void onDraw(Canvas canvas) {
//superclass will draw the bitmap accordingly
super.onDraw(canvas);
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
//this will erase a round rectangle, I need the exact inverse
canvas.drawRoundRect(rect, rx, ry, paint);
}
Is there any way to "erase" not the round rectangle, but it's inverse, ie, the round corners? And what if I just want to erase one of the corners?
Draw using a BitmapShader with a transparent color for your Paint object.
If you just want to erase one of the corners, try drawing it as a Path instead of a RoundRect.
protected void onDraw(Canvas canvas) {
BitmapShader bitmapShader = new BitmapShader(<original drawable>, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(0xFF000000);
paint.setShader(bitmapShader);
canvas.drawRoundRect(rect, rx, ry, paint);
}

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