欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义view圆并随手指移动

程序员文章站 2023-12-09 15:20:27
本文实例为大家分享了android自定义view圆并随手指移动的具体代码,供大家参考,具体内容如下 main代码 public class mainacti...

本文实例为大家分享了android自定义view圆并随手指移动的具体代码,供大家参考,具体内容如下

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();
 // 设置全屏
 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; //圆点默认x坐标
 private float cy = 50; //圆点默认y坐标
 private int radius = 20;
 //定义颜色数组
 private int colorarray[] = {color.black,color.black,color.green,color.yellow, color.red};
 private int paintcolor = colorarray[0]; //定义画笔默认颜色

 public myview(context context) {
  super(context);
  //初始化画笔
  initpaint();
 }
 private void initpaint(){
  paint = new paint();
  //设置消除锯齿
  paint.setantialias(true);
  //设置画笔颜色
  paint.setcolor(paintcolor);
 }

 //重写ondraw方法实现绘图操作
 @override
 protected void ondraw(canvas canvas) {
  super.ondraw(canvas);
  //将屏幕设置为白色
  canvas.drawcolor(color.white);
  //修正圆点坐标
  revise();
  //随机设置画笔颜色
  setpaintrandomcolor();
  //绘制小圆作为小球
  canvas.drawcircle(cx, cy, radius, paint);
 }

 //为画笔设置随机颜色
 private void setpaintrandomcolor(){
  random rand = new random();
  int randomindex = rand.nextint(colorarray.length);
  paint.setcolor(colorarray[randomindex]);
 }

 //修正圆点坐标
 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(); //该方法会调用ondraw方法,重新绘图
   break;
  case motionevent.action_move:
   // 移动
   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,在该方法中再也不会收到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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。