理解自制Su易做图ceView代码
概述:
最近在读himi的android游戏开发专栏,其中通过前几张剖析View的继承接口类发现Su易做图ceView比较适合做Game Dev然后本人也动手跟着himi的代码敲了几下。最终虽然也达到了himi的运行效果。但是其中还是经过了好多疑点。现在本人将自己的理解发布出来,欢迎交流与学习
Code List:
[java]
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Su易做图ceHolder;
import android.view.Su易做图ceHolder.Callback;
import android.view.Su易做图ceView;
public class LoginView extends Su易做图ceView implements Callback,Runnable{
private Canvas canvas; //定义画布对象
private Thread thread; //定义线程线程对应
private int ScreenW,ScreenH; //定义屏幕的宽度和高度
private Paint paint; //定义画笔对象
private Su易做图ceHolder sh; //声明Su易做图ceHolder对象
private Resources resources; //声明资源对象
private Bitmap bitmapr; //注册bitmap
private Bitmap bitmapb; //背景bitmap
private int bp_x,bp_y; //注册背景图宽度高�?
private boolean flag;
public static String zh_k = "注册页面";
private boolean zh_flag = true;
private String str_pass = "aaa";
//初始构�?
public LoginView(Context context) {
super(context);
//
resources = this.getResources();
bitmapr = BitmapFactory.decodeResource(resources, R.drawable.register); //注册窗口
bitmapb = BitmapFactory.decodeResource(resources, R.drawable.duola); //注册背景�?
thread = new Thread(this);
sh = this.getHolder();
sh.addCallback(this);
paint = new Paint();
paint.setColor(Color.RED);
paint.setAntiAlias(true); //去掉锯齿
this.setFocusable(true);
this.setKeepScreenOn(true); //设置屏幕常亮
resources = this.getResources();
}
public void draw(){
canvas = sh.lockCanvas(); //得到画布对象
paint.setColor(Color.RED);
if(null != canvas){ //在设置横屏的时候 坐标算法如下
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bitmapb,
-(bitmapb.getWidth() - ScreenW), -(bitmapb
.getHeight() - ScreenH), paint);
//2、3参数为x坐标和y坐标,就是位图paint的左上角位置
canvas.drawBitmap(bitmapr, bp_x,bp_y, paint);
sh.unlockCanvasAndPost(canvas);
}
}
@Override
public void run() {
while(!flag){
draw();
try {
thread.sleep(1000);
} catch (Exception e) {
Log.v("run", "error");
}
}
}
@Override
public void su易做图ceCreated(Su易做图ceHolder holder) {
ScreenW = this.getWidth();
ScreenH = this.getHeight();
bp_x = ScreenW/2-bitmapr.getWidth()/2;
bp_y = ScreenH/2-bitmapr.getHeight()/2;
thread.start();
}
@Override
public void su易做图ceChanged(Su易做图ceHolder holder, int format, int width,
int height) {
}
@Override
public void su易做图ceDestroyed(Su易做图ceHolder holder) {
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
if(x > bp_x+14 && x < bp_x+129){ //锁定登陆框姓名区域X轴
if(y >= bp_y+43 && y <= bp_y+58){ //锁定登陆框姓名区域Y轴
&nbs
补充:移动开发 , Android ,