Android自定义控件:挑战十秒

March 8, 2022

android自定义控件:挑战十秒

上代码:

public class CircleTextView extends View {
    private int mCircleRadius;
    private boolean mCircleBorder;
    private String mText;
    private Runnable mRunnable;
    private boolean mClicked=false;
    private int mCircleColor;
    private int mCircleTextSize;
    public CircleTextView(Context context) {
        this(context,null);
    }

    public CircleTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,R.attr.def_style_attr);
    }

    public CircleTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,R.style.defStyle);
    }


    public CircleTextView(final Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.circle_attrs,defStyleAttr,defStyleRes);
        int count=array.getIndexCount();

        for(int i=0;i<=count;i++){
            int attr=array.getIndex(i);
            switch (attr){
                case R.styleable.circle_attrs_circleRadius:
                    mCircleRadius=array.getInt(attr,28);
                    break;
                case R.styleable.circle_attrs_circleBorder:
                    mCircleBorder=array.getBoolean(attr,true);
                    break;
                case R.styleable.circle_attrs_circleText:
                    mText=array.getString(attr);
                    break;
                case R.styleable.circle_attrs_circleColor:
                    mCircleColor=array.getColor(attr, GRAY);
                    break;
                case R.styleable.circle_attrs_circleTextSize:
	                    mCircleTextSize=array.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,35,getResources().getDisplayMetrics()));
                    break;
                default:
                    break;
            }
        }
	
        array.recycle();
        Log.i("mytest", count+ String.valueOf(mCircleTextSize) +" "+mText);

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!mClicked){
                    post(mRunnable=new Runnable() {
                        float i=0.000f;
                        @Override
                        public void run() {
            	            i=i+0.02f;
                            mText= String.format("%.2f",i);
                            postInvalidate();
                            postDelayed(this,10);

                        }
                    });
            	    mClicked=true;
                }else {
            	    removeCallbacks(mRunnable);
                    mClicked=false;
            	    if(Float.valueOf(mText)<10.01
                            &&Float.valueOf(mText)>9.99){
            	        Toast.makeText(context, "恭喜你中奖", Toast.LENGTH_SHORT).show();
                    }
            	}


            }
        });
	

    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint=new Paint();
        paint.setColor(mCircleColor);
        canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,Math.min(getMeasuredWidth(),getMeasuredHeight())/2,paint);
        paint.setTextSize(mCircleTextSize);
        paint.setColor(WHITE);
        Rect bound=new Rect();
        paint.getTextBounds(mText,0,mText.length(),bound);
        canvas.drawText(mText,getWidth()/2-bound.width()/2,getHeight()/2+bound.height()/2,paint);

    }
}

Layout:

   <com.xiaoyifei.howmoney.CircleTextView
      android:layout_height="match_parent"
      android:layout_width="match_parent"
       android:background="#4DBA42"
       app:circleColor="#13AA9C"
      app:circleText="开始"
       app:circleTextSize="45sp"

      app:circleRadius="75"
      app:circleBorder="true"/>


</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="def_style_attr" format="reference"/>
    <attr name="circleRadius" format="integer"/>
    <attr name="circleBorder" format="boolean"/>
    <attr name="circleText" format="string"/>
    <attr name="circleColor" format="color"/>
    <attr name="circleTextSize" format="dimension"/>

    <declare-styleable name="circle_attrs">
        <attr name="circleRadius"/>
        <attr name="circleBorder"/>
        <attr name="circleText"/>
        <attr name="circleColor"/>
        <attr name="circleTextSize"/>

    </declare-styleable>
</resources>