您的位置:首页 > 其它

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

2016-07-06 09:51 393 查看
*****************************************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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: