自定义画一个可以拖动的圆形或图片
2016-07-06 09:51
393 查看
*****************************************MainActivity.class中不需要写东西********************************************
只需要写一个类继承View就可以了
********************************************BallsView.class************************************************************
******************************************mian布局***************************************
* 类型复制继承View的类的包名即可*
只需要写一个类继承View就可以了
********************************************BallsView.class************************************************************
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.util.FloatMath; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.Toast; public class BallsView extends View { private static final String TAG = "BallsView"; private int height; private int width; private float x; private float y; private float radius = 60;//设置半径为60 private boolean onBall; public BallsView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // 写逻辑。。。 initView(); } /** * xml中使用的时候 * * @param context */ public BallsView(Context context, AttributeSet attrs) { this(context, attrs, 0); } // 代码中new BallsView(); public BallsView(Context context) { this(context, null); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 获取当前控件的高度 height = this.getHeight(); // 获取当前控件的宽度 width = this.getWidth(); x = width / 2; y = height / 2; } boolean flag = true; // canvas 画布 private Bitmap bitmap; private int bh; private int bw; @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (flag) { // bitmap = BitmapFactory.decodeResource(getResources(), // R.drawable.ic_launcher);//图片 // bh = bitmap.getHeight() / 2; // bw = bitmap.getWidth() / 2; flag = false; } // 创建画笔 Paint paint = new Paint(); //设置颜色为黑色 paint.setColor(Color.BLACK);//RED canvas.drawCircle(x, y, radius, paint); // canvas.drawBitmap(bitmap, x - bw, y - bh, null);//图片 // Log.i(TAG, "onDraw......." + "x" + x + "y" + y); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 获取到当前位置 float downX = event.getX(); float downY = event.getY(); onBall = isOnBall(downX, downY); Toast.makeText(getContext(), onBall + "", 0).show(); break; case MotionEvent.ACTION_MOVE: if (onBall) { x = event.getX(); y = event.getY(); // 调用到onDraw // invalidate(); postInvalidate(); } break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_CANCEL: break; default: break; } return true; } private void initView() { } /** * 计算当前手指是否落在小球上 * * @param downX * @param downY * @return */ public boolean isOnBall(float downX, float downY) { float sqrt = FloatMath.sqrt((downX - x) * (downX - x) + (downY - y) * (downY - y)); if (sqrt <= radius) { return true; } return false; } }
******************************************mian布局***************************************
* 类型复制继承View的类的包名即可*
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.day1rikao.BallsView android:id="@+id/ballsView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
相关文章推荐
- bootstrap栅格系统的div高度怎样定?
- 几种常用的数据库连接池
- IOS代码笔记之文字走马灯效果
- tee 命令重定向输出到多个文件
- 图算法6之1008
- 让Scrapy的Spider更通用
- spring(错误二) Error creating bean with name 'xxxx'
- wget ftp
- iOS中集成ijkplayer视频直播框架
- gcc编译器下pow函数编译的问题
- JAVA Math 常用方法
- Redis Java客户端jedis工具类以及Redis实现的跨jvm的锁
- Python开发一个堡垒机
- mysql存储过程之游标遍历数据表
- 李洪强经典面试题25(选择题)
- tar解压命令
- 《招聘一个靠谱的iOS》面试题参考答案(上)
- 关于EditText输入密码时的限制
- HTTP深入浅出 http请求
- linux下crontab任务调度指令