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

首頁 > 系統(tǒng) > Android > 正文

Android自定義View畫圓功能

2019-12-12 01:57:36
字體:
供稿:網(wǎng)友

本文實例為大家分享了Android自定義View畫圓的具體代碼,供大家參考,具體內(nèi)容如下

引入布局

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:id="@+id/activity_main"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:paddingBottom="@dimen/activity_vertical_margin"  android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  tools:context="com.bwie.test.xuejian1508a20170928.MainActivity">   <com.bwie.test.xuejian1508a20170928.ViView  android:layout_width="match_parent"  android:layout_height="match_parent"   /> </RelativeLayout>

自定義View的java類,繼承View

public class ViView extends View{  Paint paint;  Context context;  //構(gòu)造方法  public ViView(Context context) {  super(context);  }   public ViView(Context context, AttributeSet attrs) {  super(context, attrs);  }   public ViView(Context context, AttributeSet attrs, int defStyleAttr) {  super(context, attrs, defStyleAttr);  }   public ViView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {  super(context, attrs, defStyleAttr, defStyleRes);  this.context=context;  }   @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  super.onMeasure(widthMeasureSpec, heightMeasureSpec);  }   @Override  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {  super.onLayout(changed, left, top, right, bottom);   }   /*繪圖*/  @Override  protected void onDraw(Canvas canvas) {  super.onDraw(canvas);  //得到屏幕寬高  int width = getWidth();  int radius = width - 450/2;  int height = getHeight(); // 創(chuàng)建畫筆  Paint paint1 = new Paint();  Paint paint2 = new Paint();  Paint paint3= new Paint(); // 消除鋸齒  paint1.setAntiAlias(true);  paint2.setAntiAlias(true);  paint3.setAntiAlias(true);  //畫筆顏色  paint1.setColor(Color.RED);  paint2.setColor(Color.WHITE);  paint3.setColor(Color.BLUE); // 畫圓。確定位置 // canvas.drawRect(100,100,width/2,height/2,paint1); // canvas.drawCircle(100,100,100,paint1); // canvas.drawCircle(250,250,200,paint2); // canvas.drawCircle(500,500,300,paint3);  //設(shè)置圓環(huán)形狀和大小  RectF oval = new RectF(width-radius,width-radius,width+radius,width+radius);  paint1.setStrokeWidth(450);  canvas.drawArc(oval,-90,90,false,paint1);   canvas.drawCircle(width/2,height/2,450,paint1);  canvas.drawCircle(width/2,height/2,300,paint2);  canvas.drawCircle(width/2,height/2,200,paint3);  }   @Override  public boolean onTouchEvent(MotionEvent event) {  return super.onTouchEvent(event);  } } 

效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 海晏县| 民丰县| 广州市| 新干县| 平顶山市| 永德县| 宜兰县| 海门市| 临高县| 商南县| 马关县| 莆田市| 新乡县| 华宁县| 庐江县| 荔波县| 孝昌县| 宿迁市| 名山县| 绵阳市| 额济纳旗| 临汾市| 嘉鱼县| 凤山县| 长泰县| 寿光市| 太白县| 蓬莱市| 红河县| 衡山县| 东方市| 颍上县| 忻州市| 南木林县| 吉安市| 彝良县| 秭归县| 麻江县| 拉孜县| 双流县| 宁乡县|