android 사용자 정의 view 원형 이동 가능

public class Mycircle2 extends View {

    private Paint paint;
    private int rawX;
    private int rawY;
    private int wid;
    private int he;
    int statusBarHeight1 = -1;
    //    ,       
    //         ,      
    public Mycircle2(Context context) {
        this(context,null);
    }

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

    public Mycircle2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //     
        //   
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //    
        paint.setColor(Color.GREEN);//      
        paint.setStrokeWidth(3);//      

        //            
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        wid = displayMetrics.widthPixels;
        he = displayMetrics.heightPixels;

        //  status_bar_height   ID          
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //    ID        
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
    }
    //       
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(50,50,50,paint);

      /*  paint.setColor(Color.LTGRAY);
        //     
        Path path = new Path();
        path.moveTo(80, 200);//          
        path.lineTo(120, 250);
        path.lineTo(80, 250);
        path.close(); //             
        canvas.drawPath(path, paint);*/
    }

    //    
    //       :
/**
 *   View         ,         ,      (x,y)  。      ,  View       
 * ,        。
 *                ,          。
 *                       ,                         。
 *        ,         ,        ,              。
 *   :
 *                 ,               ,       
 *     event.getRawX() ----     X  
 *     event.getRawY() ----     Y  
 *
 *     event.getX()--------       x
 *     event.getY()--------       Y
 *
 */

    // onTouchEvent       
    //Touch  :1.  ACTION_DOWN,2.  ACTION_UP,3    ACTION_MOVE 4.  ACTION_CANCEL
    //        
    //    ---       
    //    ---     
    //event.getAction()       

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //       
                rawX = (int) event.getRawX();
                rawY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                //         
                int yX = (int) event.getRawX();
                int yY = (int) event.getRawY();
                //           
                //       
                int jX=yX-rawX;
                int jY=yY-rawY;
                //   ,       (    )
                int l=getLeft()+jX;
                int r=getRight()+jX;
                int t=getTop()+jY;
                int b=getBottom()+jY;

                //  
                if(l<0){
                    l=0;
                    r=getWidth();
                }
                if(t<0){
                    t=0;
                    b=getHeight();
                }

                if(r>wid){
                    r=wid;
                    l=wid-getHeight();
                }
                //        ,                  
                if(b>he-statusBarHeight1){
                    //  
                    b=he-statusBarHeight1;
                    t=b-getHeight();

                }

                //       
                layout(l,t,r,b);//   View   
                // lastX,lastY    
                rawX=yX;
                rawY=yY;
                break;
            case  MotionEvent.ACTION_UP:

                break;
        }
        return true;//  true        
    }
}

좋은 웹페이지 즐겨찾기