画圆移动

package bwie.com.myview1;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by WSQ on 2017/10/31 0031.
 */

public class MyView extends View {
    private Paint paint;
    private float cx=100f;
    private float cy = 100f;

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

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //创建画笔
        paint=new Paint();
        //设置颜色
        paint.setColor(Color.parseColor("#ff0000"));
        //设置抗锯齿
        paint.setAntiAlias(true);
        //设置画笔粗细
        paint.setStrokeWidth(2);
        //设置是否为空心
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制圆形
        canvas.drawCircle(cx,cy,50,paint);

        paint.setStyle(Paint.Style.STROKE);
        Path path = new Path();
        path.moveTo(100,300);
        path.quadTo(cx,cy,300,300);
        canvas.drawPath(path,paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        cx=event.getX();
        cy=event.getY();
        //刷新view
        invalidate();
        return true;


    }
}




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="bwie.com.myview1.MainActivity">

    <bwie.com.myview1.MyView
        android:layout_width="match_parent"
        android:layout_height="match_parent"></bwie.com.myview1.MyView>

</RelativeLayout>


////////colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

  

posted @ 2017-11-02 08:37  小马哥(马云)  阅读(163)  评论(0编辑  收藏  举报