国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

自定義控件之畫圓及自定義屬性

2019-11-09 14:58:21
字體:
供稿:網(wǎng)友
自定義畫圓首先需要繼承View類并重寫三個(gè)構(gòu)造方法public class Circle extends View {    //聲明畫筆    Paint opaint;    Paint tpaint;    Paint thpaint;    String text;    int ny;    int wy;    public Circle(Context context) {        super(context);    }    public Circle(Context context, AttributeSet attrs) {        super(context, attrs);        //在構(gòu)造函數(shù)中來讀取attrs中的屬性        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.Circle);        text = ta.getString(R.styleable.Circle_text);        //第一個(gè)參數(shù)相當(dāng)屬性的id也就是屬性的名字,第二個(gè)參數(shù)為默認(rèn)值,它是在自定義布局中配置的屬性不起作用時(shí)進(jìn)行替換的        //顏色返回int類型的值        ny=ta.getColor(R.styleable.Circle_ny,Color.WHITE);        wy=ta.getColor(R.styleable.Circle_wy,Color.YELLOW);        //數(shù)值類型的要使用getDimension,返回float類型的值        float textsize=ta.getDimension(R.styleable.Circle_textsize,1);//記得此處要recycle();        ta.recycle();    }    public Circle(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    PRotected void onDraw(Canvas canvas) {        super.onDraw(canvas);//調(diào)用畫筆        Circl(canvas);    }//重寫onTouchEvent方法點(diǎn)擊在不同位置,提示不同位置    @Override    public boolean onTouchEvent(MotionEvent event) {        int x;        int y;        switch (event.getAction()){            case MotionEvent.ACTION_DOWN:                //得到點(diǎn)擊坐標(biāo)                x= (int) event.getX();                y=(int) event.getY();                int x1=(x-getWidth()/2)*(x-getWidth()/2);                int y1=(y-getHeight()/2)*(y-getHeight()/2);                //判斷點(diǎn)擊位置是否在圓內(nèi)                if (x1+y1<70*70){                    Toast.makeText(getContext(),"小圓內(nèi)",Toast.LENGTH_SHORT).show();                }else if (x1+y1<100*100&&x1+y1>70*70){                    Toast.makeText(getContext(),"圓環(huán)內(nèi)",Toast.LENGTH_SHORT).show();                }else{                    Toast.makeText(getContext(),"圓環(huán)外",Toast.LENGTH_SHORT).show();                }                break;        }        return true;    }    //創(chuàng)建一個(gè)畫圓的方法    public void Circl(Canvas canvas){        //實(shí)例化第一只畫筆        opaint=new Paint();        opaint.setColor(ny);        opaint.setAntiAlias(true);        opaint.setStyle(Paint.Style.FILL);        opaint.setStrokeWidth(1);        //畫小圓        canvas.drawCircle(getWidth()/2,getHeight()/2,70,opaint);        //實(shí)例化第二只畫筆        tpaint=new Paint();        tpaint.setColor(wy);        tpaint.setAntiAlias(true);        tpaint.setStyle(Paint.Style.STROKE);        tpaint.setStrokeWidth(100);        canvas.drawCircle(getWidth()/2,getHeight()/2,100,tpaint);        //實(shí)例化第二只畫筆        thpaint=new Paint();        thpaint.setColor(Color.BLACK);        thpaint.setAntiAlias(true);        thpaint.setStyle(Paint.Style.STROKE);        thpaint.setStrokeWidth(1);        float yh=thpaint.measureText("圓環(huán)");        canvas.drawText("圓環(huán)",(getWidth()-yh)/2,getHeight()/2,thpaint);    }}      在values文件下創(chuàng)建attrs.xml文件存放自定義屬性,代碼如下:
<declare-styleable name="Circle">    <attr name="img" format="reference" />    <attr name="text" format="string"></attr>    <attr name="ny" format="color"></attr>    <attr name="wy" format="color"></attr>    <attr name="textsize" format="dimension"></attr></declare-styleable>     在布局文件的xml中配置自定義屬性的具體內(nèi)容,如下:
<com.bwei.zhaoyangyang20170206.Circle    android:id="@+id/cl"    android:background="#ff00ff00"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    app:ny="#ff00ff"    app:wy="#ffff00"    app:textsize="10dp"    />
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 彭山县| 天气| 多伦县| 尖扎县| 沙坪坝区| 泊头市| 南澳县| 叙永县| 丰原市| 汾阳市| 舟山市| 海盐县| 星子县| 肥乡县| 仙居县| 富源县| 留坝县| 原阳县| 宁化县| 外汇| 咸宁市| 邹平县| 论坛| 二连浩特市| 平武县| 乌拉特后旗| 内黄县| 于田县| 宜兴市| 泸溪县| 隆安县| 遵义市| 通山县| 彰化市| 电白县| 渭南市| 梓潼县| 门源| 盐山县| 丽江市| 木兰县|