一、基础知识:
SurfaceView继承自View,View负责在主线程中更新动画,而SurfaceView是在一个新线程中更新动画。
SurfaceView类的主要方法:
// 在SurfaceView创建时调用
pubilic abstract void
surfaceCreated(SurfaceHolder holder)
// 在SurfaceView改变时调用
pubilic abstract void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
// 在SurfaceView销毁时调用
pubilic abstract void surfaceDestroyed(SurfaceHolder holder)
// 绘制SurfaceView画面
protected void onDraw(Canvas canvas)
(参数canvas是该SurfaceView的画笔,每一次SurfaceView中画面改变都是调用了该方法)
二、实现效果:
首先有一副图片从屏幕的左下角开始向右上方运动,当图片上沿与手机屏幕上沿相撞时,图片的水平速度大小与方向均不变,竖直方向上速度大小不变,
方向相反;当下沿相撞后,同样效果,直到图片飞出屏幕。之后,屏幕渐渐地显示一幅图片。
三、编程实现:
1. 界面编辑(res\layout\main.xml):
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:<a title=
"android"
href=
"http://www.android-study.com/jichuzhishi/543.html"
>android</a>=
"http://schemas.<a title="
android
" href="
http://www.android-study.com/jichuzhishi/
543
.html
">android</a>.com/apk/res/<a title="
android
" href="
http://www.android-study.com/jichuzhishi/
543
.html
">android</a>"
<a title=
"android"
href=
"http://www.android-study.com/jichuzhishi/543.html"
>android</a>:orientation=
"vertical"
<a title=
"android"
href=
"http://www.android-study.com/jichuzhishi/543.html"
>android</a>:layout_width=
"fill_parent"
<a title=
"android"
href=
"http://www.android-study.com/jichuzhishi/543.html"
>android</a>:layout_height=
"fill_parent"
>
<TextView
<a title=
"android"
href=
"http://www.android-study.com/jichuzhishi/543.html"
>android</a>:layout_width=
"fill_parent"
<a title=
"android"
href=
"http://www.android-study.com/jichuzhishi/543.html"
>android</a>:layout_height=
"wrap_content"
<a title=
"android"
href=
"http://www.android-study.com/jichuzhishi/543.html"
>android</a>:text=
"@string/hello"
/>
</LinearLayout>
|
2. 代码编辑:
(\src\wyf\zcl\MyActivity.java)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package
wyf.zcl;
/*
* 该例子演示surfaceView中简单场景的绘制
* MyActivity.java 为程序的主Activity
* MySurfaceView.java 为程序的SurfaceView类
* Constant.java 常量类,将常量全部写在该类中
* OnDrawThread.java 该类的作用是时时刷新onDraw,进行画面的重绘
* PicRunThread.java 该类是控制duke图片运动的类
* */
import <a title="android" href="http://www.android-study.com/jichuzhishi/543.html">android</a>.app.Activity; //引入相关包
import <a title="android" href="http://www.android-study.com/jichuzhishi/543.html">android</a>.content.pm.ActivityInfo; //引入相关包
import <a title="android" href="http://www.android-study.com/jichuzhishi/543.html">android</a>.os.Bundle; //引入相关包
import <a title="android" href="http://www.android-study.com/jichuzhishi/543.html">android</a>.view.Window; //引入相关包
import <a title="android" href="http://www.android-study.com/jichuzhishi/543.html">android</a>.view.WindowManager; //引入相关包
public class MyActivity <a title="extends" href="http://www.android-study.com/jichuzhishi/543.html">extends</a> Activity {
/** Called when the activity is first created. */
private
MySurfaceView msv;
//得到surfaceView的引用
@Override
public
void
onCreate(Bundle savedInstanceState) {
//Activity的生命周期函数,该函数是在程序创建时调用
super
.onCreate(savedInstanceState);
msv=
new
MySurfaceView(MyActivity.
this
);
//实例化MySurfaceView的对象
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置屏幕显示没有title栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//设置全屏
//设置只允许横屏
this
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(msv);
//设置Activity显示的内容为msv
}
}
|
(\src\wyf\zcl\Constant.java)
1
2
3
4
5
6
7
8
9
10
11
12
|