现在的位置: 首页 > 综合 > 正文

简单实现自定义SurfaceView

2013年10月21日 ⁄ 综合 ⁄ 共 4377字 ⁄ 字号 评论关闭

做自定义view时遇到的问题还有如何解决的:

http://blog.csdn.net/suren__123/article/details/8065992

由于要实现联想存储器用于识别数字,所以想到做一个自定义的SurfaceView类用于输入输出

效果如下图所示,可以利用手写输入图像并转换为二位数组

也可以把二维数组的图像显示出来...



程序源代码如下:

public class NumView extends SurfaceView implements Runnable,Callback, android.view.SurfaceHolder.Callback{  
    
	private static final String TAG="NumView";  
    
	SurfaceHolder holder = null;  
    
	
	//SurfaceView的几个属性
	int left,right,top,bottom = 0; 
	int screenWidth,screenHeight = 0;
	int height,width = 0;
	int dHeight,dWidth = 0;
	
	int row = 6;
	int col = 5;
	
	
	//是否允许用户输入
	boolean allowIn = true;
	double flag = 0;
	
	//当前的二维数组
	public double[][] arry = new double[row][col];  
    
	
	//重要的构造方法
    public NumView(Context context,AttributeSet attrs) {  
        super(context,attrs);  
        // TODO Auto-generated constructor stub  
        holder=this.getHolder();  
        holder.addCallback(this);  
        this.setFocusable(true);
        Log.v("NumViewIn","1");
    }
    
    public NumView(Context context) {  
        super(context);  
        // TODO Auto-generated constructor stub  
        holder=this.getHolder();  
        holder.addCallback(this);  
        this.setFocusable(true);  
        Log.v("NumViewIn","2");
    }
        
    public void surfaceChanged(SurfaceHolder holder, int format, int width,  
            int height) {  
        // TODO Auto-generated method stub  
    	
    	Log.v("SurfaceChanged","1");
          
    } 
    
    //可以理解为初始化成功时调用
    public void surfaceCreated(SurfaceHolder holder) {  
        // TODO Auto-generated method stub  
    	Log.v("SurfaceCreat","1");
    	
    	screenWidth = this.getWidth();  
        screenHeight = this.getHeight();  
        
        left = this.getLeft();
        right = this.getRight();
        top = this.getTop();
        bottom = this.getBottom();
        
        height = this.getHeight();
        width = this.getWidth();
        
        dHeight = height/row; 
    	dWidth = width/col;
    	
    	//显示数组
    	drawArry();   	
    	    	 
    }
    
    
    public void surfaceDestroyed(SurfaceHolder holder) {  
        // TODO Auto-generated method stub 
    	Log.v("SurfaceDestroyed","1");
          
    }
    
    //初始化数组大小
    public void setRowCol(int row,int col){
    	
    	this.row = row;
    	this.col = col;
    	
    	this.arry = new double[row][col];
    	
    	for(int i = 0 ; i < row ; i++){
    		for(int j = 0 ; j < col ; j++){
    			
    			arry[i][j] = -1;
    			
    		}
    	}
    	
    }
    
    //设置是否允许用户输入
    public void setAllowIn(boolean allowIn){
    	
    	this.allowIn = allowIn;
    	
    }
    
    //清屏
    public void clear(){
    	    	
    	for(int i = 0 ; i < row ; i++){
    		for(int j = 0 ; j < col ; j++){
    			
    			arry[i][j] = -1;
    			
    		}
    	}
    	
    	drawArry();
    	
    }
    
    
    
    public double[][] getArry(){
    	
    	return arry;    	
    	
    }
    
    public void setArry(double[][] arry1,int row1,int col1){
    	
    	arry = arry1;
    	row = row1;
    	col = col1;

    }
    
    public void drawArry(){
    	
    	if(holder == null){
    		return;
    	} 
    	    	    	
    	//获得canvas对象
    	Canvas canvas = holder.lockCanvas();
    	
    	if(canvas == null){
    		
    		Log.v("draw","Canvas == null");
    		return;
    	}
    	
    	//清空屏幕
    	canvas.drawColor(Color.WHITE);
    	
    	Paint paint = new Paint();
    	paint.setColor(Color.BLACK); 
    	paint.setStyle(Paint.Style.FILL);
    	
    	//canvas.drawRect(10, 10, 20, 20, paint);
    	
    	//画出图像
    	for(int i = 0;i < row; i++){
    		for(int j = 0;j < col; j++){
    			    			
    			if(arry[i][j] == 1){
    				canvas.drawRect(j*dWidth, i*dHeight, (j+1)*dWidth, (i+1)*dHeight, paint);
    			}
    		}
    		
    	}
    	  
    	//画出网格
    	paint.setColor(Color.GRAY);
    	//画行
    	for(int i = 0;i <= row + 1; i++){   		
    		canvas.drawLine(0, i*dHeight, width, i*dHeight, paint);    		
    	}
    	
    	//画列
        for(int i = 0;i <= col + 1; i++){    		
    		canvas.drawLine(i*dWidth, 0, i*dWidth, height, paint);    		
    	}
    	
    	//释放canvas对象
    	holder.unlockCanvasAndPost(canvas);
    	
    }
    
    public void drawArry(double[][] arry){
    	
    	if(holder == null){
    		return;
    	}
    	
    	this.arry = arry;
    	
    	Log.v("draw",String.valueOf(this.arry));
    	
    	
    	//获得canvas对象
    	Canvas canvas = holder.lockCanvas();
    	
    	    	
    	//清空屏幕
    	canvas.drawColor(Color.WHITE);
    	
    	
    	
    	Paint paint = new Paint();
    	paint.setColor(Color.BLACK); 
    	paint.setStyle(Paint.Style.FILL);
    	
    	//canvas.drawRect(10, 10, 20, 20, paint);
    	
    	//画出图像
    	for(int i = 0;i < row; i++){
    		for(int j = 0;j < col; j++){
    			    			
    			if(this.arry[i][j] == 1){
    				canvas.drawRect(j*dWidth, i*dHeight, (j+1)*dWidth, (i+1)*dHeight, paint);
    			}
    		}
    		
    	}
    	  
    	//画出网格
    	paint.setColor(Color.GRAY);
    	//画行
    	for(int i = 0;i <= row + 1; i++){   		
    		canvas.drawLine(0, i*dHeight, width, i*dHeight, paint);    		
    	}
    	
    	//画列
        for(int i = 0;i <= col + 1; i++){    		
    		canvas.drawLine(i*dWidth, 0, i*dWidth, height, paint);    		
    	}
    	
    	//释放canvas对象
    	holder.unlockCanvasAndPost(canvas);
    	
    	
    	
    }
        
    
    //笔触事件 
    @Override
    public boolean onTouchEvent(MotionEvent event) 
    { 
        
    	
        switch (event.getAction()) {  

        case MotionEvent.ACTION_DOWN:  
        	        	
        	if(allowIn){
        		
        		int col = (int)event.getX()/dWidth;
        		int row = (int)event.getY()/dHeight;
        		//Log.v("ACTION_DOWN",String.valueOf(row)+"-"+String.valueOf(col));
                
        		arry[row][col] = -arry[row][col];
        		flag = arry[row][col];
        		
        		drawArry(arry);        		
        	}
        	
        	break;  
        case MotionEvent.ACTION_MOVE:
        	       	
        	//Log.v("ACTION_MOVE",String.valueOf((int)event.getX())+"-"+String.valueOf((int)event.getY()));
            //要加入过界处理防止数组过界
        	
            if(allowIn){
        		
        		int c = (int)event.getX()/dWidth;
        		int r = (int)event.getY()/dHeight;
        		
        		if(c >= col){ c = col-1; }
        		if(c <= 0){ c = 0; }
        		
        		if(r >= row){ r = row-1; }
        		if(r <= 0){ r = 0; }
        		
           		arry[r][c] = flag;
        		
        		drawArry(arry);
        		        		
        	}
        		
        	break;  
        case MotionEvent.ACTION_UP:  
        	       	
        	//Log.v("ACTION_UP",String.valueOf((int)event.getX())+"-"+String.valueOf((int)event.getY()));
        
        case MotionEvent.ACTION_POINTER_DOWN:  
        	       	
        	//Log.v("ACTION_POINTER_DOWN",String.valueOf((int)event.getX())+"-"+String.valueOf((int)event.getY()));        	
    		break;
    		       
        default:  
        	break;  
        }  
        return true;
		
    }

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
	 
}

抱歉!评论已关闭.