最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Android学习5——涂鸦

IT培训 admin 10浏览 0评论

Android学习5——涂鸦

涂鸦

  • 记录
  • 源码
  • 运行结果

记录

  • 两次点击的间隔时间过短会发生“连笔”
  • 程序闪退的原因:往往是代码错了!
    boolean isClear = false;
    运行时一直闪退就是因为这个设置成了true,使得画板初始化时错误调用了值为null的new2_Bitmap,让程序初始化失败。

源码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><com.example.ex4_5.HandWriteandroid:id="@+id/handWriteView"android:layout_width="fill_parent"android:layout_height="380dp" /><LinearLayoutandroid:orientation="horizontal"android:gravity="center_horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/clear"android:text="清屏"android:layout_width="200dp"android:layout_height="wrap_content" /></LinearLayout>
</LinearLayout>

MainActivity.java

package com.example.ex4_5;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends Activity {private HandWrite handWrite = null;private Button button = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);handWrite = (HandWrite)findViewById(R.id.handWriteView);//关联View组件button = (Button)findViewById(R.id.clear);button.setOnClickListener(new mClick());}/*点击事件*/private class mClick implements View.OnClickListener{@Overridepublic void onClick(View v) {handWrite.clear();//清屏}}
}

HandWrite,java

package com.example.ex4_5;import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;public class HandWrite extends View {/*定义类变量*/Paint paint = null; //画笔/*Bitmap */Bitmap originalBitmap = null; //存放原始图像Bitmap new1_Bitmap = null; //存放从原始图像复制的位图图像Bitmap new2_Bitmap = null; //存放处理后的图像float startX = 0,startY = 0; //画线的起点坐标float clickX = 0,clickY = 0; //画线的终点坐标boolean isMove = true; //设置是否画线的标记boolean isClear = false; //设置是否清除涂鸦的标记  /*之前一开始一直闪退就是因为这个设置成了true*/int color = Color.GREEN; //设置画笔的颜色(绿色)float strokeWidth = 2.0f; //设置画笔的宽度/*带参数的构造函数*/public HandWrite(Context context, AttributeSet attrs){super(context,attrs);/*从资源中获取原始图像*/originalBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.cy).copy(Bitmap.Config.ARGB_8888,true);System.out.print(originalBitmap.toString());/*建立原始图像的位图*/new1_Bitmap = Bitmap.createBitmap(originalBitmap);}/*清除涂鸦*/public void clear(){isClear = true;new2_Bitmap = Bitmap.createBitmap(originalBitmap);invalidate();}/*设置画笔的宽度*/public void setstyle(float strokeWidth){this.strokeWidth = strokeWidth;}/*显示绘图*/@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawBitmap(HandWriting(new1_Bitmap),0,0,null);}/*记录绘制图形*/public Bitmap HandWriting(Bitmap o_Bitmap){/*定义画布*/Canvas canvas = null;/*创建绘制新图形的画布*/if (isClear){canvas = new Canvas(new2_Bitmap); /*这句出了问题*/}else {canvas = new Canvas(o_Bitmap);/*创建绘制原图形的画布*/}/*定义画笔*/paint = new Paint();paint.setStyle(Paint.Style.STROKE);paint.setAntiAlias(true);paint.setColor(color);paint.setStrokeWidth(strokeWidth);if (isMove){canvas.drawLine(startX,startY,clickX,clickY,paint); //在画布上画线条}startX = clickX;startY = clickY;if (isClear){return new2_Bitmap;}elsereturn o_Bitmap;}/*定义触摸屏事件*/@Overridepublic boolean onTouchEvent(MotionEvent event) {/*获取触摸坐标位置*/clickX = event.getX();clickY = event.getY();if (event.getAction() == MotionEvent.ACTION_DOWN){/*按下屏幕时无绘图*/isMove = false;invalidate();return true;}else if (event.getAction() == MotionEvent.ACTION_MOVE){/*记录在屏幕上滑动的轨迹*/isMove = true;invalidate();return true;}return super.onTouchEvent(event);}
}

运行结果


Android学习5——涂鸦

涂鸦

  • 记录
  • 源码
  • 运行结果

记录

  • 两次点击的间隔时间过短会发生“连笔”
  • 程序闪退的原因:往往是代码错了!
    boolean isClear = false;
    运行时一直闪退就是因为这个设置成了true,使得画板初始化时错误调用了值为null的new2_Bitmap,让程序初始化失败。

源码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><com.example.ex4_5.HandWriteandroid:id="@+id/handWriteView"android:layout_width="fill_parent"android:layout_height="380dp" /><LinearLayoutandroid:orientation="horizontal"android:gravity="center_horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/clear"android:text="清屏"android:layout_width="200dp"android:layout_height="wrap_content" /></LinearLayout>
</LinearLayout>

MainActivity.java

package com.example.ex4_5;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends Activity {private HandWrite handWrite = null;private Button button = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);handWrite = (HandWrite)findViewById(R.id.handWriteView);//关联View组件button = (Button)findViewById(R.id.clear);button.setOnClickListener(new mClick());}/*点击事件*/private class mClick implements View.OnClickListener{@Overridepublic void onClick(View v) {handWrite.clear();//清屏}}
}

HandWrite,java

package com.example.ex4_5;import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;public class HandWrite extends View {/*定义类变量*/Paint paint = null; //画笔/*Bitmap */Bitmap originalBitmap = null; //存放原始图像Bitmap new1_Bitmap = null; //存放从原始图像复制的位图图像Bitmap new2_Bitmap = null; //存放处理后的图像float startX = 0,startY = 0; //画线的起点坐标float clickX = 0,clickY = 0; //画线的终点坐标boolean isMove = true; //设置是否画线的标记boolean isClear = false; //设置是否清除涂鸦的标记  /*之前一开始一直闪退就是因为这个设置成了true*/int color = Color.GREEN; //设置画笔的颜色(绿色)float strokeWidth = 2.0f; //设置画笔的宽度/*带参数的构造函数*/public HandWrite(Context context, AttributeSet attrs){super(context,attrs);/*从资源中获取原始图像*/originalBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.cy).copy(Bitmap.Config.ARGB_8888,true);System.out.print(originalBitmap.toString());/*建立原始图像的位图*/new1_Bitmap = Bitmap.createBitmap(originalBitmap);}/*清除涂鸦*/public void clear(){isClear = true;new2_Bitmap = Bitmap.createBitmap(originalBitmap);invalidate();}/*设置画笔的宽度*/public void setstyle(float strokeWidth){this.strokeWidth = strokeWidth;}/*显示绘图*/@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawBitmap(HandWriting(new1_Bitmap),0,0,null);}/*记录绘制图形*/public Bitmap HandWriting(Bitmap o_Bitmap){/*定义画布*/Canvas canvas = null;/*创建绘制新图形的画布*/if (isClear){canvas = new Canvas(new2_Bitmap); /*这句出了问题*/}else {canvas = new Canvas(o_Bitmap);/*创建绘制原图形的画布*/}/*定义画笔*/paint = new Paint();paint.setStyle(Paint.Style.STROKE);paint.setAntiAlias(true);paint.setColor(color);paint.setStrokeWidth(strokeWidth);if (isMove){canvas.drawLine(startX,startY,clickX,clickY,paint); //在画布上画线条}startX = clickX;startY = clickY;if (isClear){return new2_Bitmap;}elsereturn o_Bitmap;}/*定义触摸屏事件*/@Overridepublic boolean onTouchEvent(MotionEvent event) {/*获取触摸坐标位置*/clickX = event.getX();clickY = event.getY();if (event.getAction() == MotionEvent.ACTION_DOWN){/*按下屏幕时无绘图*/isMove = false;invalidate();return true;}else if (event.getAction() == MotionEvent.ACTION_MOVE){/*记录在屏幕上滑动的轨迹*/isMove = true;invalidate();return true;}return super.onTouchEvent(event);}
}

运行结果


与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论