MainActivity
package org.wp.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
public static Activity INSTANCE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
INSTANCE = this;
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); // 全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new MySurfaceView(this));
}
@Override
protected void onDestroy() {
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
MySurfaceView
package org.wp.activity;
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.SurfaceHolder;
import android.view.SurfaceView;
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private static final String TAG = "MySurfaceView";
private SurfaceHolder sfh;
private Resources res;
private Bitmap bmp_login;
private Bitmap bmp_background;
private Paint paint;
private Canvas canvas;
private int ScreenW, ScreenH;
private int bmp_login_x, bmp_login_y;
private boolean flag;
private boolean flag_zh;
public static String str_username = "onewayonelife";
public static String str_password = "onewayonelifeIteyeCom";
public MySurfaceView(Context context) {
super(context);
this.setKeepScreenOn(true);
this.setFocusable(true);
sfh = this.getHolder();
sfh.addCallback(this);
res = this.getResources();
bmp_login = BitmapFactory.decodeResource(res, R.drawable.register); // 登录图
bmp_background = BitmapFactory.decodeResource(res, R.drawable.one_piece); // 背景图片
paint = new Paint();
paint.setAntiAlias(true);
}
private void draw() {
canvas = sfh.lockCanvas();
if (canvas != null) { // 当点击home键 或者返回按键的时候canvas是得不到的,这里要注意
canvas.drawBitmap(bmp_background, -(bmp_background.getWidth() - ScreenW),
-(bmp_background.getHeight() - ScreenH), paint); // 背景图片
canvas.drawBitmap(bmp_login, bmp_login_x, bmp_login_y, paint); // 登录图片
paint.setColor(Color.RED);
canvas.drawText(str_username, bmp_login_x + 20, bmp_login_y + 42 + 15, paint); // 账号文字
int passwordLen = str_password.length();
String passwordTemp = "";
if (passwordLen > 15) {
passwordLen = 15;
}
for (int i = 0; i < passwordLen; i++) {
passwordTemp += "*";
}
canvas.drawText(passwordTemp, bmp_login_x + 20, bmp_login_y + 79 + 15, paint); // 密码文字
if (flag_zh) {
paint.setColor(Color.YELLOW);
canvas.drawCircle(bmp_login_x + 23, bmp_login_y + 113, 4, paint); // 记住账号圆形
}
sfh.unlockCanvasAndPost(canvas);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float pointx = event.getX();
float pointy = event.getY();
// 点击账号输入框
if (pointx > bmp_login_x + 14 && pointx < bmp_login_x + 14 + 117) {
// 横向点击区域在账号输入框范围
if (pointy > bmp_login_y + 42 && pointy < bmp_login_y + 42 + 15) {
// 纵向点击区域在账号输入框范围
Intent intent = new Intent();
intent.putExtra("count", 1);
intent.putExtra("onewayonelife", str_username);
intent.setClass(MainActivity.INSTANCE, Register.class);
MainActivity.INSTANCE.startActivity(intent);
}
}
// 点击密码输入框
if (pointx > bmp_login_x + 14 && pointx < bmp_login_x + 14 + 117) {
// 横向点击区域在密码输入框范围
if (pointy > bmp_login_y + 79 && pointy < bmp_login_y + 79 + 15) {
// 纵向点击区域在密码输入框范围
Intent intent = new Intent();
intent.putExtra("count", 2);
intent.putExtra("onewayonelife", str_password);
intent.setClass(MainActivity.INSTANCE, Register.class);
MainActivity.INSTANCE.startActivity(intent);
}
}
// 点击记住账号
if (pointx > bmp_login_x + 15 && pointx < bmp_login_x + 15 + 15) {
// 横向点击区域在记住账号范围
if (pointy > bmp_login_y + 104 && pointy < bmp_login_y + 104 + 15) {
// 纵向点击区域在记住账号范围
flag_zh = !flag_zh;
}
}
// 点击登录
if (pointx > bmp_login_x + 42 && pointx < bmp_login_x + 42 + 60) {
// 横向点击区域在登录按键范围
if (pointy > bmp_login_y + 123 && pointy < bmp_login_y + 123 + 24) {
// 纵向点击区域在登录按键范围
Intent intent = new Intent();
intent.putExtra("count", 3);
intent.putExtra("onewayonelife_username", str_username);
intent.putExtra("onewayonelife_password", str_password);
intent.setClass(MainActivity.INSTANCE, Register.class);
MainActivity.INSTANCE.startActivity(intent);
}
}
// 点击退出按钮
if (pointx > bmp_login_x + 120 && pointx < bmp_login_x + 120 + 22) {
if (pointy > bmp_login_y + 5 && pointy < bmp_login_y + 5 + 22) {
try {
android.os.Process.killProcess(android.os.Process.myPid());
} catch (Exception e) {
e.printStackTrace();
}
}
}
return super.onTouchEvent(event);
}
@Override
public void run() {
while (!flag) {
draw();
try {
Thread.sleep(100);
} catch (Exception e) {
Log.v(TAG, "run exception");
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
ScreenW = this.getWidth();
ScreenH = this.getHeight();
bmp_login_x = ScreenW / 2 - bmp_login.getWidth() / 2; // 登录图x坐标位置
bmp_login_y = ScreenH / 2 - bmp_login.getHeight() / 2; // 登录图y坐标位置
new Thread(this).start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
Register
package org.wp.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Register extends Activity {
private Register rs;
private byte count;
private LinearLayout llay;
private Button ok;
private EditText et;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rs = this;
Intent intent = this.getIntent();
count = (byte) intent.getIntExtra("count", 0);
llay = new LinearLayout(this);
ok = new Button(this);
ok.setWidth(100);
ok.setText("确定");
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (count == 1) {
MySurfaceView.str_username = et.getText().toString();
} else if (count == 2) {
MySurfaceView.str_password = et.getText().toString();
}
rs.finish();
}
});
String temp_str = "";
String temp_username = "";
String temp_password = "";
et = new EditText(this);
tv = new TextView(this);
if (count != 3) {
temp_str = intent.getStringExtra("onewayonelife");
if (count == 1) {
rs.setTitle("请输入帐号!");
} else {
rs.setTitle("请输入密码!");
}
llay.addView(et);
llay.addView(ok);
if (temp_str != null) {
et.setText(temp_str);
}
} else {
temp_username = intent.getStringExtra("onewayonelife_username");
temp_password = intent.getStringExtra("onewayonelife_password");
rs.setTitle("您输入的信息:");
tv.setText("帐号:" + temp_username + "\n" + "密码:" + temp_password);
llay.addView(tv);
llay.addView(ok);
}
setContentView(llay);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.wp.activity" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Register" android:theme="@android:style/Theme.Dialog" android:screenOrientation="landscape" /> </application> <uses-sdk android:minSdkVersion="8" /> </manifest>