标签:实现 Bitmap paint tView 课本 import android event
一、4-4的实现
(一)课本上的代码MainAcitity类顺序出现了问题,原本是:
tView=(TextView)findViewById(R.id.textview1);
tView.setOnTouchListener(new mOnTouch());
setContentView(R.layout.activity_main);
更改后即可运行,更改后的代码为:
setContentView(R.layout.activity_main);
tView=(TextView)findViewById(R.id.textview1);
tView.setOnTouchListener(new mOnTouch());
(二)以下为源代码
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.zuoye44.TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
2.TextView.java
package com.example.zuoye44;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class TextView extends View {
int x=150,y=150;//定义小球的初始坐标
public TextView(Context context, AttributeSet attrs){
super(context,attrs);
}
void getXY(int _x,int _y){
//由触摸屏事件传递小球坐标位置
x=_x;
y=_y;
}
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawColor(Color.CYAN);//设置背景为青色
Paint paint=new Paint();
paint.setAntiAlias(true);//去锯齿
paint.setColor(Color.BLACK);//设置paint的颜色
canvas.drawCircle(x,y,30,paint);//画一个实心圆
paint.setColor(Color.WHITE);//画实心圆上的小白点
canvas.drawCircle(x-9,y-9,6,paint);
}
}
- MainActivity.java
package com.example.zuoye44;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
public class MainActivity extends AppCompatActivity {
TextView tView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tView=(TextView)findViewById(R.id.textview1);
tView.setOnTouchListener(new mOnTouch());
}
class mOnTouch implements View.OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
int x1,y1;
//获取坐标位置
x1=(int)event.getX();
y1=(int)event.getY();
if (event.getAction()==MotionEvent.ACTION_DOWN){//在屏幕上点击
//按新坐标绘图
tView.getXY(x1,y1);
tView.invalidate();
return true;
}
if (event.getAction()==MotionEvent.ACTION_MOVE)//在屏幕上滑动(拖动)
{ //按新坐标绘图
tView.getXY(x1,y1);
tView.invalidate();
return true;
}
return tView.onTouchEvent(event);
}
}
}
二、4-5的实现
(一)源代码如下:
1.activity_main.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">
<com.example.zuoye45.HandWrite
android:id="@+id/handwriteview"
android:layout_width="fill_parent"
android:layout_height="380dp"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_horizontal">
<Button
android:id="@+id/clear"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/clear"/>
</LinearLayout>
</LinearLayout>
2.HandWrite.java
package com.example.zuoye45;
import android.content.Context;
import android.view.View;
import android.graphics.*;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class HandWrite extends View {
Paint paint=null; //定义画笔
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;//设置是否清楚涂鸦的标记
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);//从资源中获取原始图像
new1_Bitmap=Bitmap.createBitmap(originalBitmap);//建立原始图像的位图
}
public void clear(){
//清除涂鸦
isClear=true;
new2_Bitmap=Bitmap.createBitmap(originalBitmap);
invalidate();
}
public void setstyle(float strokeWidth){
//设置画笔宽度
this.strokeWidth=strokeWidth;
}
@Override
protected 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(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;
}
return o_Bitmap;//若清屏,则返回原图像
}
public 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);
}
}
3.MainActivity.java
package com.example.zuoye45;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private HandWrite handWrite=null;
private Button clear=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handWrite=(HandWrite)findViewById(R.id.handwriteview);//关联view组件
clear=(Button)findViewById(R.id.clear);
clear.setOnClickListener(new mClick());
}
private class mClick implements OnClickListener{
@Override
public void onClick(View v) {
handWrite.clear();
}
}
}
标签:实现,Bitmap,paint,tView,课本,import,android,event 来源: https://blog.csdn.net/perchersummer/article/details/89035145
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。