android --小球跟随手指滑动

转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/6828044  作者:张燕广

实现的功能:手指在屏幕上滑动,变幻颜色的小球始终跟随手指移动。

实现的思路:1)自定义View,在onDraw中画圆作为小球;2)重写自定义View的onTouchEvent方法,记录触屏坐标,用新的坐标重新绘制小球。

关键技术点:自定义View应用、触摸事件处理、canvas绘图、Paint应用

第一步:新建一个工程,命名为BallViewDemo,Activity命名为BallActivity。

第二步:编写自定义View类BallView,本例中将BallView作为BallActivity的内部类,BallActivity代码如下:

  1. package com.zyg.customview.ball;  
  2.   
  3. import java.util.Random;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.graphics.Canvas;  
  8. import android.graphics.Color;  
  9. import android.graphics.Paint;  
  10. import android.os.Bundle;  
  11. import android.view.Display;  
  12. import android.view.MotionEvent;  
  13. import android.view.View;  
  14. import android.view.Window;  
  15. import android.view.WindowManager;  
  16.   
  17. public class BallActivity extends Activity {  
  18.     private int screenW;        //屏幕宽度   
  19.     private int screenH;        //屏幕高度   
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         Display dis = this.getWindowManager().getDefaultDisplay();  
  24.         // 设置全屏   
  25.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  26.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  27.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  28.         // 获取屏幕宽度   
  29.         screenW = dis.getWidth();  
  30.         // 获取屏幕高度   
  31.         screenH = dis.getHeight();  
  32.           
  33.         setContentView(new BallView(this));  
  34.     }  
  35.       
  36.     //自定义绘图类   
  37.     class BallView extends View{  
  38.         private Paint paint;        //定义画笔   
  39.         private float cx = 50;      //圆点默认X坐标   
  40.         private float cy = 50;      //圆点默认Y坐标   
  41.         private int radius = 20;  
  42.         //定义颜色数组   
  43.         private int colorArray[] = {Color.BLACK,Color.BLACK,Color.GREEN,Color.YELLOW, Color.RED};  
  44.         private int paintColor = colorArray[0]; //定义画笔默认颜色   
  45.           
  46.         public BallView(Context context) {  
  47.             super(context);  
  48.             //初始化画笔   
  49.             initPaint();  
  50.         }  
  51.         private void initPaint(){  
  52.             paint = new Paint();  
  53.             //设置消除锯齿   
  54.             paint.setAntiAlias(true);  
  55.             //设置画笔颜色   
  56.             paint.setColor(paintColor);  
  57.         }  
  58.           
  59.         //重写onDraw方法实现绘图操作   
  60.         @Override  
  61.         protected void onDraw(Canvas canvas) {  
  62.             super.onDraw(canvas);  
  63.             //将屏幕设置为白色   
  64.             canvas.drawColor(Color.WHITE);  
  65.             //修正圆点坐标   
  66.             revise();  
  67.             //随机设置画笔颜色   
  68.             setPaintRandomColor();  
  69.             //绘制小圆作为小球   
  70.             canvas.drawCircle(cx, cy, radius, paint);  
  71.         }  
  72.           
  73.         //为画笔设置随机颜色   
  74.         private void setPaintRandomColor(){  
  75.             Random rand = new Random();  
  76.             int randomIndex = rand.nextInt(colorArray.length);  
  77.             paint.setColor(colorArray[randomIndex]);  
  78.         }  
  79.           
  80.         //修正圆点坐标   
  81.         private void revise(){  
  82.             if(cx <= radius){  
  83.                 cx = radius;  
  84.             }else if(cx >= (screenW-radius)){  
  85.                 cx = screenW-radius;  
  86.             }  
  87.             if(cy <= radius){  
  88.                 cy = radius;  
  89.             }else if(cy >= (screenH-radius)){  
  90.                 cy = screenH-radius;  
  91.             }  
  92.         }  
  93.           
  94.         @Override  
  95.         public boolean onTouchEvent(MotionEvent event) {  
  96.             switch (event.getAction()) {  
  97.             case MotionEvent.ACTION_DOWN:  
  98.                 // 按下   
  99.                 cx = (int) event.getX();  
  100.                 cy = (int) event.getY();  
  101.                 // 通知重绘   
  102.                 postInvalidate();   //该方法会调用onDraw方法,重新绘图   
  103.                 break;  
  104.             case MotionEvent.ACTION_MOVE:  
  105.                 // 移动   
  106.                 cx = (int) event.getX();  
  107.                 cy = (int) event.getY();  
  108.                 // 通知重绘   
  109.                 postInvalidate();  
  110.                 break;  
  111.             case MotionEvent.ACTION_UP:  
  112.                 // 抬起   
  113.                 cx = (int) event.getX();  
  114.                 cy = (int) event.getY();  
  115.                 // 通知重绘   
  116.                 postInvalidate();  
  117.                 break;  
  118.             }  
  119.               
  120.             /* 
  121.              * 备注1:此处一定要将return super.onTouchEvent(event)修改为return true,原因是: 
  122.              * 1)父类的onTouchEvent(event)方法可能没有做任何处理,但是返回了false。 
  123.              * 2)一旦返回false,在该方法中再也不会收到MotionEvent.ACTION_MOVE及MotionEvent.ACTION_UP事件。 
  124.              */  
  125.             //return super.onTouchEvent(event);   
  126.             return true;    
  127.         }  
  128.     }  
  129. }  
package com.zyg.customview.ball;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class BallActivity extends Activity {
	private int screenW;		//屏幕宽度
	private int screenH;		//屏幕高度
    @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Display dis = this.getWindowManager().getDefaultDisplay();
		// 设置全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// 获取屏幕宽度
		screenW = dis.getWidth();
		// 获取屏幕高度
		screenH = dis.getHeight();
		
		setContentView(new BallView(this));
	}
    
    //自定义绘图类
    class BallView extends View{
    	private Paint paint; 		//定义画笔
    	private float cx = 50;		//圆点默认X坐标
    	private float cy = 50;		//圆点默认Y坐标
    	private int radius = 20;
    	//定义颜色数组
    	private int colorArray[] = {Color.BLACK,Color.BLACK,Color.GREEN,Color.YELLOW, Color.RED};
    	private int paintColor = colorArray[0]; //定义画笔默认颜色
    	
    	public BallView(Context context) {
    		super(context);
    		//初始化画笔
    		initPaint();
    	}
    	private void initPaint(){
    		paint = new Paint();
    		//设置消除锯齿
    		paint.setAntiAlias(true);
    		//设置画笔颜色
    		paint.setColor(paintColor);
    	}
    	
    	//重写onDraw方法实现绘图操作
    	@Override
    	protected void onDraw(Canvas canvas) {
    		super.onDraw(canvas);
    		//将屏幕设置为白色
    		canvas.drawColor(Color.WHITE);
    		//修正圆点坐标
    		revise();
    		//随机设置画笔颜色
    		setPaintRandomColor();
    		//绘制小圆作为小球
    		canvas.drawCircle(cx, cy, radius, paint);
    	}
    	
    	//为画笔设置随机颜色
    	private void setPaintRandomColor(){
    		Random rand = new Random();
    		int randomIndex = rand.nextInt(colorArray.length);
    		paint.setColor(colorArray[randomIndex]);
    	}
    	
    	//修正圆点坐标
    	private void revise(){
    		if(cx <= radius){
    			cx = radius;
    		}else if(cx >= (screenW-radius)){
    			cx = screenW-radius;
    		}
    		if(cy <= radius){
    			cy = radius;
    		}else if(cy >= (screenH-radius)){
    			cy = screenH-radius;
    		}
    	}
    	
    	@Override
		public boolean onTouchEvent(MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				// 按下
				cx = (int) event.getX();
				cy = (int) event.getY();
				// 通知重绘
				postInvalidate();	//该方法会调用onDraw方法,重新绘图
				break;
			case MotionEvent.ACTION_MOVE:
				// 移动
				cx = (int) event.getX();
				cy = (int) event.getY();
				// 通知重绘
				postInvalidate();
				break;
			case MotionEvent.ACTION_UP:
				// 抬起
				cx = (int) event.getX();
				cy = (int) event.getY();
				// 通知重绘
				postInvalidate();
				break;
			}
			
			/*
			 * 备注1:此处一定要将return super.onTouchEvent(event)修改为return true,原因是:
			 * 1)父类的onTouchEvent(event)方法可能没有做任何处理,但是返回了false。
			 * 2)一旦返回false,在该方法中再也不会收到MotionEvent.ACTION_MOVE及MotionEvent.ACTION_UP事件。
			 */
			//return super.onTouchEvent(event);
			return true;  
		}
    }
}

main.xml与AndroidManifest.xml未作修改,不再贴出~

备注:代码中的备注1介绍了onTouchEvent方法在实际开发中的一个Bug的解决方法,详见代码。

第三步:运行程序,效果如下:

下一篇将用自定义SurfaceView代替自定义View实现该实例功能,并总结一下自定义View与自定义SurfaceView区别与应用场景。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值