Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

android - TextView:shadowDx/Dy/Radius in dip?

How can we specify a TextView's shadowDx/Dy/Radius values in dip? Right now I have:

<style name="foo">
  <item name="android:shadowDx">-1</item>
  <item name="android:shadowDy">-1</item>
  <item name="android:shadowRadius">1</item>
</style>

I can't seem to specify "1dip" in these places. Is there a way to do it other than setting them in code and doing the device density multiplication ourselves?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I made an overridden TextView with custom xml attributes to handle this in a convenient way.

Which is also available as a library: https://github.com/rongi/text-view-shadow-dips

public class TextViewShadowDips extends TextView {

public TextViewShadowDips(Context context, AttributeSet attrs) {
    super(context, attrs);

    final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.TextViewShadowDips);
    // App can crash on some devices if shadow radius is more than 25 pixels
    // On Samsung Galaxy S6 this crash happens when you copy a text from an input field
    // https://stackoverflow.com/questions/4866928/ranges-for-radius-in-shadowradius-and-visiblity-in-textview?lq=1
    final float shadowRadius = Math.min(
        attributes.getDimension(R.styleable.TextViewShadowDips_shadowRadius, 0f),
        25f
    );
    final float shadowDx = attributes.getDimension(R.styleable.TextViewShadowDips_shadowDx, 0f);
    final float shadowDy = attributes.getDimension(R.styleable.TextViewShadowDips_shadowDy, 0f);
    final int shadowColor = attributes.getColor(R.styleable.TextViewShadowDips_shadowColor, 0);
    if (shadowColor != 0) {
        setShadowLayer(shadowRadius, shadowDx, shadowDy, shadowColor);
    } else {
        getPaint().clearShadowLayer();
    }
    attributes.recycle();
}

attrs.xml inside "values" resource folder

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="TextViewShadowDips">
        <attr name="shadowRadius" format="dimension"/>
        <attr name="shadowDx" format="dimension"/>
        <attr name="shadowDy" format="dimension"/>
        <attr name="shadowColor" format="color"/>
    </declare-styleable>

</resources>

Usage example:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <my.package.name.TextViewShadowDips 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:shadowColor="#000000"
        app:shadowRadius="2dp"
        app:shadowDx="2dp"
        app:shadowDy="2dp"
        />

</RelativeLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...