自定义圆角点击变色TextView

来源:互联网 发布:2017剑灵萝莉捏脸数据 编辑:程序博客网 时间:2024/05/07 15:55

项目中经常使用到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,第一次写自己的东西,希望大家多多评价,我是小白中的小白。。。




0 0
原创粉丝点击