package xiaohang.zhimeng;
import java.math.BigDecimal;
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.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class GameView extends View implements Runnable {
// 声明Paint对象
private Paint mPaint = null;
private static Integer width;// 分辨率宽
private static Integer heigth;// 分辨率高
private static BigDecimal vertical;// 基础高
private static Integer widthSpacing = 5;// 横向偏移量
private static Integer heigthSpacing = 5;// 纵向偏移量
private Bitmap planeOrderAppBmp;// 机票审批图片
private Bitmap myOrderBmp;// 我的订单
private Bitmap planeBookBmp;// 机票审批图片
private Bitmap myInfoBmp;// 机票审批图片
/* 需要根据比例进行设置 */
private static Integer cross;// 基础边长
private static Integer wordSize = 32;// 字体大小
private Integer bitMapDistance = 15;// 图片与文字之间的距离
public GameView(Context context, AttributeSet attr) {
super(context, attr);
// 构建画笔对象
mPaint = new Paint();
/* 初始化图片 设置图片缩放 */
initPlayOrderApp(context, vertical.intValue());
initMyOrder(context, vertical.intValue());
initplaneBook(context, vertical.intValue());
initMyInfo(context, vertical.intValue());
// 开启线程
new Thread(this).start();
}
/**
* 初始化机票审批图片
*/
public void initPlayOrderApp(Context context, Integer height) {
BitmapFactory.Options options = new BitmapFactory.Options();
planeOrderAppBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.cha, options);
// int newWidth = height;// 新图片宽度
// int newHeight = height;// 新图片高度
// // 计算缩放比例
// float scaleWidth = ((float) newWidth) / planeOrderAppBmp.getWidth();
// float scaleHeight = ((float) newHeight) /
// planeOrderAppBmp.getHeight();
// // 取得想要缩放的matrix参数
// Matrix matrix = new Matrix();
// matrix.postScale(scaleWidth, scaleHeight);
// planeOrderAppBmp = Bitmap.createBitmap(planeOrderAppBmp, 0, 0,
// planeOrderAppBmp.getWidth(),
// planeOrderAppBmp.getHeight(), matrix, true);
}
/**
* 初始化我的订单图片
*/
public void initMyOrder(Context context, Integer height) {
BitmapFactory.Options options = new BitmapFactory.Options();
myOrderBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.cha, options);
// int newWidth = height - 10;// 新图片宽度
// int newHeight = height - 10;// 新图片高度
// // 计算缩放比例
// float scaleWidth = ((float) newWidth) / myOrderBmp.getWidth();
// float scaleHeight = ((float) newHeight) / myOrderBmp.getHeight();
// // 取得想要缩放的matrix参数
// Matrix matrix = new Matrix();
// matrix.postScale(1, 1);
// myOrderBmp = Bitmap.createBitmap(myOrderBmp, 0, 0,
// myOrderBmp.getWidth(), 78, matrix, true);
}
/**
* 初始化我的订单图片
*/
public void initplaneBook(Context context, Integer height) {
BitmapFactory.Options options = new BitmapFactory.Options();
planeBookBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.cha, options);
// int newWidth = height;// 新图片宽度
// int newHeight = height;// 新图片高度
// // 计算缩放比例
// float scaleWidth = ((float) newWidth) / planeBookBmp.getWidth();
// float scaleHeight = ((float) newHeight) / planeBookBmp.getHeight();
// // 取得想要缩放的matrix参数
// Matrix matrix = new Matrix();
// matrix.postScale(scaleWidth, scaleHeight);
// planeBookBmp = Bitmap.createBitmap(planeBookBmp, 0, 0,
// planeBookBmp.getWidth(), planeBookBmp.getHeight(),
// matrix, true);
}
/**
* 初始化我的订单图片
*/
public void initMyInfo(Context context, Integer height) {
BitmapFactory.Options options = new BitmapFactory.Options();
myInfoBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.cha, options);
// int newWidth = height;// 新图片宽度
// int newHeight = height;// 新图片高度
// // 计算缩放比例
// float scaleWidth = ((float) newWidth) / myInfoBmp.getWidth();
// float scaleHeight = ((float) newHeight) / myInfoBmp.getHeight();
// // 取得想要缩放的matrix参数
// Matrix matrix = new Matrix();
// matrix.postScale(scaleWidth, scaleHeight);
// myInfoBmp = Bitmap.createBitmap(myInfoBmp, 0, 0,
// myInfoBmp.getWidth(), myInfoBmp.getHeight(), matrix, true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
if (x > width / 2 - cross && x < width / 2 + cross) {
if (y > heigth / 9 && y < heigth / 9 + vertical.intValue()) {
System.out.println("点击屏幕图像触控事件");
}
}
return super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 设置画布为黑色背景
canvas.drawColor(Color.WHITE);
// 取消锯齿
mPaint.setAntiAlias(true);
// 将画笔设置为实心
mPaint.setStyle(Paint.Style.FILL);
{
/* 上 */
// 绘制多边形
Path path = new Path();
path.moveTo(width / 2 - cross, heigth / 9);
path.lineTo(width / 2 + cross, heigth / 9);
path.lineTo(width / 2 + cross * 2, heigth / 9 + vertical.intValue());
path.lineTo(width / 2 + cross, heigth / 9 + vertical.intValue() * 2);
path.lineTo(width / 2 - cross, heigth / 9 + vertical.intValue() * 2);
path.lineTo(width / 2 - cross * 2, heigth / 9 + vertical.intValue());
// 使这些点构成封闭的多边形
path.close();
mPaint.setTextSize(wordSize);
mPaint.setColor(Color.rgb(241, 241, 241));
// mPaint.setColor(Color.rgb(49, 169, 238));
// 绘制这个多边形
canvas.drawPath(path, mPaint);
// canvas.drawBitmap(planeOrderAppBmp, width / 2 -
// planeOrderAppBmp.getWidth() / 2,
// heigth / 9 + vertical.intValue() / 3, mPaint);
mPaint.setColor(Color.WHITE);
Float strwid = mPaint.measureText("测试");
// canvas.drawText("机票审批", width / 2 - strwid / 2, heigth / 9 +
// vertical.intValue() * 2 - bitMapDistance,
// mPaint);
}
{ /* 中 */
// 绘制多边形
Path path = new Path();
path.moveTo(width / 2 - cross, heigth / 9 + vertical.intValue() * 2 + heigthSpacing);
path.lineTo(width / 2 + cross, heigth / 9 + vertical.intValue() * 2 + heigthSpacing);
path.lineTo(width / 2 + cross * 2, heigth / 9 + vertical.intValue() * 3 + heigthSpacing);
path.lineTo(width / 2 + cross, heigth / 9 + vertical.intValue() * 4 + heigthSpacing);
path.lineTo(width / 2 - cross, heigth / 9 + vertical.intValue() * 4 + heigthSpacing);
path.lineTo(width / 2 - cross * 2, heigth / 9 + vertical.intValue() * 3 + heigthSpacing);
// 使这些点构成封闭的多边形
path.close();
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(wordSize * 1.5f);
// 绘制这个多边形
canvas.drawPath(path, mPaint);
mPaint.setColor(Color.rgb(237, 109, 0));
Float strwid = mPaint.measureText("测试");
canvas.drawText("测试", width / 2 - strwid / 2, heigth / 9 + vertical.intValue() * 3 + wordSize, mPaint);
}
{
// 绘制多边形 左上
Path path = new Path();
path.moveTo((width / 2 - cross) - (cross * 3 + widthSpacing), heigth / 9 + vertical.intValue());
path.lineTo((width / 2 + cross) - (cross * 3 + widthSpacing), heigth / 9 + vertical.intValue());
path.lineTo((width / 2 + cross * 2) - (cross * 3 + widthSpacing), heigth / 9 + vertical.intValue() * 2);
path.lineTo((width / 2 + cross) - (cross * 3 + widthSpacing), heigth / 9 + vertical.intValue() * 3);
path.lineTo((width / 2 - cross) - (cross * 3 + widthSpacing), heigth / 9 + vertical.intValue() * 3);
path.lineTo((width / 2 - cross * 2) - (cross * 3 + widthSpacing), heigth / 9 + vertical.intValue() * 2);
// 使这些点构成封闭的多边形
path.close();
mPaint.setColor(Color.rgb(241, 241, 241));
// 绘制这个多边形
canvas.drawPath(path, mPaint);
}
//
// {
// // 绘制多边形 右上上
// Path path = new Path();
// path.moveTo(width / 2 - cross + (cross * 3 + widthSpacing),
- 1
- 2
前往页