大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android中view的简单应用---随手指移动的小球

public class DrawView extends View{
    public  float currentX=40;
    public float currentY=50;
    public DrawView(Context context){
        super(context);
    }
    //重写ondraw,通过canvas绘画
    protected void onDraw(Canvas canvas){
        Paint paint=new Paint();
        paint.setColor(Color.RED);
        canvas.drawCircle(currentX,currentY,25,paint);
    }


public class MainActivity extends Activity{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout root=(LinearLayout) findViewById(R.id.root);
        final DrawView drawView=new DrawView(this);

        WindowManager wm = this.getWindowManager();

        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        Toast.makeText(this,"width is"+width+"height"+height,Toast.LENGTH_LONG).show();

        drawView.setMinimumHeight(300);
        drawView.setMinimumHeight(500);
        drawView.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                drawView.currentX=event.getX();
                drawView.currentY=event.getY();
                drawView.invalidate();
                return  true;
            }
        });
            root.addView(drawView);
    }




}

分享题目:Android中view的简单应用---随手指移动的小球
网站网址:http://dzwzjz.com/article/peidid.html
在线咨询
服务热线
服务热线:028-86922220
TOP