Android 魔术字体按钮(高亮,阴影效果)

    效果如下,封装记录源码:

 

   double_arrow.png    图标资源

界面:

       <LinearLayout
            android:layout_marginTop="20dp"
            android:layout_gravity="right"
            android:id="@+id/lin_jump"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:layout_marginRight="100dp"
            android:orientation="horizontal">

            <com.xiaoluobei.policecar.view.view.ShineTextView xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center"
                android:gravity="center"
                android:padding="10dp"
                android:text="跳    过"
                android:textColor="@color/white"
                android:textSize="18sp"
                app:bgShadowColor="@color/blue"
                app:bgShadowDy="1"
                app:bgShadowRadius="12"
                app:outerShadowColor="@color/blue"
                app:outerShadowDy="1"
                app:outerShadowRadius="12" />

            <ImageView
                android:layout_width="20dp"
                android:layout_height="15dp"
                android:background="@mipmap/double_arrow" />
        </LinearLayout>

   ShineTextView :

@SuppressLint("AppCompatCustomView")
public class ShineTextView extends TextView {
    private ArrayList<Shadow> outerShadows;

    public ShineTextView(Context context) {
        super(context);
        init(null);
    }

    public ShineTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public ShineTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    public void init(AttributeSet attrs) {
        outerShadows = new ArrayList<Shadow>();
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ShineTextView);
            if (a.hasValue(R.styleable.ShineTextView_bgShadowColor)) {
                float bgRadius = a.getFloat(R.styleable.ShineTextView_bgShadowRadius, 0);
                float bgDx = a.getFloat(R.styleable.ShineTextView_bgShadowDx, 0);
                float bgDy = a.getFloat(R.styleable.ShineTextView_bgShadowDy, 0);
                int bgColor = a.getColor(R.styleable.ShineTextView_bgShadowColor, 0xff000000);
                this.addOuterShadow(bgRadius, bgDx, bgDy, bgColor);
            }
            if (a.hasValue(R.styleable.ShineTextView_outerShadowColor)) {
                float radius = a.getFloat(R.styleable.ShineTextView_outerShadowRadius, 0);
                float dx = a.getFloat(R.styleable.ShineTextView_outerShadowDx, 0);
                float dy = a.getFloat(R.styleable.ShineTextView_outerShadowDy, 0);
                int color = a.getColor(R.styleable.ShineTextView_outerShadowColor, 0xff2b88f6);
                this.addOuterShadow(radius, dx, dy, color);
            }
            a.recycle();
        }
    }

    public void updateShadow() {

    }

    public void addOuterShadow(float r, float dx, float dy, int color) {
        outerShadows.add(new ShineTextView.Shadow(r, dx, dy, color));
    }

    public void clearOuterShadows() {
        outerShadows.clear();
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.setCompoundDrawables(null, null, null, null);
        for (ShineTextView.Shadow shadow : outerShadows) {
            this.setShadowLayer(shadow.r, shadow.dx, shadow.dy, shadow.color);
            super.onDraw(canvas);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {

        } else if (widthMode == MeasureSpec.AT_MOST) {

        } else if (widthMode == MeasureSpec.UNSPECIFIED) {

        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    public static class Shadow {
        float r;
        float dx;
        float dy;
        int color;

        public Shadow(float r, float dx, float dy, int color) {
            this.r = r;
            this.dx = dx;
            this.dy = dy;
            this.color = color;
        }
    }

}

猜你喜欢

转载自blog.csdn.net/Crystal_xing/article/details/84970249