1.android提供倒影效果,原生方法
/**
* Gives the text a shadow of the specified radius and color, the specified
* distance from its normal position.
*
* @attr ref android.R.styleable#TextView_shadowColor
* @attr ref android.R.styleable#TextView_shadowDx
* @attr ref android.R.styleable#TextView_shadowDy
* @attr ref android.R.styleable#TextView_shadowRadius
*/
public void setShadowLayer(float radius, float dx, float dy, int color) {
mTextPaint.setShadowLayer(radius, dx, dy, color);
mShadowRadius = radius;
mShadowDx = dx;
mShadowDy = dy;
// Will change text clip region
if (mEditor != null) mEditor.invalidateTextDisplayList();
invalidate();
}
(1)参数radius : 表示外发光的扩散范围,值越大越模糊,越小外放光越清楚。
(2)参数dx:x轴的偏移量
(3)参数dy : y轴的偏移量
(4)参数color:外发光的演示
在代码中可以直接代用view的setShadowLayer设置外发光效果
2. 也可以自定义view 设置外发光属性,下面以设置双重外放光背景为例子。
(1)在value目录下面添加attrs.xml
<resources>
<declare-styleable name="ShineTextView">
<attr name="outerShadowColor" format="color" />
<attr name="outerShadowRadius" format="float" />
<attr name="outerShadowDx" format="float" />
<attr name="outerShadowDy" format="float" />
<attr name="bgShadowColor" format="color" />
<attr name="bgShadowRadius" format="float" />
<attr name="bgShadowDx" format="float" />
<attr name="bgShadowDy" format="float" />
</declare-styleable>
</resources>
设置自定义属性:
<attr name="outerShadowColor" format="color" />表示外发光颜色
<attr name="bgShadowColor" format="color" /> 表示背景发光颜色
(2)定义布局xml文件
<com.base.module.gvclauncher2.ui.ShineTextView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/shadow_tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:text="@string/schedule"
android:textColor="#fff1f1f1"
android:textSize="54sp"
app:bgShadowColor="#FF000000"
app:bgShadowDy="1"
app:bgShadowRadius="12"
app:outerShadowColor="#CC2b88f6"
app:outerShadowDy="1"
app:outerShadowRadius="12" />
(3)创建自定义ShineTextView.java
package com.base.module.gvclauncher2.ui;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
import java.util.ArrayList;
import com.base.module.gvclauncher2.R;
/**
* Created by xzx on 18-4-4.
*/
public class ShineTextView extends TextView {
private ArrayList<ShineTextView.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<ShineTextView.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;
}
}
}
a.init()中获取到外发光和背景发光属性
b.onDraw()中通过重复绘制外发光效果
(4)以上在布局xml中定义了属性适合初步设置样式,还可以在代码中设置样式
ShineTextView tv = (ShineTextView)findViewById(R.id.shadow_tv1);
tv.addOuterShadow(12, 0, 0, Color.parseColor("#000000"));
tv.addOuterShadow(12, 0, 0, Color.parseColor("#2b88f6"));
tv.invalidate();
通过addOuterShadow()方法添加自定义属性,然后重新调用invalidate方法使得view进行重绘。
invalidate方法会使得view调用onDraw()方法。当View的appearance发生改变,比如状态改变(enable,focus),背景改变,隐显改变等,这些都属于appearance范畴,都会引起invalidate操作。
(5) 效果如下:
显示底部黑色发光,然后再是蓝色发光,双重发光,快去定制你自己的发光效果吧!!!