主Activiy
package com.example.lodier;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rllt= (RelativeLayout) findViewById(R.id.lt);
final DrawCircle drawCirc=new DrawCircle(this);
drawCirc.setMinimumWidth(10);
drawCirc.setMinimumHeight(10);
rllt.addView(drawCirc);
}
}
DrawCircle
类
package com.example.lodier;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by Administrator on 2016/11/1 0001.
*/
public class DrawCircle extends View {
float x = 40;
float y = 50;
Paint mPaint=new Paint();
//View的构造方法
public DrawCircle(Context context) {
super(context);
}
public DrawCircle(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawCircle(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.BLUE);
canvas.drawCircle(x,y,20,mPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
x=event.getX();
y=event.getY();
//刷新的作用
invalidate();
return true;
}
}