模仿自定义View的一个小例子

来源:互联网 发布:查看软件的注册表 编辑:程序博客网 时间:2024/04/29 05:05

先看效果图,不会做动图


就是几个圈,能够根据手指触摸的位置来决定绘制粉红色圈的个数

代码如下

public class BitmapCircleView extends View {    private Bitmap backBitmap;//背景图片    private Bitmap backColorBitmap;//背景圆的背景    private Bitmap colorBitmap;//根据计算绘制的图片    private int radius=150;//图片的形成的半径    private int amount=12;//图片总个数    private int number=0;//绘制变色图片的个数    private int bitmapRadius=15;//一个圆形图片的半径大小    private float arc=360/amount;//每张图片的间隔    private int cx=0;//中心坐标    private int cy=0;    public BitmapCircleView(Context context, AttributeSet attrs) {        super(context, attrs);        backBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.back);        backColorBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.backcolor);        colorBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.color);        setFocusable(true);        setClickable(true);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        int width=MeasureSpec.getSize(widthMeasureSpec);        int height=MeasureSpec.getSize(heightMeasureSpec);        int widthMode=MeasureSpec.getMode(widthMeasureSpec);        if(widthMode==MeasureSpec.UNSPECIFIED){            width=height=240;        }        setMeasuredDimension(width,height);        cx=width/2;        cy=height/2;    }    @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()){            case MotionEvent.ACTION_DOWN:                calculateNumber(event);                break;            case MotionEvent.ACTION_MOVE:                calculateNumber(event);                break;            case MotionEvent.ACTION_UP:                calculateNumber(event);                break;        }        invalidate();        return super.onTouchEvent(event);    }    /**     * 用于计算触摸点与中心原点形成的角度     * @param event     */    private void calculateNumber(MotionEvent event) {        float px=event.getX();        float py=event.getY();        float dx=px-cx;        float dy=cy-py;        float degree= (float) Math.atan(Math.abs(dy/dx));        degree= (float) Math.toDegrees(degree);        if(dx>=0&&dy>=0){            degree=90-degree;        }        else if(dx>0&&dy<0){            degree+=90;        }        else if(dx<0&&dy<0){            degree=270-degree;        }        else{            degree+=270;        }        number= (int) Math.round(degree / arc+0.5);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        canvas.drawBitmap(backBitmap, new Rect(0, 0, backBitmap.getWidth(), backBitmap.getHeight()),                new Rect(0, 0, getWidth(), getHeight()), null);        for(int i=0;i<amount;i++){            float degree=-90+arc*i;            //计算图片的中心点            float px= (float) (cx+radius*Math.cos(Math.toRadians(degree)));            float py= (float) (cy+radius*Math.sin(Math.toRadians(degree)));            //图片的绘制坐标            int top= (int) (px-bitmapRadius);            int left= (int) (py-bitmapRadius);            int right= (int) (px+bitmapRadius);            int bottom=(int) (py+bitmapRadius);            if(i<number){                canvas.drawBitmap(colorBitmap, new Rect(0, 0, colorBitmap.getWidth(),colorBitmap.getHeight()),                        new Rect(top, left, right, bottom), null);            }            else {                canvas.drawBitmap(backColorBitmap, new Rect(0, 0, backColorBitmap.getWidth(), backColorBitmap.getHeight()),                        new Rect(top, left, right, bottom), null);            }        }    }}

主要就是利用canvas的方法将图形缩放到指定位置,然后最重要的就是计算触摸点与中心的角度(数学渣真是为难啊)


0 0
原创粉丝点击