Android如何实现自定义view圆并随手指移动

发布时间:2021-06-30 11:00:18 作者:小新
来源:亿速云 阅读:159

这篇文章主要介绍了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>

感谢你能够认真阅读完这篇文章,希望小编分享的“Android如何实现自定义view圆并随手指移动”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. Android中如何实现EditText随输入法一起移动并悬浮在输入法之上
  2. Android自定义View实现带4圆角或者2圆角的效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android view

上一篇:Android如何实现自定义View中attrs.xml

下一篇:Android如何实现自定义组件跟随自己手指主动画圆

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》