圆的放大和缩小

package com.lisen.view;

import android.annotation.SuppressLint;
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;
import android.view.View.MeasureSpec;

public class Myview extends View {
    // 画笔
    Paint paint = new Paint();
    // 颜色
    int color = Color.RED;
    // 屏幕的宽和高
    private int with;
    private int height;
    // 圆心的位置
    float centerx = 0;
    float centery = 0;
    // 圆的半径
    float radius = 80;
    float padding = 0;

    public Myview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public Myview(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public Myview(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        // 颜色
        paint.setColor(color);
        // 画圆
        canvas.drawCircle(centerx, centery, radius, paint);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        with = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);

        centerx = with / 2;
        centery = height / 2;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
            int bottom) {
        // TODO Auto-generated method stub
        super.onLayout(changed, left, top, right, bottom);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            break;

        case MotionEvent.ACTION_MOVE:
            int count = event.getPointerCount();
            switch (count) {
            case 1:
                float gg = (event.getX() - centerx) * (event.getX() - centerx)
                        + (event.getY() - centery) * (event.getY() - centery);
                float kf = (float) Math.sqrt(gg);

                if (kf <= radius) {
                    centerx = event.getX();
                    centery = event.getY();
                    invalidate();
                }
                break;

            case 2:
                // 计算两手之间的距离
                float disX = Math.abs(event.getX(0) - event.getX(1));
                float disY = Math.abs(event.getY(0) - event.getY(1));
                // 两手之间的距离就是圆的直径
                double sqrt = Math.sqrt(disX * disX + disY * disY);
                // 计算圆的半径,重新刷新数据
                radius = (float) sqrt / 2;
                invalidate();
                break;
            }
            break;
        case MotionEvent.ACTION_UP:

            break;
        }
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值