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

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

Android自定義view圓并隨手指移動(dòng)

2019-10-22 18:29:12
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了Android自定義view圓并隨手指移動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下

main代碼

public class MainActivity extends AppCompatActivity { private int screenW; //屏幕寬度 private int screenH; //屏幕高度 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Display dis = this.getWindowManager().getDefaultDisplay(); // 設(shè)置全屏 requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN); // 獲取屏幕寬度 screenW = dis.getWidth(); // 獲取屏幕高度 screenH = dis.getHeight(); setContentView(new MyView(this)); } //自定義繪圖類 class MyView extends View { private Paint paint; //定義畫筆 private float cx = 50; //圓點(diǎn)默認(rèn)X坐標(biāo) private float cy = 50; //圓點(diǎn)默認(rèn)Y坐標(biāo) private int radius = 20; //定義顏色數(shù)組 private int colorArray[] = {Color.BLACK,Color.BLACK,Color.GREEN,Color.YELLOW, Color.RED}; private int paintColor = colorArray[0]; //定義畫筆默認(rèn)顏色 public MyView(Context context) {  super(context);  //初始化畫筆  initPaint(); } private void initPaint(){  paint = new Paint();  //設(shè)置消除鋸齒  paint.setAntiAlias(true);  //設(shè)置畫筆顏色  paint.setColor(paintColor); } //重寫onDraw方法實(shí)現(xiàn)繪圖操作 @Override protected void onDraw(Canvas canvas) {  super.onDraw(canvas);  //將屏幕設(shè)置為白色  canvas.drawColor(Color.WHITE);  //修正圓點(diǎn)坐標(biāo)  revise();  //隨機(jī)設(shè)置畫筆顏色  setPaintRandomColor();  //繪制小圓作為小球  canvas.drawCircle(cx, cy, radius, paint); } //為畫筆設(shè)置隨機(jī)顏色 private void setPaintRandomColor(){  Random rand = new Random();  int randomIndex = rand.nextInt(colorArray.length);  paint.setColor(colorArray[randomIndex]); } //修正圓點(diǎn)坐標(biāo) private void revise(){  if(cx <= radius){  cx = radius;  }else if(cx >= (screenW-radius)){  cx = screenW-radius;  }  if(cy <= radius){  cy = radius;  }else if(cy >= (screenH-radius)){  cy = screenH-radius;  } } @Override public boolean onTouchEvent(MotionEvent event) {  switch (event.getAction()) {  case MotionEvent.ACTION_DOWN:   // 按下   cx = (int) event.getX();   cy = (int) event.getY();   // 通知重繪   postInvalidate(); //該方法會(huì)調(diào)用onDraw方法,重新繪圖   break;  case MotionEvent.ACTION_MOVE:   // 移動(dòng)   cx = (int) event.getX();   cy = (int) event.getY();   // 通知重繪   postInvalidate();   break;  case MotionEvent.ACTION_UP:   // 抬起   cx = (int) event.getX();   cy = (int) event.getY();   // 通知重繪   postInvalidate();   break;  }  /*  * 備注1:此處一定要將return super.onTouchEvent(event)修改為return true,原因是:  * 1)父類的onTouchEvent(event)方法可能沒有做任何處理,但是返回了false。  * 2)一旦返回false,在該方法中再也不會(huì)收到MotionEvent.ACTION_MOVE及MotionEvent.ACTION_UP事件。  */  //return super.onTouchEvent(event);  return true; } }}

布局

<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" tools:context="com.example.sn.MainActivity"> <com.example.sn.MainActivity.MyView android:id="@+id/myview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /></RelativeLayout>

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


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 徐闻县| 龙胜| 申扎县| 陇川县| 西青区| 米泉市| 阿鲁科尔沁旗| 德保县| 莱芜市| 南汇区| 吉隆县| 曲阳县| 横峰县| 喜德县| 塔城市| 中阳县| 抚顺县| 嵊州市| 锦州市| 启东市| 来安县| 镇赉县| 和硕县| 稻城县| 宝清县| 康平县| 成武县| 松桃| 潮安县| 兴化市| 柯坪县| 宁武县| 焦作市| 荃湾区| 永济市| 青田县| 沭阳县| 江达县| 历史| 剑河县| 松江区|