自定义view

主要实现:点击button之后数字和自定义圆的大小一直增加,后期可以拖动。
建一个attrs的xml(内容如下):

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <attr name="radio" format="integer"/>
    <attr name="Mywidth" format="integer"/>

    <declare-styleable name="CustomView">

        <attr name="radio"/>
        <attr name="Mywidth"/>

    </declare-styleable>

</resources>

自定义view的类CustomView:

public class CustomView extends GridView {

    private int index = 0;
    private int radio;
    private int mywidth;


    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);

        //改变角度和text(就是数字)
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.CustomView_radio:
                    radio = a.getInteger(attr, 0);
                    break;


            }

        }
        a.recycle();


    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画笔paint(圆的)
        Paint paint = new Paint();
        paint.setColor(Color.RED);

        //数字
        Paint paint1 = new Paint();
        paint1.setColor(Color.GREEN);
        paint1.setTextSize(50);


        Paint paint2 = new Paint();
        paint2.setColor(Color.YELLOW);
        //空心的
        paint2.setStyle(Paint.Style.STROKE);
        //圆的宽度
        paint2.setStrokeWidth(mywidth);

        canvas.drawCircle(300f, 300, radio, paint);
        canvas.drawText(index + "", 230f, 300f, paint1);

        canvas.drawCircle(300f, 300, 130, paint2);

    }


    public void setInteger(int radio) {

        this.radio = radio;

    }


    public void setText(int text) {

        this.index = text;

    }




    private int lastX = 0;
    private int lastY = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:

               if (index == 100){

                int dx = (int) event.getRawX() - lastX;
                int dy = (int) event.getRawY() - lastY;

                int left = getLeft() + dx;
                int top = getTop() + dy;
                int right = getRight() + dx;
                int bottom = getBottom() + dy;

                layout(left, top, right, bottom);
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();


               }
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                break;
        }
        return true;
    }





}

activity-main类的代码

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="点击"

        />

    <com.bwie.myweektest.CustomView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:id="@+id/cv"
        app:Mywidth="30"
        app:radio="10"
        >

    </com.bwie.myweektest.CustomView>

Mainactivity类

public class MainActivity extends AppCompatActivity {


    private CustomView cv;
    private Button button;

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    cv.setInteger(25);
                    cv.invalidate();

                    break;

                case 2:

                    cv.setInteger(50);
                    cv.invalidate();

                    break;

                case 3:

                    cv.setInteger(75);
                    cv.invalidate();

                    break;

                case 4:

                    cv.setInteger(100);
                    cv.invalidate();

                    break;

                case 5:

                    cv.setInteger(125);
                    cv.invalidate();

                    break;

                case 6:


                    Object obj = msg.obj;
                    cv.setText((Integer) obj);
                    cv.invalidate();


                    break;

            }


        }
    };


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

        initView();


    }


    private void initView() {
        cv = (CustomView) findViewById(R.id.cv);

        button = (Button) findViewById(R.id.btn);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread() {

                    @Override
                    public void run() {
                        super.run();

                        for (int i = 1; i < 6; i++) {

                            try {

                                for (int j = (20 * i) - 20; j < (20 * i) + 1; j++) {

                                    sleep(50);
                                    Message message = new Message();
                                    message.obj = j;
                                    message.what = 6;
                                    handler.sendMessage(message);

                                }

                                handler.sendEmptyMessage(i);

                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                        }

                    }
                }.start();


            }
        });


    }
}

就OK了 ^_^。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值