实习第八天:SurfaceView显示动画效果(1)
2019-04-15 15:27发布
生成海报
原文地址:http://blog.csdn.net/ypist/article/details/8571032
一、基础知识:
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. 界面编辑(reslayoutmain.xml):
[java] view
plain copy
-
"1.0" encoding="utf-8"?>
-
"http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="@string/hello"
-
/>
-
2. 代码编辑:
(srcwyfzclMyActivity.Java)
[java] view
plain copy
-
package wyf.zcl;
-
-
-
-
-
-
-
-
-
import android.app.Activity;
-
import android.content.pm.ActivityInfo;
-
import android.os.Bundle;
-
import android.view.Window;
-
import android.view.WindowManager;
-
public class MyActivity extends Activity {
-
-
private MySurfaceView msv;
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
msv=new MySurfaceView(MyActivity.this);
-
requestWindowFeature(Window.FEATURE_NO_TITLE);
-
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
-
WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
-
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
-
setContentView(msv);
-
}
-
}
(srcwyfzclConstant.java)
[java] view
plain copy
-
package wyf.zcl;
-
import android.view.Display;
-
-
public class Constant {
-
public static int SCREENWIDTH=480;
-
public static int SCREENHEIGHT=320;
-
public static int PICWIDTH=64;
-
public static int PICHEIGHT=64;
-
public static int ONDRAWSPEED=30;
-
public static float PICXSPEED=1.5f;
-
public static float PICYSPEED=2;
-
public static int PICRUNSPEED=30;
-
public static int PICALPHASPEED=20;
-
}
(srcwyfzclMySurfaceView.java)
[java] view
plain copy
-
package wyf.zcl;
-
import android.content.Context;
-
import android.graphics.Bitmap;
-
import android.graphics.BitmapFactory;
-
import android.graphics.Canvas;
-
import android.graphics.Color;
-
import android.graphics.Paint;
-
import android.view.Display;
-
import android.view.SurfaceHolder;
-
import android.view.SurfaceView;
-
public class MySurfaceView extends SurfaceView
-
implements SurfaceHolder.Callback{
-
-
int dy=Display.DEFAULT_DISPLAY;
-
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮