6

I am using that "hack". I have read here in stackoverflow.

@Override
public void draw(Canvas canvas) {
    for (int i = 0; i < 20; i++) {
        super.draw(canvas);
    }
}

But my border still smoothie,I wanna put a large and solid border on all my TextView (I already have my component extend a textview).

I have a selector in text color when I click in this text the text color need to change.(It was already working,but I tried to apply another alternative using canvas,in this alternative,I lost this comportment).

enter image description here

Aditi Parikh
  • 1,528
  • 3
  • 12
  • 33
rcorbellini
  • 1,217
  • 1
  • 19
  • 42

2 Answers2

3

This page solve your problem, you can custom the style:

How do I put a border around an Android textview?

You can set a shape drawable (a rectangle) as background for the view.

<TextView android:text="Some text" android:background="@drawable/back"/>

And rectangle drawable back.xml (put into res/drawable folder):

    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <solid android:color="#ffffff" />
   <stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>

You can use #00000000 for the solid color to have a transparent background. You can also use padding to separate the text from the border. for more information see: http://developer.android.com/guide/topics/resources/drawable-resource.html

Community
  • 1
  • 1
dpulgarin
  • 536
  • 4
  • 17
1

Your current solution (the hack) is working fine, you just have to tweak 2 parameters accordingly to get a better "solid" shadow effect.

Parameters

The 1st parameter is the shadow radius of the TextView. This parameter decides how "wide" the blur (shadow) effect will spread behind your letter.

The 2nd parameter is the repeat counter of the for loop that wraps around your TextView's onDraw(...) method. Higher repeat count will get you a more "solid" shadow by trading off the performance.

"Solid" shadow

The rule here is, increment on shadow radius (↑) must always accompany with increment on repeat counter (↑) to achieve the "solid" shadow effect.

Similarly, if you want to gain performance by reducing repeat counter (↓), you have to decrease shadow radius (↓) as well.

Solid shadow TextView

package com.example.solidshadowtext;

import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

public class SolidShadowTextView extends TextView {
    /**
     * Shadow radius, higher value increase the blur effect
     */
    private static final float SHADOW_RADIUS = 10f;

    /**
     * Number of times a onDraw(...) call should repeat itself.
     * Higher value ends up in more solid shadow (but degrade in performance)
     * This value must be >= 1
     */
    private static final int REPEAT_COUNTER = 10000;

    // Shadow color
    private static final int SHADOW_COLOR = 0xff000000;

    public SolidShadowTextView(Context context) {
        super(context);
        init();
    }

    public SolidShadowTextView(Context context, @Nullable AttributeSet attrs) {
       super(context, attrs);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        for (int i = 0; i < REPEAT_COUNTER; i++) {
            super.onDraw(canvas);
        }
    }

    @Override
    public void setShadowLayer(float radius, float dx, float dy, int color) {
        // Disable public API to set shadow
    }

    private void init() {
        super.setShadowLayer(SHADOW_RADIUS, 0, 0, SHADOW_COLOR);
    }
}

Sample

Solid shadow TextView

LightYearsBehind
  • 1,074
  • 12
  • 20
  • I alredy try just up my count of repeat time, but i have one animation in layout with some text and that animmation become very slow in that way. (and i try with just with 150x, ) – rcorbellini Mar 18 '16 at 17:13