Andriod课程设计

更新时间:2024-05-29 13:02:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

石家庄科技信息职业学院

Android涂鸦 课程设计(实训)报告

系 部 _______________专 业 _____________ 班 级 _______________学 号 ____________ 姓 名 _______________课 题 _____________ 指导教师 _______________报告成绩______________

起讫日期: 年 月 日 至 年 月 日

- 1 -

摘 要

Android:是谷歌于2007年公布的开放式源代码手机系统,一直受到人们的热捧。Android是基于Linux内核的软件平台和操作系统,它开放性就优于其他封闭式的手机系统,因此,任何人都可能根据自己的喜好将手机系统中的所有功能重新编写。这使得越来越多的人关注这个操作系统。本次作品音乐播放器就是基于Android平台的。

本论文主要阐述以面向对象的程序开发语言eclipse为开发工具, 基于智能手机Android之上设计一个涂鸦游戏。涂鸦它不仅能增强思维能力 提高智力,而且富含创造性,有助于情绪的宣泄。本系统是本系统中设定了人手绘画。它与javame的涂鸦设计有许多的不一样,Android本来就是全触摸的形式,而且操作非常简单。

- 2 -

目 录

一、摘要?????????????????????3 二、游戏设计???????????????????3 2.1游戏功能???????????????4 2.2运行环境需求???????????????? 12 三、具体实现???????????????????4 4.1环境的搭建???????????????4 4.2主要代码???????????????? 12 4.3运行效果图????????????????? 22 四、实训小结???????????????????????24 五、参考文献???????????????????3

- 3 -

前 言

游戏是我们日常生活的一个必不可少的娱乐项目,也带动了游戏相关产业市场的发展。游戏平台是经过从游戏机到电视游戏,然后从到pc游戏,到手机游戏,中间经历了很多的技术的更新和变革。但随着新技术的发展和我们生活水平质量的提高,手机开始逐渐变为普及,因为手机是非常小巧便捷的,而且方便的特性成为我们日常生活中必备通讯工具。而基于手机平台的游戏也就拥有的市场空间是巨大的,也因随着3g的开通和应用,手机游戏必然会迎来新的一轮游戏热潮!

现在的移动电话是小型的计算机,它的处理能力与台式机的标准处理能力相比很有限,但是足够运行一个小型的游戏。现在的手机的一个特性就是它们还是网络计算机,能够高速发送和接收数字数据。除了语音数据以外,它们还可以发送和接收其它类型的数据。所以这样的网络游戏也可以在手机上实现。短信类游戏就好像“虚拟宠物”那样。短信游戏是通过玩家和游戏服务商通过短信中的文字的内容来交流,达到进行游戏的目的的一种文字游戏。 由于短信游戏的整个游戏过程都是通过文字来表达,造成短信游戏的娱乐性较差。但是短信游戏却是兼容性最好的手机游戏之一。只要您的手机可以发短信,您就可以畅快的享受短信游戏给您带来的快乐了。图形类游戏更接近我们常说的“电视游戏”,玩家通过动画的形式来发展情节进行游戏。由于游戏采用了更为直观且更为精美的画面直接表现,因此图形类游戏的游戏性和代入感往往较文字类游戏高。因此广受玩家们的欢迎。嵌入式游戏是一种将游戏程序预先固化在手机的芯片中的游戏。由于这种游戏的所有数据都是预先固化在手机芯片中的,因此这种游戏是无法进行任何修改的。也就是说,您不能更换其它游戏的,只能玩您手机中已经存在的娱乐游戏,而且您也不能将它们删除。手机在网络游戏行业在多年的技术经验与运营经验的积累与总结之后,到了2009年迫不及待的需要创新出新的游戏模式和崭新的运营模式来推动手机游戏市场的爆发和发展。其中比较吸引人们的是手机的休闲类的游戏。应该可以说它是与PC网络游戏完全的脱离开了,而且充分发挥了自身的优势手机终端。休闲类的游戏恰巧与之相反,只需要很少的网络流量的网络环境,通过少量多次的游戏形式带给玩家流畅的游戏体验满足玩家瞬间提升的快感,但是同时却很难具有长期的“沉迷”性。

- 4 -

二、游戏设计

2.1游戏功能

1.用手动触屏绘画,方便简单。 2.可暂停,下回继续游戏。 3.选择图形绘画。 4.可以翻页从新绘画。

2.2运行环境需求

1.操作系统:Windows xp

2.支持环境:Android 2.1版本 3.开发环境:Eclipse 3.5 ADT 0.95

三、具体代码 3.1环境搭建:

任何事物要运行,都要有它的环境,Android也有它的环境才能够运行,下面介绍Android的开发环境配置. 搭建开发环境需要的软件: 操作系统:Windows XP或Linux

软件包:Android SDK(Software Development kit Java Development kit) 、ADT(Android Develoopment Tool) IDE环境:Eclipse IDE+ADT Eclipse3.3以上

JDK:Java Runtime Environment虚拟机 、(JDK)Java Development kit 安装步骤如下:

第一步:安装Java虚拟机sun-java6-jdk版本

第二步:安装Eclipse3.5工具 官网http://www.eclipse.org/downloads/ 选择版本:进行安装

第三步:安装Android SDK:首先要下载SDK,可以在这个网址下载Http://developer.android.com/sdk ,选择相应的系统进行安装

- 5 -

第四步:安装Android ADT插件

运行Eclipse,选择help->install new software 选择add,将会弹出一个框

图 3.4

点击OK,选择要安装的软件,占next,然后选择接受协议,直到安装成功,重启Eclipse软件,设置Android SDK Home,Window->refrence

图 3.5

在SDK location中输入SDK Tools路径:D:\\android

- 6 -

software\\android-sdk-windows 点击OK

3.2其他主要代码:

1. package org.vhow.paintpad.drawings;

import org.vhow.paintpad.tools.Brush;

import android.graphics.Canvas; /**

* A circle. */

public class Circle extends Drawing { @Override public void draw(Canvas canvas) { canvas.drawCircle( this.startX + (this.stopX - this.startX) / 2, this.startY + (this.stopY - this.startY) / 2, Math.abs(this.startX - this.stopX) / 2, Brush.getPen()); } }

2. package org.vhow.paintpad.drawings;

import android.graphics.Canvas; /**

* Abstract shape. All the shapes in this application extends this class. */

public abstract class Drawing { public float startX; public float startY; public float stopX; public float stopY; public void reset() {

- 7 -

this.startX = 0; this.startY = 0; this.stopX = 0; this.stopY = 0; } /** * A abstract method, that all the shapes must implement. * * @param canvas * A canvas to draw on. */ public abstract void draw(Canvas canvas); public void fingerDown(float x, float y, Canvas canvas) { this.reset(); this.startX = x; this.startY = y; } public void fingerMove(float x, float y, Canvas canvas) { this.stopX = x; this.stopY = y; } public void fingerUp(float x, float y, Canvas canvas) { this.stopX = x; this.stopY = y; this.draw(canvas); this.reset(); } }

3.package org.vhow.paintpad.drawings; /**

* Factory class, used to generate drawing. */

public class DrawingFactory { Drawing drawing = null;

- 8 -

/** * @param id * The id of the drawing. * @return The Drawing instance with the id. */ public Drawing createDrawing(int id) { switch (id) { case DrawingId.DRAWING_PATHLINE: drawing = new PathLine(); break; case DrawingId.DRAWING_STRAIGHTLINE: drawing = new StraightLine(); break; case DrawingId.DRAWING_RECT: drawing = new Rect(); break; case DrawingId.DRAWING_OVAL: drawing = new Oval(); break; case DrawingId.DRAWING_CIRCLE: drawing = new Circle(); break; case DrawingId.DRAWING_POINTS: drawing = new Points(); break; case DrawingId.DRAWING_ERASER: drawing = new Eraser(); break; } return drawing; } }

4. package org.vhow.paintpad.drawings; /**

* Defines the IDs of the drawings. */

public class DrawingId { public static final int DRAWING_PATHLINE = 0;

- 9 -

public static final int DRAWING_STRAIGHTLINE = 1; public static final int DRAWING_RECT = 2; public static final int DRAWING_OVAL = 3; public static final int DRAWING_CIRCLE = 4; public static final int DRAWING_POINTS = 5; public static final int DRAWING_ERASER = 6; }

5. package org.vhow.paintpad.drawings;

import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; /**

* An earser, drawing the track line with the color of the bitmap's background * color. */

public class Eraser extends Drawing { Path mPath = null; private float mX, mY; private static final float TOUCH_TOLERANCE = 4; Paint eraser; public Eraser() { mPath = new Path(); eraser = new Paint(); eraser.setColor(Color.WHITE); eraser.setStrokeWidth(5f); } @Override public void draw(Canvas canvas) { System.out.println(\ canvas.drawPath(this.mPath, eraser); } @Override public void fingerDown(float x, float y, Canvas canvas) { mPath.reset();

- 10 -

mPath.moveTo(x, y); this.mX = x; this.mY = y; } @Override public void fingerMove(float x, float y, Canvas canvas) { float dx = Math.abs(x - mX); float dy = Math.abs(y - mY); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2); mX = x; mY = y; } this.draw(canvas); } @Override public void fingerUp(float x, float y, Canvas canvas) { mPath.lineTo(mX, mY); this.draw(canvas); mPath.reset(); } }

6. package org.vhow.paintpad.drawings;

import org.vhow.paintpad.tools.Brush;

import android.graphics.Canvas; import android.graphics.RectF;

public class Oval extends Drawing { RectF rectF = null; public Oval() { rectF = new RectF(); } @Override

- 11 -

public void draw(Canvas canvas) { rectF.left = this.startX; rectF.right = this.stopX; rectF.top = this.startY; rectF.bottom = this.stopY; canvas.drawOval(rectF, Brush.getPen()); } }

7. package org.vhow.paintpad.drawings;

import org.vhow.paintpad.tools.Brush;

import android.graphics.Canvas; import android.graphics.Path; /**

* Track the finger's movement on the screen. */

public class PathLine extends Drawing { Path mPath = null; private float mX, mY; private static final float TOUCH_TOLERANCE = 4; public PathLine() { mPath = new Path(); } @Override public void draw(Canvas canvas) { canvas.drawPath(this.mPath, Brush.getPen()); } @Override public void fingerDown(float x, float y, Canvas canvas) { mPath.reset(); mPath.moveTo(x, y); this.mX = x; this.mY = y;

- 12 -

} @Override public void fingerMove(float x, float y, Canvas canvas) { float dx = Math.abs(x - mX); float dy = Math.abs(y - mY); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2); mX = x; mY = y; } this.draw(canvas); } @Override public void fingerUp(float x, float y, Canvas canvas) { mPath.lineTo(mX, mY); this.draw(canvas); mPath.reset(); } }

8. package org.vhow.paintpad.drawings;

import org.vhow.paintpad.tools.Brush;

import android.graphics.Canvas; import android.graphics.Paint; /**

* Some points. */

public class Points extends Drawing { Paint pen; public Points() { pen = new Paint(Brush.getPen()); pen.setStyle(Paint.Style.FILL); }

- 13 -

@Override public void draw(Canvas canvas) { canvas.drawCircle(stopX, stopY, Brush.getPen().getStrokeWidth() + 1, pen); } @Override public void fingerDown(float x, float y, Canvas canvas) { canvas.drawCircle(x, y, Brush.getPen().getStrokeWidth() + 1, pen); } @Override public void fingerMove(float x, float y, Canvas canvas) { canvas.drawCircle(x, y, Brush.getPen().getStrokeWidth() + 1, pen); } }

9. package org.vhow.paintpad.drawings;

import org.vhow.paintpad.tools.Brush;

import android.graphics.Canvas; /**

* A rectangle. */

public class Rect extends Drawing { @Override public void draw(Canvas canvas) { canvas.drawRect(this.startX, this.startY, this.stopX, this.stopY, Brush.getPen()); } }

10. package org.vhow.paintpad.drawings;

import org.vhow.paintpad.tools.Brush;

import android.graphics.Canvas;

- 14 -

/**

* A straight line. */

public class StraightLine extends Drawing { @Override public void draw(Canvas canvas) { canvas.drawLine(this.startX, this.startY, this.stopX, this.stopY, Brush.getPen()); } }

4.2主要代码:

1. package org.vhow.paintpad.setting;

import java.text.SimpleDateFormat; import java.util.zip.ZipEntry; import java.util.zip.ZipFile;

import org.vhow.paintpad.R;

import org.vhow.paintpad.helper.ColorPickerDialog; import org.vhow.paintpad.tools.Brush;

import android.content.DialogInterface; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo;

import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle;

import android.preference.CheckBoxPreference; import android.preference.Preference;

import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceActivity; /**

* When the user select \ * implements OnPreferenceClickListener interface, when the user click * Preference the onPreferenceClick() will be called. */

public class SettingsActivity extends PreferenceActivity implements OnPreferenceClickListener, ColorPickerDialog.OnColorChangedListener { SharedPreferences prefs = null; Preference pen_width = null;

- 15 -

Preference pen_color = null; Preference canvas_bg_color = null; String pen_width_key; String pen_color_key; CheckBoxPreference pen_style = null; Preference versionName; Preference lastBuildTime; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** * Inflates the settings.xml to this activity. */ addPreferencesFromResource(R.xml.settings); pen_width_key = getResources().getString(R.string.pen_width_key); pen_color_key = getResources().getString(R.string.pen_color_key); /** * Find the target to be handled. */ pen_width = (Preference) findPreference(getResources().getString( R.string.pen_width_key)); pen_color = (Preference) findPreference(getString(R.string.pen_color_key)); versionName = (Preference) findPreference(getString(R.string.setting_about_version_key)); lastBuildTime = (Preference) findPreference(getString(R.string.setting_about_build_key)); versionName.setSummary(getVersionName()); lastBuildTime.setSummary(getLastBuiltTime()); /** * Register the listeners */ pen_width.setOnPreferenceClickListener(this); pen_color.setOnPreferenceClickListener(this); }

- 16 -

private String getLastBuiltTime() { String lastBuiltTimeString = \ try { ApplicationInfo ai = getPackageManager().getApplicationInfo( getPackageName(), 0); ZipFile zf = new ZipFile(ai.sourceDir); ZipEntry ze = zf.getEntry(\ long time = ze.getTime(); lastBuiltTimeString = new SimpleDateFormat(\ .format(new java.util.Date(time)); }

}

catch (Exception e) { // Just leave the last built time to be \}

return lastBuiltTimeString;

private String getVersionName() { String versionName = \ try { versionName = this.getPackageManager().getPackageInfo( getPackageName(), 0).versionName; } catch (NameNotFoundException e) { // Just leave the versionName to be \ } return versionName; }

/**

* Handle the event that the preference is clicked. */

public boolean onPreferenceClick(Preference preference) { if (preference.getKey().equals(pen_color_key))

- 17 -

{ String str = getResources().getString(R.string.tip_choose_color); new ColorPickerDialog(this, this, Brush.getPen().getColor(), str) .show(); } else if (preference.getKey().equals(pen_width_key)) { showSetBrushWidthDialog(); } return true; } /** * Pop up a dialog with SeekBar to control the width of the brush. */ private void showSetBrushWidthDialog() { SeekBarDialog seekBarDialog = new SeekBarDialog(this); seekBarDialog.setTitle(Brush.getPen().getStrokeWidth() + \ seekBarDialog.setButton( getResources().getString(R.string.alert_dialog_ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.dismiss(); } }); seekBarDialog.show(); } /** * When brush's color is changed, this method will be called. */ public void colorChanged(int color) { Brush pen = Brush.getPen(); pen.setColor(color); } }

2. package org.vhow.paintpad.setting;

import org.vhow.paintpad.tools.Brush;

- 18 -

import android.app.AlertDialog; import android.content.Context; import android.os.Bundle;

import android.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; /**

* Use this class to set the width of the brush. */

public class SeekBarDialog extends AlertDialog { Context context; TextView textView = null; SeekBar seekBar = null; public SeekBarDialog(Context context) { this(context, android.R.style.Theme_Panel); this.context = context; } public SeekBarDialog(Context context, int theme) { super(context, theme); } @Override protected void onCreate(Bundle savedInstanceState) { this.seekBar = new SeekBar(this.context); setSeekBar(); setView(seekBar); super.onCreate(savedInstanceState); } private void setSeekBar() { this.seekBar.setMax(20); this.seekBar.setProgress((int) Brush.getPen().getStrokeWidth()); this.seekBar.setOnSeekBarChangeListener(seekBarListener); }

- 19 -

OnSeekBarChangeListener seekBarListener = new OnSeekBarChangeListener() { public void onStopTrackingTouch(SeekBar seekBar) { } public void onStartTrackingTouch(SeekBar seekBar) { } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { Brush.getPen().setStrokeWidth(progress); SeekBarDialog.this.setTitle(Brush.getPen().getStrokeWidth() + \ } }; /** * @param max * the max width of the brush */ public void setSeekBarMax(int max) { this.seekBar.setMax(max); } public int getSeekBarMax() { return this.seekBar.getMax(); } }

3. package org.vhow.paintpad.helper;

import android.app.Dialog; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Shader;

import android.graphics.SweepGradient;

- 20 -

import android.os.Bundle;

import android.view.MotionEvent; import android.view.View;

public class ColorPickerDialog extends Dialog { String title; public interface OnColorChangedListener { void colorChanged(int color); } private OnColorChangedListener mListener; private int mInitialColor; private static class ColorPickerView extends View { private Paint mPaint; private Paint mCenterPaint; private final int[] mColors; private OnColorChangedListener mListener; ColorPickerView(Context c, OnColorChangedListener l, int color) { super(c); mListener = l; mColors = new int[] { 0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00, 0xFFFF0000 }; Shader s = new SweepGradient(0, 0, mColors, null); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setShader(s); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(32); mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mCenterPaint.setColor(color); mCenterPaint.setStrokeWidth(5); } private boolean mTrackingCenter; private boolean mHighlightCenter;

- 21 -

@Override

protected void onDraw(Canvas canvas) { float r = CENTER_X - mPaint.getStrokeWidth() * 0.5f; }

canvas.translate(CENTER_X, CENTER_X);

canvas.drawOval(new RectF(-r, -r, r, r), mPaint);

canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint); if (mTrackingCenter) { int c = mCenterPaint.getColor(); mCenterPaint.setStyle(Paint.Style.STROKE); }

if (mHighlightCenter) { mCenterPaint.setAlpha(0xFF); } else { mCenterPaint.setAlpha(0x80); }

canvas.drawCircle(0, 0, CENTER_RADIUS + mCenterPaint.getStrokeWidth(), mCenterPaint); mCenterPaint.setStyle(Paint.Style.FILL); mCenterPaint.setColor(c);

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(CENTER_X * 2, CENTER_Y * 2); }

private static final int CENTER_X = 100; private static final int CENTER_Y = 100;

private static final int CENTER_RADIUS = 32; private int ave(int s, int d, float p) { return s + java.lang.Math.round(p * (d - s));

- 22 -

}

private int interpColor(int colors[], float unit) { if (unit <= 0) { return colors[0]; } if (unit >= 1) { return colors[colors.length - 1]; } }

float p = unit * (colors.length - 1); int i = (int) p; p -= i;

// now p is just the fractional part [0...1) and i is the index int c0 = colors[i]; int c1 = colors[i + 1];

int a = ave(Color.alpha(c0), Color.alpha(c1), p); int r = ave(Color.red(c0), Color.red(c1), p);

int g = ave(Color.green(c0), Color.green(c1), p); int b = ave(Color.blue(c0), Color.blue(c1), p); return Color.argb(a, r, g, b);

private static final float PI = 3.1415926f;

@Override

public boolean onTouchEvent(MotionEvent event) { float x = event.getX() - CENTER_X; float y = event.getY() - CENTER_Y; boolean inCenter = java.lang.Math.sqrt(x * x + y * y) <= CENTER_RADIUS;

switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mTrackingCenter = inCenter; if (inCenter) { mHighlightCenter = true;

- 23 -

}

}

invalidate(); break; } case MotionEvent.ACTION_MOVE: if (mTrackingCenter) { if (mHighlightCenter != inCenter) { mHighlightCenter = inCenter; invalidate(); } } else { float angle = (float) java.lang.Math.atan2(y, x); // need to turn angle [-PI ... PI] into unit [0....1] float unit = angle / (2 * PI); if (unit < 0) { unit += 1; } mCenterPaint.setColor(interpColor(mColors, unit)); invalidate(); } break; case MotionEvent.ACTION_UP: if (mTrackingCenter) { if (inCenter) { mListener.colorChanged(mCenterPaint.getColor()); } mTrackingCenter = false; // so we draw w/o halo invalidate(); } break; }

return true;

public ColorPickerDialog(Context context, OnColorChangedListener listener, int initialColor, String str) {

- 24 -

}

}

super(context);

mListener = listener;

mInitialColor = initialColor; title = str;

@Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); OnColorChangedListener l = new OnColorChangedListener() { public void colorChanged(int color) { mListener.colorChanged(color); dismiss(); } }; }

setContentView(new ColorPickerView(getContext(), l, mInitialColor)); setTitle(title);

3.3运行效果图

- 25 -

- 26 -

四、实训小结

通过一个小游戏的程序开发,让我更加深入的去了解面向对象的程序设计,更加理解了面向对象的三个特性封装、继承与多态给软件开发带来的好处。在这次的小游戏开发的过程中我自己也是尽量的体现出了这三个特性,这样使我在整个的开发过程中思维都是清析的,并且当出现BUG时我也能够很快的去准确的将错误定位。所以在这次的游戏开发练习中,我学会了很多的软件开发的一些原则和基本的方法,也锻炼了自己的一个独立思维的能力。 五、参考文献

1. 专著:[M]作者:余志龙 陈昱勋 郑名杰 陈小凤 郭秩均。

书名[M]:Google Android SDK开发范例大全(第二版)。 出版地:北京

- 27 -

出版社:人民邮电出版社 出版年份:2010

2.网络资料:参考播放功能和SD卡的资料,其他Android资料。

- 28 -

本文来源:https://www.bwwdw.com/article/dly6.html

Top