• Android 画图类View与SurfaceView之学习


    http://blog.csdn.net/andyhuabing/article/details/12653183

    Canvas 的使用:

    lockCanvas  锁定画布,绘图之前必须锁定画布才能得到当前画布对象

    unlockCanvasAndPost 开始绘制时锁定画布,绘制完成之后解锁画布

    TestSurfaceView.java 代码:

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    public class TestSurfaceView extends SurfaceView implements  SurfaceHolder.Callback {
    	//控制循环
    	private boolean mbLoop = false;
    	
    	//定义SurfaceHolder对象
    	private SurfaceHolder mSurfaceHolder = null;
    	private int miCount = 0;
    	public int x = 50, y = 50;
    	private int mWidth = 1280, mHeight = 720;
    	private Bitmap mBitmap = null;
    
    	public TestSurfaceView(Context context) {
    		super(context);
    		
    		//实例化SurfaceHolder
    		mSurfaceHolder = this.getHolder();
    		
    		//添加回调
    		mSurfaceHolder.addCallback(this);
    		this.setFocusable(true);
    	}
    	
    	  public void setDisplayWH(int w, int h) {  
    	        mWidth = w;  
    	        mHeight = h;  
    	    }
    	  
    	   public void setBitmap(Bitmap bitmap) {  
    	        this.mBitmap = bitmap;  
    	    }  
    
    	@Override
    	public void surfaceCreated(SurfaceHolder holder) {
    		mbLoop = true;
    		
    		Thread th = new Thread(new Runnable(){
    
    			@Override
    			public void run() {
    				while(mbLoop){
    					try {
    						Thread.sleep(200);
    					} catch (InterruptedException e) {
    						e.printStackTrace();
    					}
    					synchronized(mSurfaceHolder){
    						drawBitmap();
    						DrawData();
    					}
    				}
    
    			}
    			
    		});
    		th.start();
    		
    	}
    
    	@Override
    	public void surfaceChanged(SurfaceHolder holder, int format, int width,
    			int height) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void surfaceDestroyed(SurfaceHolder holder) {
    		// TODO Auto-generated method stub
    		
    	}  
    	
        private void drawBitmap() {  
            // 锁定画布,得到canvas  
            if (mSurfaceHolder == null || this.mBitmap == null)  
                return;  
      
            Canvas canvas = mSurfaceHolder.lockCanvas();  
            if (canvas == null) {  
                return;  
            }  
      
            // 绘图  
            Paint paint = new Paint();  
            paint.setAntiAlias(true);  
            paint.setColor(Color.BLUE);  
      
            canvas.drawBitmap(this.mBitmap, 200, 0, paint);  
      
            // 绘制后解锁,绘制后必须解锁才能显示  
            mSurfaceHolder.unlockCanvasAndPost(canvas);  
        } 
        
        // 绘图方法  
        private void DrawData() {  
            if (mSurfaceHolder == null)  
                return;  
      
            // 锁定画布,得到canvas  
            Canvas canvas = mSurfaceHolder.lockCanvas();  
            if (canvas == null) {  
                return;  
            }  
      
            if (miCount < 100) {  
                miCount++;  
            } else {  
                miCount = 0;  
            }  
      
            // 绘图  
            Paint mPaint = new Paint();  
            mPaint.setAntiAlias(true);  
            mPaint.setColor(Color.BLACK);  
      
            // 绘制矩形--清屏作用  
            canvas.drawRect(0, 0, mWidth, mHeight, mPaint);  
              
            switch (miCount % 4) {  
            case 0:  
                mPaint.setColor(Color.BLUE);  
                break;  
            case 1:  
                mPaint.setColor(Color.GREEN);  
                break;  
            case 2:  
                mPaint.setColor(Color.RED);  
                break;  
            case 3:  
                mPaint.setColor(Color.YELLOW);  
                break;  
            default:  
                mPaint.setColor(Color.WHITE);  
                break;  
            }  
      
            // 绘制矩形--  
            canvas.drawCircle(x, y, 50, mPaint);  
              
            // 绘制后解锁,绘制后必须解锁才能显示  
            mSurfaceHolder.unlockCanvasAndPost(canvas);  
        }  
    	
    } 
    

     MainActivity代码:

    import android.app.Activity;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.Menu;
    
    
    public class MainActivity extends Activity {  
    	    static final String TAG = "MainActivity";  
    	    private TestSurfaceView mTestSurfaceView = null;  
    	    private int mWidth = 0, mHeight = 0;  
    	  
    	    @Override  
    	    public void onCreate(Bundle savedInstanceState) {  
    	        super.onCreate(savedInstanceState);  
    	  
    	        initView();  
    	  
    	        startTestSurfaceView();  
    	          
    	        // setContentView(R.layout.main);  
    	    }  
    	  
    	    void initView() {  
    	        // 使用自定义的View  
    	        mTestSurfaceView = new TestSurfaceView(this);  
    	        setContentView(mTestSurfaceView);         
    	        //获取手机分辨率
    	        DisplayMetrics dm = new DisplayMetrics();  
    	        getWindowManager().getDefaultDisplay().getMetrics(dm);  
    	        mWidth = dm.widthPixels;  
    	        mHeight = dm.heightPixels;  
    	        Log.i(TAG, "Display Metrics " + mWidth + " mHeight:" + mHeight);  
    	    }  
    	  
    	    @Override  
    	    public boolean onCreateOptionsMenu(Menu menu) {  
    	        getMenuInflater().inflate(R.menu.main, menu);  
    	        return true;  
    	    }  
    	  
    	    public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {  
    	        switch (keyCode) {  
    	        case KeyEvent.KEYCODE_DPAD_DOWN:  
    	            if (mTestSurfaceView.y >= mHeight)  
    	                mTestSurfaceView.y = 0;  
    	            mTestSurfaceView.y += 10;  
    	            break;  
    	        case KeyEvent.KEYCODE_DPAD_UP:  
    	            if (mTestSurfaceView.y <= 0)  
    	                mTestSurfaceView.y = mHeight;  
    	            mTestSurfaceView.y -= 10;  
    	            break;  
    	        case KeyEvent.KEYCODE_DPAD_LEFT:  
    	            if (mTestSurfaceView.x <= 0)  
    	                mTestSurfaceView.x = mWidth;  
    	            mTestSurfaceView.x -= 10;  
    	            break;  
    	        case KeyEvent.KEYCODE_DPAD_RIGHT:  
    	            if (mTestSurfaceView.x >= mWidth)  
    	                mTestSurfaceView.x = 0;  
    	            mTestSurfaceView.x += 10;  
    	            break;  
    	        case KeyEvent.KEYCODE_BACK:  
    	            this.finish();  
    	            break;  
    	        }  
    	        return false;  
    	    };  
    	  
    	    void startTestSurfaceView() {  
    	        mTestSurfaceView.setDisplayWH(mWidth, mHeight);  
    	        mTestSurfaceView.setBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
    	    }  
    
    }
    
  • 相关阅读:
    token的时限多长才合适?
    WebFTP安装说明
    维度表和事实表的区别
    互联网产品mysql数据库设计总结
    网络的介数中心性(betweenness)及计算方法
    python中的编码与解码
    增强学习Reinforcement Learning经典算法梳理3:TD方法
    Mybatis 参考
    防御CSRF、XSS和SQL注入攻击
    转:PriorityQueue
  • 原文地址:https://www.cnblogs.com/leihupqrst/p/3730429.html
Copyright © 2020-2023  润新知