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

本文实例为大家分享了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>

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

(0)

相关推荐

  • 基于Android实现随手指移动的ImageView

    ImageView用来显示任意图像图片,可以自己定义显示尺寸,显示颜色等等. 运行效果是这样的(文字说明): 首次进入程序,手指点击屏幕上的任意位置,图片会随之移动. 布局文件 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="

  • Android中View跟随手指移动效果

    最近做了一个项目中,其中遇到这样的需求要求图片移动到手指触碰的地方.具体实现代码如下所示: package com.example.plane; import Android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Display; import android.view.KeyEvent; import android.view.Menu; i

  • Android实现View拖拽跟随手指移动效果

    今天想实现这个功能,但是网上搜索代码,都是利用setPadding,setMargin 等方法去实现的,这在Android 4.0 以前是没问题的,但是,android 4.0 后系统已经提供了更简单的方法给我们用了,就是setTranslationX() 和setTranslationY() .这两个是View的属性方法.现在我就用这两个方法实现一个View可以跟着手指移动拖拽的效果.代码非常非常简单: public class DragView extends TextView { floa

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

    本文实例为大家分享了Android自定义view圆并随手指移动的具体代码,供大家参考,具体内容如下 main代码 public class MainActivity extends AppCompatActivity { private int screenW; //屏幕宽度 private int screenH; //屏幕高度 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(saved

  • Android自定义View圆形和拖动圆、跟随手指拖动效果

    单纯的自定义一个圆非常简单 只需要几步就完成 拖动圆添加实现触摸事件即可 我在第一次自定义View圆遇到的几个Bug: 1.拖动圆的话在xml里面设置的自定义圆的宽和高是它能活动的空间的大小 不是圆控件的大小 如果你定义了100dp 拖动它的时候超过100dp这个距离这个圆就会看不见 就像下面这样 如果想活动于整个屏幕直接给宽和高match_parent属性就好了 2.我在定义充满属性match_parent的时候运行会报错,什么方法都用了就是不行,耐心等待过一会就好了-有可能是studio没来

  • Android自定义View圆形和拖动圆跟随手指拖动

    单纯的自定义一个圆非常简单 只需要几步就完成 拖动圆添加实现触摸事件即可 我在第一次自定义View圆遇到的小问题: 1.拖动圆的话在xml里面设置的自定义圆的宽和高是它能活动的空间的大小 不是圆控件的大小 如果你定义了100dp 拖动它的时候超过100dp这个距离这个圆就会看不见 就像下面这样 如果想活动于整个屏幕直接给宽和高match_parent属性就好了 2.在布局里自定的view会提示编译 点击Build编译一下就好了 下面开始写代码: 先是单纯的创建一个圆形 创建一个类继承View 实

  • Android自定义View画圆功能

    本文实例为大家分享了Android自定义View画圆的具体代码,供大家参考,具体内容如下 引入布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools&q

  • Android自定义View实现箭头沿圆转动实例代码

    具体代码如下所示: //MyCircleView类 public class MyCircleView extends View{ //当前画笔画圆的颜色 private int CurrenCircleBoundColor; private Paint paint; ////从xml中获取的颜色 private int circleBundColor; private float circleBoundWidth; private float pivotX; private float piv

  • Android自定义View倒计时圆

    本文实例为大家分享了Android自定义View倒计时圆的具体代码,供大家参考,具体内容如下 创建attr <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CountDownView"> <!--颜色--> <attr name="ringColor" format=&q

  • Android自定义view实现圆的扩散效果

    本文实例为大家分享了Android自定义View的实现水波纹,供大家参考,具体内容如下 一.实现效果 MainActivity.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.andro

  • Android自定义View实现跟随手指移动的小兔子

    本文实例为大家分享了Android自定义View实现跟随手指移动的小兔子,供大家参考,具体内容如下 自定义的View实现跟随手指的小兔子 按前面的例子新创建一个project,再在project中新创建一个module 将需要的背景图和兔子图片放入mipmap中 将布局管理器改为帧布局管理器 <?xml version="1.0" encoding="utf-8"?> //修改为帧布局管理器FrameLayout <FrameLayout xmln

  • Android自定义View手势密码

    Android 自定义View 当然是十分重要的,笔者这两天写了一个自定义 View 的手势密码,和大家分享分享: 首先,我们来创建一个表示点的类,Point.java: public class Point { // 点的三种状态 public static final int POINT_STATUS_NORMAL = 0; public static final int POINT_STATUS_CLICK = 1; public static final int POINT_STATUS

  • android自定义View圆圈拖动

    本文实例为大家分享了android自定义View圆圈拖动的具体代码,供大家参考,具体内容如下 问题: 1 . 累加问题:"点击坐标"坐标在移动时必须改变位置,不然将导致累加过载 2. 圆形改变问题,每次刷新时圆必将改变位置 3. 图片平移:圆在移动时只要 public class MovingBlockView extends View { //画笔 Paint paint = new Paint(); Region circleRegion; Path circlePath; pri

随机推荐