安卓自定义一个全角度的方向盘View
2018-01-04 18:48
176 查看
近日在制作一款坦克小游戏的过程中,想要实现方向盘控制坦克的移动
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wronged.gif)
;
绞尽脑汁终于实现了这个小功能
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
;
应该有很多不合理的地方
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wail.gif)
;
但是楼主是一个才学安卓一年不到的小白
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wronged.gif)
;
1xian贴MainActivity的代码
2zai贴WheelView的代码;
2zuihou贴xml布局文件代码
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wronged.gif)
;
绞尽脑汁终于实现了这个小功能
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
;
应该有很多不合理的地方
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wail.gif)
;
但是楼主是一个才学安卓一年不到的小白
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wronged.gif)
;
1xian贴MainActivity的代码
private DisplayMetrics displayMetrics; private int windowWidth; private int windowHeight; private WheelView wheel; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.activity_main); initWheelView(); } public void initWheelView(){ wheel = (WheelView) findViewById(R.id.my_wheel); wheel.setOnTouchListener(this); wheel.setWindowSize(windowWidth,windowHeight); wheel.rushSmallXYAndBigXY(30, windowHeight - wheel.bitmapBig.getHeight() - 30); } public void getWindowMeasure(){ displayMetrics=new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); windowWidth = displayMetrics.widthPixels; windowHeight = displayMetrics.heightPixels; }
@Override public boolean onTouch(View v, MotionEvent event) { switch (v.getId()) { case R.id.my_wheel: switch (event.getAction()) { case MotionEvent.ACTION_DOWN: wheel.removeBitmapSmall(event); break; case MotionEvent.ACTION_MOVE: wheel.removeBitmapSmall(event); break; case MotionEvent.ACTION_UP: wheel.rushSmallXYAndBigXY(30, windowHeight - wheel.bitmapBig.getHeight() - 30); wheel.invalidate(); break; } } return true; }
2zai贴WheelView的代码;
public class WheelView extends View { /*大圆图片*/ Bitmap bitmapBig; /*小圆图片*/ Bitmap bitmapSmall; /*大圆左上x坐标*/ int bitmapBigLeftTopX ; /*大圆左上y坐标*/ int bitmapBigLeftTopY ; /*小圆左上x坐标*/ int bitmapSmallLeftTopX; /*小圆左上y坐标*/ int bitmapSmallLeftTopY; /*圆心x*/ double centerX; /*圆心y*/ double centerY; /*点击的x坐标*/ float pointX; /*点击的y坐标*/ float pointY; /*屏幕的宽度*/ int windowWidth; /*屏幕的高度*/ int windowHeight; public WheelView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); /*初始化图片*/ bitmapBig=BitmapFactory.decodeResource(getResources(), R.drawable.wheel_big); bitmapSmall=BitmapFactory.decodeResource(getResources(),R.drawable.wheel_small); /*初始化所有坐标*/ rushSmallXYAndBigXY(0,0); } public void rushSmallXYAndBigXY(int bitmapBigLeftTopX, int bitmapBigLeftTopY){ this.bitmapBigLeftTopX=bitmapBigLeftTopX; this.bitmapBigLeftTopY=bitmapBigLeftTopY; centerX=bitmapBigLeftTopX+bitmapBig.getWidth()/2; centerY=bitmapBigLeftTopY+bitmapBig.getHeight()/2; bitmapSmallLeftTopX=(int)centerX-bitmapSmall.getWidth()/2; bitmapSmallLeftTopY=(int)centerY-bitmapSmall.getHeight()/2; invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(bitmapBig,bitmapBigLeftTopX,bitmapBigLeftTopY,null); canvas.drawBitmap(bitmapSmall,bitmapSmallLeftTopX,bitmapSmallLeftTopY,null); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); /*宽度设置为屏幕一半*/ int width=getMeasuredWidth()/2; setMeasuredDimension(width,heightMeasureSpec); } /*根据触摸屏幕切换小圆的位置*/ public void removeBitmapSmall(MotionEvent event){ /*获的触屏事件的,X,Y坐标*/ pointX=event.getX(); pointY=event.getY(); /*超出屏幕一半时点击是没有效果的*/ if(pointX<windowWidth/2){ /*大圆半径*/ int r=bitmapBig.getWidth()/2; /*触摸的点到圆心的距离*/ double R=Math.sqrt(Math.pow(pointX-centerX,2)+Math.pow(pointY-centerY,2)); /*如果触摸的点再圆内,则将小圆坐标设置为触摸的点*/ if (R<r){ bitmapSmallLeftTopX=((int)(pointX-bitmapSmall.getWidth()/2)); bitmapSmallLeftTopY=((int)(pointY-bitmapSmall.getHeight()/2)); } else{ /*根据比例计算出,刚好不超出大圆的小圆坐标*/ double x1=(Math.abs(pointX-centerX)*r)/R; double y1=(Math.abs(pointY-centerY)*r)/R; double x2=0; double y2=0; if (pointX>=centerX&&pointY>=centerY){ x2=x1+centerX; y2=y1+centerY; } if (pointX<=centerX&&pointY>=centerY){ x2=centerX-x1; y2=centerY+y1; } if (pointX<=centerX&&pointY<=centerY){ x2=centerX-x1; y2=centerY-y1; } if (pointX>=centerX&&pointY<=centerY){ x2=centerX+x1; y2=centerY-y1; } bitmapSmallLeftTopX=((int)x2-bitmapSmall.getWidth()/2); bitmapSmallLeftTopY=((int)y2-bitmapSmall.getHeight()/2); } invalidate(); }else {
/*如果超过屏幕宽度一半,则回到,一开始设置的屏幕左下角30dp处*/ rushSmallXYAndBigXY(30, windowHeight - bitmapBig.getHeight() - 30); } } public void setWindowSize(int windowWidth,int windowHeight){
/*获得屏幕的宽度和高度,在mainActivity中调用*/ this.windowWidth=windowWidth; this.windowHeight=windowHeight; } }
2zuihou贴xml布局文件代码
<com.example.wo.tankgame.LeverMode.WheelView android:id="@+id/my_wheel" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true"/>
相关文章推荐
- 安卓自定义 View 基础:角度弧度
- 安卓自定义view基础02-角度与弧度
- 【安卓-自定义布局】安卓App开发思路 一步一个脚印(六)实现ViewPager最基本的
- 安卓自定义View----且看如何巧妙地实现一个类似于电视遥控板的环形按钮效果(上)
- 安卓自定义View基础(二)角度和弧度
- 【安卓-自定义布局】安卓App开发思路 一步一个脚印(十)实现内嵌在app中的webview 腾讯开源X5 高效安全
- 安卓自定义View基础-角度与弧度
- 【安卓-自定义布局】安卓App开发思路 一步一个脚印(七)实现ViewPager无限循环与自动播放
- 安卓自定义View基础-角度与弧度
- 安卓自定义View基础-角度与弧度
- 自定义View练习(一个自定义的折线图)
- android自定义View学习(一)----创建一个视图类
- 实现自定义ViewGroup的一个精简例子
- 你也可以自己写一个可爱 & 小资风格的Android加载等待自定义View
- iPhone开发学习笔记005——使用XIB自定义一个UIView,然后将这个view添加到controller的view
- 安卓自定义View进阶-事件分发机制原理
- 一个简单的自定义View,仿圆形进度条
- 安卓开发中自定义View之onMeasure(),onLayout(),onDraw()讲解(二)
- 安卓学习-界面-View的自定义
- 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)