自定义画一个可以拖动的圆形或图片


*****************************************MainActivity.class中不需要写东西********************************************

                                                         只需要写一个类继承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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值