自定义圆角点击变色TextView

项目中经常使用到TextView当作button来使用,显得更加自然,然后每一个TextView的点击事件需要写3个xml

普通状态,按下状态,状态选择。。。<shape ...><shape ...><selector ...>

然后写了一个textView对其进行了一下封装


values下attr.xml中

<declare-styleable name="MyTextView">
    <attr name="corner_Size" format="integer"/>
    <attr name="normal_Color" format="color|reference"/>
    <attr name="press_Color" format="color|reference"/>
</declare-styleable>

MyTextView主要代码:


package com.shj.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

/**
 * Created by SHJ on 2015/12/14.
 */
public class MyTextView extends TextView {
    private int mBgColor = 0;
    private int mCornerSize = 0;
    private int mPressColor = 0;
    private boolean isPress = false;
    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttrs(context, attrs);
        this.setClickable(true);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        if(!isPress) {
            init();
        }
        super.onDraw(canvas);
    }

    private void init() {
        setBackgroundRounded(this.getMeasuredWidth(), this.getMeasuredHeight(), this,mBgColor);
    }

    private void getAttrs(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
        mBgColor = ta.getColor(R.styleable.MyTextView_normalColor, Color.BLUE);
        mCornerSize = ta.getInt(R.styleable.MyTextView_cornerSize, 0);
        mPressColor = ta.getColor(R.styleable.MyTextView_pressColor,Color.RED);
        ta.recycle();
    }

    public void setBackgroundRounded(int w, int h, View v,int color)
    {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        double dH = (metrics.heightPixels / 100) * 1.5;
        int iHeight = (int)dH;
        iHeight = mCornerSize;
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        paint.setAntiAlias(true);
        paint.setColor(color);
        RectF rec = new RectF(0, 0, w, h);
        c.drawRoundRect(rec, iHeight, iHeight, paint);
        v.setBackgroundDrawable(new BitmapDrawable(getResources(), bmp));
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                isPress = true;
                setBackgroundRounded(this.getMeasuredWidth(), this.getMeasuredHeight(), this, mPressColor);
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                isPress = false;
                init();
                break;
        }
        return super.onTouchEvent(event);
    }
}

布局中使用:

<com.shj.myapplication.MyTextView
    android:id="@+id/myText"
    android:text="@string/hello_world"
    android:padding="8dp"
    app:normalColor="@color/blue_300"
    app:pressColor="@color/red_200"
    app:cornerSize="8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

OK,第一次写自己的东西,希望大家多多评价,我是小白中的小白。。。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值