自定义view可拖动的圆

布局

<LinearLayout 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:orientation="vertical"
    tools:context="com.bawey.mycerlier.MainActivity">
<com.bawey.mycerlier.view.Mycirlir
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

自定义view

package com.bawey.mycerlier.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;

/**
 * Created by zld on 2018/2/24.
 */

public class Mycirlir extends View {

    private Paint paint;
    private int lastX;
    private int lastY;

    public Mycirlir(Context context) {
        this(context,null);
    }

    public Mycirlir(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public Mycirlir(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        paint.setColor(Color.RED);
        paint.setStrokeWidth(3);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(100,100,100,paint);
        super.onDraw(canvas);
    }
//拖动事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                //得到间距
                int mX= (int) event.getRawX();
                int jX= mX- lastX;
                int mY= (int) event.getRawY();
                int jY= mY-lastY;
                //得到原始上下左右坐标
                int l = getLeft();
                int r = getRight();
                int t = getTop();
                int b = getBottom();
                //得到移动后的坐标;
                l = l+jX;
                 r = r+jX;
                 t = t+jY;
                 b = b+jY;
                //判断不超出屏幕
                if(l<0){
                    l=0;
                    r=getWidth();
                }
                if(r<0){
                    r=0;
                    l=getWidth();
                }
                if(t<0){
                    t=0;
                    b=getWidth();
                }
                if(b<0){
                    b=0;
                    t=getWidth();
                }
                //显示赋值的位置
                layout(l,t,r,b);
                lastX=mX;
                lastY = mY;
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;//代表自己消耗事件
    }
}

展示出来就ok了不需要添加权限

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值