Android的入门开发:
1:在Eclipse中新建一个Android项目,基本流程是:新建一个项目,点击File-New-Android Project,然后在三 个框中分别输入你的package名、应用名、Activity名。
2: 写xlm,实体化xlm的布局、控件等。
3:如果你要实现监听或者页面的跳转之类的,你就需要在再写JAVA文件。
4:使用Android模拟器运行、调试程序,或者使用数据线连接手机进行调试。(提示:第一次启动Android模拟器的时候可能时间有点略久,有人第一次启动1个小时的)
5:在调试过程中可以使用Java中的System.out.println()语句或者Log.i()语句来进行测试语句是否执行或者输出某个变量或常量的值,但需要查看控制台Console旁边的LogCat才可以看到输出的语句或值。
6:关于rsc/values/strings.xlm文件里的文字,比如菜单的名字或者其他的标题之类的,假如你直接写文字,不会报错,但会出现警告,我们尽可能的把那些东西写在string.xlm中,因为以后写的代码多一些后,有时候需要修改的时候,就不用去到处去翻、到处找,只要修改string.xlm中的相对应的值就行了。
画图板的基本实现
先是一个简单的登录界面
login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:background="@drawable/login_background"
android:gravity="center" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帐号:"/>
<EditText
android:id="@+id/zhanghao"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"/>
<EditText
android:id="@+id/mima"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:id="@+id/butLogin"/>
</LinearLayout>
然后是一个画板的界面
draw.xlm
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/ivDrawing"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
MainActivity.java
package com.example.androidlogin;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText zhanghao;
private EditText mima;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button butLogin = (Button)this.findViewById(R.id.butLogin);//从R文件中获取登录按钮
zhanghao=(EditText)this.findViewById(R.id.zhanghao);//获取帐号文本框
mima=(EditText)this.findViewById(R.id.mima);//获取密码文本框
LoginListener ll = new LoginListener(this,this.zhanghao,this.mima);//实例化 LoginListener对象
butLogin.setOnClickListener(ll);为按钮添加监听器
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.draw, menu);
return true;
}
}
LoginListener.java
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
public class LoginListener implements OnClickListener {
private MainActivity ma ;
private EditText zhanghao;
private EditText mima;
public LoginListener(MainActivity la,EditText zhanghao,EditText mima){
this.ma = la;
this.zhanghao=zhanghao;
this.mima=mima;
}//传递密码、帐号文本框和MainActivity
public void onClick(View v) {
String a=zhanghao.getText().toString();//获取文本框上的文字
String b=mima.getText().toString();
if(a.equals("CY")&&b.equals("CY")){
Toast.makeText(ma, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
//设置从MainActivity跳转到DrawActivity
intent.setClass(ma, Game2048Activity.class);
//使用MainActivity来启动页面的跳转
ma.startActivity(intent);
}
else{
Toast.makeText(ma, "登录失败", Toast.LENGTH_SHORT).show();}
//出现一段时间后将会消失,
}
}//当鼠标点击事件源对象时,将会执行这一方法
DrawActivity.java
package com.example.androidlogin;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class DrawActivity extends Activity {
private DrawListener dl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.draw);
//获取事件源对象ImageView
ImageView ivDrawing = (ImageView)this.findViewById(R.id.ivDrawing);
//实例化DrawListener事件处理类的对象
dl = new DrawListener(this);
//给事件源设置触屏监听方法,指定事件处理类的对象dl
ivDrawing.setOnTouchListener(dl);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.draw, menu);//从R文件中获取菜单
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
dl.setItemId(item.getItemId());//获取菜单上item的文字信息,传递到DrawListener中
return true;
}
}
DrawListener.java
package com.example.androidlogin;
import java.util.Random;
import android.R.string;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
/**
* 事件处理类
*/
public class DrawListener implements OnTouchListener {
private double[] p=new double [2];
private double[] q=new double [2];
private float x1, y1, x2, y2, k, l;
private Bitmap bm;// 位图
private Canvas canvas;// 画布,提供画不同的图形方法
private Paint paint;// 画笔
private ImageView ivDraw;// 显示图片组件对象
private DrawActivity ac;
private String type = "line";
public DrawListener(DrawActivity da) {
// 实例化Paint画笔对象
paint = new Paint();this.ac = da;
}
public void zhixian(float x1,float y1,float x2,float y2,int j){
if(j==0) return ;
canvas.drawLine(x1, (float)(y1+50), (float)(x2-x1)/3+x1, (float)(y2+50),paint);
canvas.drawLine((float)((x2-x1)/3)*2+x1,(float)y1+50,x2,(float)(y2+50),paint);
j--;
zhixian(x1, y1+50, (x2-x1)/3+x1, y2+50,j);
zhixian(((x2-x1)/3)*2+x1,y1+50,x2,y2+50,j);
}
public void setItemId(int itemId) {
switch (itemId) {
case R.id.color_red:
paint.setColor(Color.RED);
break;
case R.id.color_green:
paint.setColor(Color.GREEN);
break;
case R.id.color_blue:
paint.setColor(Color.BLUE);
break;
case R.id.type_line:
type = "line";
break;
case R.id.type_oval:
type = "oval";
break;
case R.id.type_rect:
type = "rect";
break;
case R.id.type_roundrect:
type = "roundrect";
break;
case R.id.type_string:
type = "string";
break;
case R.id.type_fengxing1:
type = "fengxing1";
break;
case R.id.type_fengxing2:
type = "fengxing2";
break;
case R.id.type_fengxing3:
type = "fengxing3";
break;
case R.id.shuye:
type = "shuye";
break;
case R.id.digui:
type = "digui";
break;
case R.id.pencil:
type = "pencil";
break;
case R.id.rubber:
type = "rubber";
break;
case R.id.pengqiang:
type = "pengqiang";
break;
case R.id.picture:
type = "picture";
break;
}
}
/**
* 触屏事件的处理方法
*
* @param v就是事件源ImageView
* @param event是存储事件相关信心的对象
*/
public boolean onTouch(View v, MotionEvent event) {
Random rand = new Random();
if (bm == null) {
ivDraw = (ImageView) v;
// 创建一个和ImageView一样大小的32位真彩色位图对象。
bm = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Bitmap.Config.ARGB_8888);
// 根据bm位图对象实例化一个Canvas对象
canvas = new Canvas(bm);
}
int action = event.getAction();// 获取触屏的动作
// 绘制直线
if (type.equals("line")){
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
canvas.drawLine(x1, y1, x2, y2, paint);
break;}
}
else if (type.equals("rect")) {
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
RectF rect = new RectF(x1, y1, x2, y2);
canvas.drawRect(rect, paint);
break;}
}
else if (type.equals("oval")){
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
float r = 30;
canvas.drawCircle(x1,y1,r,paint);
break;}
}
else if (type.equals("roundrect")){
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
RectF rect = new RectF(x1, y1, x2, y2);
float r = 30;
canvas.drawRoundRect(rect, r, r, paint);
break;}
}
else if (type.equals("string")){
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
String a = "Yeah!";
canvas.drawText(a, 0, 5, x1, y1, paint);
break;}
}
else if(type.equals("fengxing1")){
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
p[0]=0;q[0]=0;
for(int i=0;i<1000;i++)
{
if(i==200)
paint.setColor(Color.BLUE);
if(i==400)
paint.setColor(Color.RED);
if(i==600)
paint.setColor(Color.GREEN);
if(i==800)
paint.setColor(Color.BLACK);
p[1]=Math.sin(q[0]*(-2))-Math.cos(p[0]*(-2));
q[1]=Math.sin(p[0]*(-1.2))-Math.cos(2*q[0]);
p[0]=p[1];q[0]=q[1];
k=(float)(p[1]*20+x2); l=(float)(q[1]*20+y2);
canvas.drawPoint(k, l, paint);
}
break;
}
}
else if(type.equals("fengxing2")){
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
p[0]=0;q[0]=0;
for(int i=0;i<1000;i++)
{
if(i==200)
paint.setColor(Color.BLUE);
if(i==400)
paint.setColor(Color.RED);
if(i==600)
paint.setColor(Color.GREEN);
if(i==800)
paint.setColor(Color.BLACK);
p[1]=(-6.56)*Math.sin(1.40*p[0])-Math.sin(1.56*q[0]);
q[1]=(1.40)*Math.cos(1.400*p[0])-Math.cos(1.56*q[0]);
p[0]=p[1];q[0]=q[1];
canvas.drawPoint((float)(p[1]*20)+x2,(float)(q[1]*20)+y2 , paint);
}
break;
}
}
else if(type.equals("fengxing3")){
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
System.out.println("Yeah");
p[0]=0;q[0]=0;
for(int i=0;i<1000;i++)
{
if(i==200)
paint.setColor(Color.BLUE);
if(i==400)
paint.setColor(Color.RED);
if(i==600)
paint.setColor(Color.GREEN);
if(i==800)
paint.setColor(Color.BLACK);
p[1]=q[0]-Math.signum(p[0])*Math.sqrt(Math.abs(p[0]));
q[1]=0.4-p[0];
p[0]=p[1];q[0]=q[1];
canvas.drawPoint((float)(p[1]*20)+x2,(float)(q[1]*20)+y2 , paint);
}
break;
}
}
else if(type.equals("shuye")){
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
p[0]=0;q[0]=0;
for(int i=0;i<10000;i++)
{
int o = rand.nextInt(100)+1;
if(o>=1&&o<=10)
{
p[1]=0;
q[1]=0.16*q[0];
p[0]=p[1];q[0]=q[1];
canvas.drawPoint((float)((-p[1])*20+x2),(float)((-q[1])*20+y2) , paint);
}
if(o>=11&&o<=18)
{
p[1]=0.2*p[0]+(-0.26)*q[0];
q[1]=0.23*p[0]+0.22*q[0]+0.16;
p[0]=p[1];q[0]=q[1];
canvas.drawPoint((float)((-p[1])*20+x2),(float)((-q[1])*20+y2) , paint);
}
if(o>=19&&o<=26)
{
p[1]=(-0.15)*p[0]+0.28*q[0];
q[1]=0.26*p[0]+0.24*q[0]+0.44;
p[0]=p[1];q[0]=q[1];
canvas.drawPoint((float)((-p[1])*20+x2),(float)((-q[1])*20+y2) , paint);
}
if(o>=27&&o<=100)
{
p[1]=0.75*p[0]+0.04*q[0];
q[1]=(-0.04)*p[0]+0.85*q[0]+1.6;
p[0]=p[1];q[0]=q[1];
canvas.drawPoint((float)((-p[1])*20+x2),(float)((-q[1])*20+y2) , paint);
}
}
break;
}
}
else if(type.equals("digui")){
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
canvas.drawLine(x1, y1, x2, y1,paint);
zhixian(x1,y1,x2,y1,6);
break;
}
}
else if(type.equals("pencil")){
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_MOVE:
x2 = event.getX();
y2 = event.getY();
canvas.drawLine(x1, y1,x2,y2,paint);
x1=x2;y1=y2;
break;
}
}
else if(type.equals("rubber")){
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_MOVE:
x2 = event.getX();
y2 = event.getY();
paint.setColor(Color.WHITE);
RectF rect = new RectF(x1, y1, x2, y2);
canvas.drawRect(rect, paint);
x1=x2;y1=y2;
break;
}
paint.setColor(Color.BLACK);
}
else if(type.equals("pengqiang")){
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_MOVE:
x2 = event.getX();
y2 = event.getY();
int o = rand.nextInt(4);
canvas.drawPoint(x2+o,y2+0, paint);
canvas.drawPoint(x2-o,y2-0, paint);
canvas.drawPoint(x2-o,y2+0, paint);
canvas.drawPoint(x2+o,y2-0, paint);
ivDraw.setImageBitmap(bm);
break;
}
}
else if(type.equals("picture")){
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
Bitmap ori = BitmapFactory.decodeResource(ac.getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(ori,x2,y2,paint);
break;
}
}
// 将bm位图对象设置成ImageView要显示的图片
ivDraw.setImageBitmap(bm);
return true;// 返回值false,表示该方法执行失败,之前所有执行过的代码都将回滚。
}
}
src/res/menu/draw.xlm
简单菜单的实现,更高端的还有上下文等菜单实现
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:title="颜色"
android:id="@+id/item_color"
android:showAsAction="always">
<menu>
<item android:title="红色"
android:icon="@drawable/ic_launcher"
android:id="@+id/color_red"/>
<item android:title="绿色"
android:icon="@drawable/ic_launcher"
android:id="@+id/color_green"/>
<item android:title="蓝色"
android:icon="@drawable/ic_launcher"
android:id="@+id/color_blue"/>
</menu>
</item>
<item android:title="类型"
android:id="@+id/item_type"
android:showAsAction="always">
<menu>
<item android:title="直线"
android:icon="@drawable/ic_launcher"
android:id="@+id/type_line" />
<item android:title="矩形"
android:icon="@drawable/ic_launcher"
android:id="@+id/type_rect"/>
<item android:title="圆"
android:icon="@drawable/ic_launcher"
android:id="@+id/type_oval"/>
<item android:title="圆角矩形"
android:icon="@drawable/ic_launcher"
android:id="@+id/type_roundrect"/>
<item android:title="字符"
android:icon="@drawable/ic_launcher"
android:id="@+id/type_string"/>
<item android:title="分形1"
android:icon="@drawable/ic_launcher"
android:id="@+id/type_fengxing1"/>
<item android:title="分形2"
android:icon="@drawable/ic_launcher"
android:id="@+id/type_fengxing2"/>
<item android:title="分形3"
android:icon="@drawable/ic_launcher"
android:id="@+id/type_fengxing3"/>
<item android:title="树叶"
android:icon="@drawable/ic_launcher"
android:id="@+id/shuye"/>
<item android:title="递归"
android:icon="@drawable/ic_launcher"
android:id="@+id/digui"/>
<item android:title="铅笔"
android:icon="@drawable/ic_launcher"
android:id="@+id/pencil"/>
<item android:title="橡皮"
android:icon="@drawable/ic_launcher"
android:id="@+id/rubber"/>
<item android:title="喷枪"
android:icon="@drawable/ic_launcher"
android:id="@+id/pengqiang"/>
<item android:title="图片"
android:icon="@drawable/ic_launcher"
android:id="@+id/picture"/>
</menu>
</item>
</menu>
上面是画图板的基本代码,运行后会出现一个登录界面,在帐号和密码中分别输入CY和CY就会跳转到画图界面,画图界面开始默认是画直线,想画其他图形可以点击菜单,可以实现画笔颜色的改变和画笔所画图形的改变,还有橡皮可以用!!!
还有一个是自定义组件
新建一个Myview.java
package com.example.zidingyi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class Myview extends View {
public Bitmap bm = null;
public Canvas acanvas = null;
public Paint paint = null;
public float x2 = 0,y2 = 0, y1 = 0,x1 = 0;
public Myview(Context context) {
super(context);
}
public Myview(Context context, AttributeSet attrs) {
super(context,attrs);
paint = new Paint();
}
public Myview(Context context, AttributeSet attrs ,int defStyle){
super(context,attrs,defStyle);
}
public boolean onTouchEvent( MotionEvent event){
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
acanvas.drawLine(x1,y1,x2,y2,paint);
this.postInvalidate();//刷新,执行onDraw方法
break;
}
return true;
}
//一开始就调用onDraw方法,和java里的paint方法一样
//参数中的canvas是自带的,不需要初始化
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bm == null) {
//根据组件的大小创建一个32位真彩色图像
bm = Bitmap.createBitmap(this.getWidth(), this.getHeight(),
Bitmap.Config.ARGB_8888);
//根据创建的位图图像bm创建画布对象
acanvas = new Canvas(bm);
}
//开始画时将bm画到自己的组件上
canvas.drawBitmap(bm,0,0,paint);
}
}
这是实现组定义组件需要写的基本的东西,需要其他功能可以自己写函数添加进去!!!