Android SurfaceView 学习笔记(二)

SurfaceView 是一个继承了View但是由于一般的View有这很大区别的类.
  这是由于 SurfaceView 的绘制方法和原来的View不同.在 View 中 系统不允许主线程外的线程控制 UI .但是 SurfaceView 却可以 .下面是我总结的几个要点:
  1. 首先需要实现 View 的构造方法.( 如果 需要在XML 文件中布局需要实现public S(Context context, AttributeSet attrs) 这个构造方法 )
  2. 由于需要对SurfaceView 进行监控所以需要实现 SurfaceHolder.Callback 这个接口( 可以用内部类或者方法实现.) 这个接口需要实现三个方法:
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {} //大小改变的时候被调用到.
  public void surfaceCreated(SurfaceHolder holder) {} // 创建的时候被调用到
  public void surfaceDestroyed(SurfaceHolder holder) {} //销毁的时候被调用
  3.在SurfaceView 中屏幕接触处理和 布局处理和View一样.
  4. 使用绘制的时候和 View 完全不一样.他是使用 SufaceHodler 的方法
  public canvas holder.lockCanvas();
  public void unlockCanvasAndPost(canvas);
  第一个方法可以调用出一个Canvas 画布.在上面绘制所需的画面.然后调用第二个方法.这样就可以在屏幕上面绘制出来的.
  View中的 invalidate()方法需要在主线程中调用(postInvalidate()不同).但是 SurfaceView不需要.SurfaceView绘制效率比View高.
  5.SurfaceView中如果需要请求重新布局同样使用 requestLayout();
  6. 和View一样重要的一些方法:
  onMeasure(int ,int); 是使用 View 前需要调用的方法. 通知View进行自身尺寸测量.如果自己重写的话测量完自身大小注意需要调用setMeasuredDimension(int, int);这个方法设置控件大小.

  onLayout(boolean,int,int,int,int); 这个方法使父控件具体分配给当前View的具体位置的方法.

下面上代码,XML代码

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView   
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="wrap_content"  
  10.     android:text="@string/hello"  
  11.     />  
  12. <com.demo.test.CustomView  
  13.     android:id="@+id/customView1"  
  14.     android:layout_width="wrap_content"  
  15.     android:layout_height="wrap_content">  
  16. </com.demo.test.CustomView>  
  17. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<com.demo.test.CustomView
    android:id="@+id/customView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</com.demo.test.CustomView>
</LinearLayout>
Activity 中 的onCreate(Bundle)

  1. public void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.         setContentView(R.layout.main);  
  4.     }  
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

SurfaceView 子类
  1. public class CustomView extends SurfaceView implements SurfaceHolder.Callback   
  2. {  
  3.     private SurfaceHolder holder;  
  4.     private MyThread thread;  
  5.     private boolean isrun;  
  6.     public  Bitmap bitmap;  
  7.     public  Matrix matrixnew Matrix(); ;  
  8.     public  Paint mPaint = new Paint();  
  9.     public  InputStream is;  
  10.       
  11.     public CustomView(Context context, AttributeSet attrs){  
  12.        super(context, attrs);  
  13.        holder = getHolder();  
  14.        holder.addCallback(this);  
  15.        isrun = false;  
  16.     }  
  17.     @Override  
  18.     public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {  
  19.        // TODO Auto-generated method stub  
  20.   
  21.     }  
  22.     @Override  
  23.     public void surfaceCreated(SurfaceHolder arg0) {  
  24.        // TODO Auto-generated method stub  
  25.         is = getResources().openRawResource(R.drawable.view);  
  26.         bitmap = BitmapFactory.decodeStream(is);  
  27.         isrun = true;  
  28.         thread = new MyThread();  
  29.         thread.start();  
  30.     }  
  31.     @Override  
  32.     public void surfaceDestroyed(SurfaceHolder arg0) {  
  33.        // TODO Auto-generated method stub  
  34.        isrun = false;  
  35.     }  
  36.     class MyThread extends Thread  
  37.     {  
  38.        public void run()  
  39.        {  
  40.            SurfaceHolder runholder = holder;  
  41.            if(isrun == true)  
  42.            {  
  43.               Canvas canvas = runholder.lockCanvas();  
  44.               canvas.drawBitmap(bitmap,matrix,mPaint);  
  45.               runholder.unlockCanvasAndPost(canvas);  
  46.            }  
  47.        }  
  48.     }  
  49. }  
public class CustomView extends SurfaceView implements SurfaceHolder.Callback 
{
    private SurfaceHolder holder;
    private MyThread thread;
    private boolean isrun;
    public  Bitmap bitmap;
    public  Matrix matrix= new Matrix(); ;
    public  Paint mPaint = new Paint();
    public  InputStream is;
    
    public CustomView(Context context, AttributeSet attrs){
       super(context, attrs);
       holder = getHolder();
       holder.addCallback(this);
       isrun = false;
    }
    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
       // TODO Auto-generated method stub

    }
    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
       // TODO Auto-generated method stub
    	is = getResources().openRawResource(R.drawable.view);
    	bitmap = BitmapFactory.decodeStream(is);
    	isrun = true;
    	thread = new MyThread();
    	thread.start();
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
       // TODO Auto-generated method stub
       isrun = false;
    }
    class MyThread extends Thread
    {
       public void run()
       {
           SurfaceHolder runholder = holder;
           if(isrun == true)
           {
              Canvas canvas = runholder.lockCanvas();
              canvas.drawBitmap(bitmap,matrix,mPaint);
              runholder.unlockCanvasAndPost(canvas);
           }
       }
    }
}



结果如图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值