Android SurfaceView 学习笔记
来源:互联网 发布:h3c路由器关闭端口 编辑:程序博客网 时间:2024/04/26 20:05
转帖:http://www.androidegg.com/portal.php?mod=view&aid=1258
这是由于 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的具体位置的方法.
下面是一个小例子 :
main.xml:
< ?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"
>
< EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:focusable = "true"
/>
< com.en.demo.SVD
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
< /LinearLayout>
SurfaceView 子类 SVD:
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SVD extends SurfaceView implements SurfaceHolder.Callback, Runnable{
SurfaceHolder holder;
public SVD(Context context, AttributeSet attrs) {
super(context, attrs);
System.out.println("init()");
holder = getHolder();
holder.addCallback(this);
}
public void draw(){
System.out.println(" draw()");
Canvas canvas = holder.lockCanvas();
draw(canvas);
holder.unlockCanvasAndPost(canvas);
}
@Override
public void draw(Canvas canvas) {
canvas.drawColor(0xff7f7f7f + (run < < 3));
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.setMeasuredDimension(130, 100);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
System.out.println("width:"+width+" height:"+height);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
System.out.println(" create ");
new Thread(this).start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
this.holder = null;
}
int run = 10;
@Override
public void run() {
// TODO Auto-generated method stub
try{
while( run-- > 0){
draw();
Thread.sleep(1000);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Activity 中 的onCreate(Bundle)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
转帖:http://www.androidegg.com/portal.php?mod=view&aid=1258
- Android SurfaceView 学习笔记
- Android SurfaceView学习笔记
- android学习笔记 SurfaceView模板
- Android SurfaceView 学习笔记(二)
- Android SurfaceView 学习笔记(二)
- Android Camera之SurfaceView学习笔记
- Android SurfaceView 笔记
- android笔记3(surfaceview)
- Android SurfaceView使用 笔记
- Android 笔记 surfaceView textureView
- android群英传笔记 surfaceView
- android 之surfaceView 学习
- Android SurfaceView学习示例
- Android之SurfaceView学习
- Android之SurfaceView学习
- Android之SurfaceView学习
- Android之SurfaceView学习
- Android中SurfaceView学习
- 参考文献的自动生成
- 采用有利于版本管理的代码格式
- 逻辑数据库的用法
- 文章5:“We will never forget you [online]”: An Empirical Investigation of Post-mortem MySpace Comments
- 避免使用虚函数作为库的接口
- Android SurfaceView 学习笔记
- 慎用匿名 namespace
- Android手机、E-mail、QQ验证
- PostgreSQL+PostGIS的使用
- 用JRockit来检测内存泄露
- Linux下PHP添加curl模块
- 转:java.lang.NoClassDefFoundError netBeans中或sun.rmi.activation.execPolicy警告eclipse中
- PHP上传RAR压缩包并解压类
- FILE实验操作之MP3信息提取