闪光的TextView

//闪光的textview

public class ShineTextView extends TextView {



// LinearGradient线性渲染
private LinearGradient mLinearGradient;
// RadialGradient环形渲染
private RadialGradient mRadialGradient;
// 矩阵
private Matrix mGradientMatrix;
// 画笔
private Paint mPaint;
private int mViewWidth = 0;
private int mViewHeight = 0;
// 
private int mTranslateX = 0;
private int mTranslateY = 0;
private int[] colors;


/**
* 构造方法
* @param context
* @param attrs
*/

public ShineTextView(Context context, AttributeSet attrs) {
super(context, attrs);
//获取自定义style,attr
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.textcolors);
//取得color1
String color1 = ta.getString(R.styleable.textcolors_color1);
//取得color2
String color2 = ta.getString(R.styleable.textcolors_color2);
//取得color3
String color3 = ta.getString(R.styleable.textcolors_color3);
               //创建渲染数组
colors = new int[] { Color.parseColor(color1), Color.parseColor(color2), Color.parseColor(color3) };
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (mViewWidth == 0 || mViewHeight == 0) {
mViewWidth = getMeasuredWidth();
mViewHeight = getMeasuredHeight();
if (mViewWidth > 0 || mViewHeight > 0) {
mPaint = getPaint();
/**
* 创建环形渲染 
* float x: 圆心X坐标 
* float y: 圆心Y坐标 
* float radius: 圆的半径 
* colors: 渲染颜色数组 floate[] positions:
*  相对位置数组,可为null,若为null,可为null,颜色沿渐变线均匀分布
*  Shader.TileMode tile:渲染器平铺模式
*/

mRadialGradient = new RadialGradient(50, 50, 40, colors, new float[] { 0, 0.5f, 1 },
Shader.TileMode.CLAMP);


/**
* 创建线性渲染
* float x0: 渐变起始点x坐标
                                * float y0:渐变起始点y坐标
                                * float x1:渐变结束点x坐标
                                * float y1:渐变结束点y坐标
                                * int[] colors:颜色 的int 数组

                                * float[] positions: 相对位置的颜色数组,可为null,  若为null,可为null,颜色沿渐变线均匀分布

                                * Shader.TileMode tile: 渲染器平铺模式

*/

mLinearGradient = new LinearGradient(-mViewWidth, 0, 0, 0, colors, new float[] { 0, 0.5f, 1 },
Shader.TileMode.CLAMP);
mPaint.setShader(mRadialGradient);
mPaint.setColor(Color.parseColor("#ffffffff"));

/**
* 绘制阴影
* radius:阴影半径
                                 * dx:X轴方向的偏移量
                                 * dy:Y轴方向的偏移量
                                 * color:阴影颜色
*/

mPaint.setShadowLayer(3, 2, 2, 0xFFFF00FF);
mGradientMatrix = new Matrix();
}
}
}


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mGradientMatrix != null) {
mTranslateX += mViewWidth / 10;
mTranslateY += mViewHeight / 10;
if (mTranslateX > 2 * mViewWidth && mTranslateY > 2 * mViewHeight) {
mTranslateX = -mViewWidth;
mTranslateY = -mViewHeight;
}
mGradientMatrix.setTranslate(mTranslateX, mTranslateY);


// mLinearGradient.setLocalMatrix(mGradientMatrix);
mRadialGradient.setLocalMatrix(mGradientMatrix);
postInvalidateDelayed(50);
}
}

}


在values文件下定义的xml属性:

dimens文件


<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

</resources>


textattrs文件


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

    <declare-styleable name="textcolors">
        <attr name="color1" format="string" />
        <attr name="color2" format="string" />
        <attr name="color3" format="string" />
    </declare-styleable>

</resources>


最后是在布局中使用:

<com.example.ilikebasketball1.view.ShineTextView
            android:id="@+id/login_sign_in_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|bottom"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="20dp"
            android:text="注册"
            android:textSize="18dp"
            android:textStyle="bold"
            app:color1="#FBF6D6"
            app:color2="#D08AD2"
            app:color3="#4C1B83" />
    </LinearLayout>


 别忘记加上自定义标签

 xmlns:app="http://schemas.android.com/apk/res/com.example.ilikebasketball1"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android中,可以使用SpannableStringBuilder类来实现TextView中嵌套TextView的效果。具体步骤如下: 1. 创建一个外部的TextView,设置它的text为一个SpannableStringBuilder对象。 2. 在SpannableStringBuilder对象中插入需要嵌套的TextView,使用setSpan()方法将TextView对象作为参数传入。 3. 设置TextView的布局参数,使其可以适应外部TextView的尺寸。 以下是示例代码: ``` TextView outerTextView = findViewById(R.id.outer_text_view); SpannableStringBuilder builder = new SpannableStringBuilder(); TextView innerTextView = new TextView(this); innerTextView.setText("Inner TextView"); builder.append("Outer TextView "); builder.setSpan(new MySpannable(innerTextView), builder.length(), builder.length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); outerTextView.setText(builder); outerTextView.setMovementMethod(LinkMovementMethod.getInstance()); class MySpannable extends ClickableSpan { private TextView textView; public MySpannable(TextView textView) { this.textView = textView; this.textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); } @Override public void onClick(View widget) { // Do something when inner TextView is clicked } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setColor(ds.linkColor); // Use link color for inner TextView ds.setUnderlineText(false); } @Override public void drawBackground(Canvas canvas, Paint paint, int left, int right, int top, int baseline, int bottom, CharSequence text, int start, int end, int lnum) { // Do not draw background for inner TextView } } ``` 在上面的代码中,我们创建了一个内部TextView对象,并将它作为参数传入MySpannable类的构造方法中。然后,使用setSpan()方法将MySpannable对象插入到SpannableStringBuilder对象中,从而实现了TextView的嵌套效果。 注意,为了使内部TextView能够响应点击事件,需要调用setMovementMethod()方法并传入LinkMovementMethod.getInstance()。此外,还需要重写MySpannable类的updateDrawState()方法来设置内部TextView的颜色,以及重写drawBackground()方法来取消内部TextView的背景色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值