国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 洪湖市| 申扎县| 监利县| 贺州市| 内江市| 阿图什市| 雅安市| 仁怀市| 河津市| 班玛县| 临沭县| 慈溪市| 贵溪市| 白银市| 盘锦市| 页游| 楚雄市| 临漳县| 福贡县| 肥东县| 阆中市| 土默特左旗| 苍溪县| 色达县| 建昌县| 原阳县| 土默特右旗| 定西市| 宿州市| 金寨县| 齐齐哈尔市| 海原县| 安新县| 霍山县| 万载县| 翼城县| 荆门市| 故城县| 德庆县| 河池市| 息烽县|