XML: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/relativeLayout" tools:context="com.example.lianxi.MainActivity"> <com.example.lianxi.View.BallView android:id="@+id/ball" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> Activity: package com.example.lianxi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.RelativeLayout; import com.example.lianxi.View.BallView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RelativeLayout container =(RelativeLayout) findViewById(R.id.relativeLayout); BallView ballView = new BallView(this); container.addView(ballView); } } BallView: package com.example.lianxi.View; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; /** * Created by 你家大林哥 on 2018/4/12. */ public class BallView extends View { private Paint paint; Context context; private int x =18; private int y = 18; private int radius = 188; public BallView(Context context) { super(context); this.context = context; } public BallView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); this.context = context; } public BallView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.WHITE); paint = new Paint(); paint.setColor(Color.RED); paint.setAntiAlias(true); canvas.drawCircle(x,y,radius,paint); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: x =(int)event.getX(); y = (int)event.getY(); System.out.println("按下时:"+"x坐标:"+event.getRawX()+""+"y坐标:"+event.getRawY()); case MotionEvent.ACTION_MOVE: x =(int)event.getX(); y = (int)event.getY(); System.out.println("移动时:"+"x坐标:"+event.getRawX()+""+"y坐标:"+event.getRawY()); case MotionEvent.ACTION_UP: x =(int)event.getX(); y = (int)event.getY(); System.out.println("抬起时:"+"x坐标:"+event.getRawX()+""+"y坐标:"+event.getRawY()); break; } WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); int width = manager.getDefaultDisplay().getWidth(); int height = manager.getDefaultDisplay().getHeight(); if (x >= 18 && y >= 18 && x <= width - 18 && y <= height - 18){ postInvalidate(); } return true; } }
Android圆球随鼠标而移动
猜你喜欢
转载自blog.csdn.net/nijiadalinge/article/details/79911618
今日推荐
周排行