1. 程式人生 > >自定義TextView自定義,點選生成隨機數

自定義TextView自定義,點選生成隨機數

package day.com.day01_viewall;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


import java.util.ArrayList;


import static android.R.attr.width;
import static android.content.ContentValues.TAG;


/**
 * autour: 周旋
 * date: 2017/9/28 15:06 
 * update: 2017/9/28
 */


public class MyTexxtView extends View{
    /**
     * 需要繪製的文字
     */
    private String mText;
    /**
     * 文字的顏色
     */
    private int mTextColor;
    /**
     * 文字的大小
     */
    private float mTextSize;
    private ArrayList<String> mTextList;
    /**
     * 繪製時控制文字繪製的範圍
     */
    private Rect mBound;
    private Paint mPaint;


    public MyTexxtView(Context context) {
        this(context, null);
    }
    public MyTexxtView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public MyTexxtView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTextList = new ArrayList<String>();
        //獲取自定義屬性的值
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyTextView, defStyleAttr, 0);
        mText = a.getString(R.styleable.MyTextView_mText);
        mTextColor = a.getColor(R.styleable.MyTextView_mTextColor, Color.BLACK);
        mTextSize = a.getDimension(R.styleable.MyTextView_mTextSize, 100);
        Log.v("openxu", "文字總長度:"+mText);
        mPaint = new Paint();
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);
        //獲得繪製文字的寬和高
        mBound = new Rect();
        mPaint.getTextBounds(mText, 0, mText.length(), mBound);
    }
    //API21
//    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
//        init();
//    }


    @Override
    protected void onDraw(Canvas canvas) {
        //繪製文字
        canvas.drawText(mText,88 ,88, mPaint);
        Log.i(TAG, "onDraw:=====6666 ");
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_DOWN:
                //獲取螢幕上點選的座標
                float x=event.getX();
                float y = event.getY();
                float a=(float) (Math.random()*10000);
                mText=""+a;
                invalidate(); //更新檢視


                Log.i(TAG, "onTouchEventyyy:== "+y+"onTouchEventxxx:=="+x);
                //如果座標在我們的文字區域內,則將點選的文字改顏色
                if(x>width-2*textWidth){
                    //點選後,獲取座標代表的單詞的含義
                    index = (int) (y/(height/27));
                    invalidate();//更新檢視
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                //點選擡起後,回覆初始位置。
                Log.i(TAG, "擡起");


               break;
        }
        //這句話不要修改
        return super.onTouchEvent(event);
    }

}

//xml中呼叫

 <day.com.day01_viewall.MyTexxtView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        fanyl:mTextSize="25sp"
        fanyl:mTextColor="#263de9"
        fanyl:mText="1508A"
        />