
本文实例为大家分享了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圆并随手指移动全部内容,希望文章能够帮你解决Android自定义view圆并随手指移动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)