android_绘图

android_绘图
main.xml
<com.zwz.MyView  
		android:id="@+id/myView"
    	android:layout_width="fill_parent" 
    	android:layout_height="fill_parent" 
    />


public class MyView extends View{
	public MyView(Context context, AttributeSet attrs) {//构造器
		super(context, attrs);
	}
	protected void onDraw(Canvas canvas) {//重写的绘制方法
		super.onDraw(canvas);
		canvas.drawColor(Color.BLACK);//绘制黑色背景
		Paint paint = new Paint();//创建画笔
		paint.setColor(Color.WHITE);//设置画笔颜色为红色
		
		canvas.drawRect(10, 10, 110, 110, paint);//绘制矩形
		canvas.drawText("这是字符串", 10, 130, paint);//字符串,以字符串下面为基准
		
		RectF rf1 = new RectF(10, 130, 110, 230);//定义一个矩形
		canvas.drawArc(rf1, 0, 45, true, paint);//画弧,顺时针
		
		canvas.drawLine(150, 10, 250, 110, paint);//画线
		RectF rf2 = new RectF(150, 130, 250, 230);//定义一个矩形
		canvas.drawOval(rf2, paint);//画圆 
	}
}


------------------------------------------
图片绘制
android_绘图

public class MyView extends View{//继承自View
	Bitmap myBitmap;//图片的引用
	Paint paint;//画笔的引用
	public MyView(Context context, AttributeSet attrs) {//构造器
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.initBitmap(); 
	}
	public void initBitmap(){ 
		paint = new Paint();//创建一个画笔
		myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img);//获得图片资源
	}
	@Override
	protected void onDraw(Canvas canvas) {//重写的绘制方法
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		paint.setAntiAlias(true);//打开抗锯齿
		paint.setColor(Color.WHITE);//设置画笔的颜色
		paint.setTextSize(15);
		canvas.drawBitmap(myBitmap, 10, 10, paint);//绘制图片
		canvas.drawText("图片的宽度: "+myBitmap.getWidth(), 20, 220, paint);//绘制字符串,图片的宽度
		canvas.drawText("图片的高度: "+myBitmap.getHeight(), 150, 220, paint);//绘制字符串,图片的高度
	}
}


-----------------------------------------------------------------
SurfaceView--显存的一个映射,写入到Surface 的内容,可以被直接复制到显存从而显示出来
继承SurfaceView并实现SurfaceHolder.Callback接口 ----> SurfaceView.getHolder()获得SurfaceHolder对象 ---->SurfaceHolder.addCallback(callback)添加回调函数---->SurfaceHolder.lockCanvas()获得Canvas对象并锁定画布----> Canvas绘画 ---->SurfaceHolder.unlockCanvasAndPost(Canvas canvas)结束锁定画图,并提交改变,将图形显示

SurfaceHolder,可以把它当成surface的控制器,用来操纵surface

(1)、abstract void addCallback(SurfaceHolder.Callback callback);
// 给SurfaceView当前的持有者一个回调对象。
(2)、abstract Canvas lockCanvas();
// 锁定画布,一般在锁定后就可以通过其返回的画布对象Canvas,在其上面画图等操作了。
(3)、abstract Canvas lockCanvas(Rect dirty);
// 锁定画布的某个区域进行画图等..因为画完图后,会调用下面的unlockCanvasAndPost来改变显示内容。
// 相对部分内存要求比较高的游戏来说,可以不用重画dirty外的其它区域的像素,可以提高速度。
(4)、abstract void unlockCanvasAndPost(Canvas canvas);
// 结束锁定画图,并提交改变

  public class ViewTest extends Activity {
       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
           setContentView(new MyView(this));
       }
      //视图内部类
 21     class MyView extends SurfaceView implements SurfaceHolder.Callback
 22     {
 23         private SurfaceHolder holder;
 24         private MyThread myThread; 
 25         public MyView(Context context) {
 26             super(context);
 27             // TODO Auto-generated constructor stub
 28             holder = this.getHolder();
 29             holder.addCallback(this);
 30             myThread = new MyThread(holder);//创建一个绘图线程
 31         }
 32 
 33         @Override
 34         public void surfaceChanged(SurfaceHolder holder, int format, int width,
 35                 int height) {
 36             // TODO Auto-generated method stub
 37             
 38         }
 39 
 40         @Override
 41         public void surfaceCreated(SurfaceHolder holder) {
 42             // TODO Auto-generated method stub
 43             myThread.isRun = true;
 44             myThread.start();
 45         }
 46 
 47         @Override
 48         public void surfaceDestroyed(SurfaceHolder holder) {
 49             // TODO Auto-generated method stub
 50             myThread.isRun = false;
 51         }
 52         
 53     }
 54     //线程内部类
 55     class MyThread extends Thread
 56     {
 57         private SurfaceHolder holder;
 58         public boolean isRun ;
 59         public  MyThread(SurfaceHolder holder)
 60         {
 61             this.holder =holder; 
 62             isRun = true;
 63         }
 64         @Override
 65         public void run()
 66         {
 67             int count = 0;
 68             while(isRun)
 69             {
 70                 Canvas c = null;
 71                 try
 72                 {
 73                     synchronized (holder)
 74                     {
 75                         c = holder.lockCanvas();//锁定画布,一般在锁定后就可以通过其返回的画布对象Canvas,在其上面画图等操作了。
 76                         c.drawColor(Color.BLACK);//设置画布背景颜色
 77                         Paint p = new Paint(); //创建画笔
 78                         p.setColor(Color.WHITE);
 79                         Rect r = new Rect(100, 50, 300, 250);
 80                         c.drawRect(r, p);
 81                         c.drawText("这是第"+(count++)+"秒", 100, 310, p);
 82                         Thread.sleep(1000);//睡眠时间为1秒
 83                     }
 84                 }
 85                 catch (Exception e) {
 86                     // TODO: handle exception
 87                     e.printStackTrace();
 88                 }
 89                 finally
 90                 {
 91                     if(c!= null)
 92                     {
 93                         holder.unlockCanvasAndPost(c);//结束锁定画图,并提交改变。
 94 
 95                     }
 96                 }
 97             }
 98         }
 99     }
100 }




你可能感兴趣的:(android)