模仿自定义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
- 模仿自定义View的一个小例子
- 一个模仿内建数组的小例子
- 自定义View简单小例子
- 自定义ViewGroup的一个小例子
- Android自定义View之图形图像(模仿360的刷新球自定义一个SeekBar)
- Android自定义View之图形图像(模仿360的刷新球自定义一个SeekBar)
- Android自定义View之图形图像(模仿360的刷新球自定义一个SeekBar)
- 一个模仿Google Suggest的例子
- android自定义View的和FramgentActivity的一个小坑
- 官方的自定义 View 例子
- 关于自定义特性的一个简单的小例子
- Android自定义View:一个精致的打钩小动画
- 一个模仿ping的小程序
- 一个小的例子
- 自定义View的一个例子(这里使用了使用了自定义Button)
- Android模仿打字机效果的自定义View实现
- Android模仿打字机效果的自定义View实现
- Android自定义View模仿QQ主页的开关
- oracle 策略POLICY学习
- iOS获取UIView上某点的颜色值
- Ubuntu下安装ss服务端
- cheng@ARM结构与编程之day3-MOV LSL
- DOM对象(document)
- 模仿自定义View的一个小例子
- 黑马程序员-------面向对象(下)
- IE6 IE7 IE8 IE9 IE10 Css hack及IE条件注释法
- Android中补间动画的实现之二
- 非谓语动词---不定式作名词
- 线程知识学习五——并发
- [转] iOS --- 应用架构谈之二: View层的组织和调用方案
- alpha测试和beta测试
- Activity的生命周期的运行情况