안드로이드 작은 Demo-둥근 공이 손가락 궤적을 따라 움직인다

2246 단어
eatwhatApp의 클라이언트는 기본적으로 실현되고 후속적으로 수정을 진행할 것이다. 오늘은 인터넷에서 찾은 작은 데모를 만들어서 화면에 둥근 공이 나타나고 손가락을 따라 이동한다.
다음 뷰에서 자바 클래스 DrawView를 상속합니다.
public class DrawView extends View {

	public DrawView(Context context) {
		super(context);
	}
}

속성을 정의하려면 다음과 같이 하십시오.
	//  
	private Paint paint;
	//  X  
	private float currentX;
	//  Y  
	private float currentY;

	public DrawView(Context context) {
		super(context);
		this.paint = new Paint();
		this.currentX = 100;
		this.currentY = 100;
	}

onDraw() 메서드 다시 쓰기:
        @Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
            	//      
		paint.setColor(Color.RED);
		//    ,    (currentX, currentY),  10
		canvas.drawCircle(currentX, currentY, 10, paint);
	}     

onTouchEven 다시 쓰기 () 방법:
        @Override
    public boolean onTouchEvent(MotionEvent event) {
        
        //         x,y
        currentX = event.getX();
        currentY = event.getY();
        
        //    
        invalidate();
        return true;
    }        

 
이후에는 activity에서 이 컨트롤을 인스턴스화할 수 있습니다.
        RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
        DrawView draw = new DrawView(this);
        layout.addView(draw);

 
이렇게 하면 이 데모를 완성할 수 있다.

좋은 웹페이지 즐겨찾기