How i can split a textview? - android

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.

Related

Remove pixel on stroke painted from clipped path Android

I'm using a simple custom view which draw a path and clip its children.
Code used :
private void config() {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setShadowLayer(10.0f, 4.0f, 4.0f, DARK)
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(20);
}
#Override
protected void dispatchDraw(Canvas canvas) {
canvas.save();
circlePath.reset();
circlePath.addCircle(cx, cy, circleRadius, Path.Direction.CW);
canvas.drawPath(circlePath, paint);
canvas.clipPath(circlePath);
super.dispatchDraw(canvas);
canvas.restore();
}
The path is fully transparent with only a white stroke. If there is no child inside this view container everything is fine.
Empty path :
Problem : I have noticed when a child is clipped, it seems that pixels appears around the white stroke, and only inside the path
Path with a black clipped children inside:
I zoomed and resized screenshots to better understand but also in real size pixels are visible.
I tried without shadowLayer but there is still pixels
Could someone tell me what is the problem source and how to fix it ?

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

Draw ring with black shadow

I'm trying to draw a ring with black shadow border. I'm able to achieve this with custom view when I use any color like RED, but what I want is a transparent circle with black shadow border.
Paint mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
mPaint.setShadowLayer(5.5f, 6.0f, 6.0f, Color.BLACK);
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(70, 70, 50, mPaint); }
What's happening here is that it's drawing a filled in transparent circle, and setting the shadow layer to that. Since the circle is transparent, you see the whole shadow of the object through it, not just the edges you're looking for. The shadow is black, so it looks like the whole circle is black.
Try setting the Paint style to Stroke. That should leave the middle transparent, and just draw the shadow of the outer ring. It may draw the shadow in both directions, though(inner and outer), so you may have to adjust the shadow radius accordingly.
mPaint.setStyle(Paint.Style.STROKE);
Are you targeting Android SDK 11 or higher? Since HoneyComb shadow rendring with hardware support had been disabled, you habe to turn on software rendering for this layer. You have to annotate your function for setting up the paint like this:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setUpPaint(){
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
mPaint.setShadowLayer(5.5f, 6.0f, 6.0f, 0x80000000);
/* --- for android:minSdkVersion="11" --- */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(LAYER_TYPE_SOFTWARE, mPaint);
}
}
You'll need to effectively draw the shadow using a fully transparent colour, as you've found so far, then remove that coloured center using PorterDuff.Mode.CLEAR - leaving just the shadow on the outside.
PorterDuffXfermode mXferMode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
// draw the src/dst example into our offscreen bitmap
int sc = canvas.saveLayer(0, 0, 70 + 50, 70 + 50, null,
Canvas.MATRIX_SAVE_FLAG |
Canvas.CLIP_SAVE_FLAG |
Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
Canvas.CLIP_TO_LAYER_SAVE_FLAG);
canvas.drawCircle(70, 70, 50, shadowPaint);
shadowPaint.setXfermode(mXferMode);
canvas.drawCircle(70, 70, 50, shadowPaint);
shadowPaint.setXfermode(null);
canvas.restoreToCount(sc);

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

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.

Categories

Resources