布局:
<LinearLayout 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:orientation="vertical" 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="com.example.trackdrawer03.MainActivity" > <!--以上改了linearlayout的orientation--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/clrbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="clear" /> <Button android:id="@+id/savebtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="save" /> </LinearLayout> <ImageView android:id="@+id/iv_canvvas" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
java
package com.example.trackdrawer03; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private Button clrbtn; private ImageView ivcanvas; private Canvas canvas; private Paint paint; private Bitmap baseBitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); clrbtn = (Button) findViewById(R.id.clrbtn); ivcanvas = (ImageView) findViewById(R.id.iv_canvvas); paint = new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth(3); clronClickListner clrListner = new clronClickListner(); clrbtn.setOnClickListener(clrListner); ivcanvas.setOnTouchListener(touch); } private View.OnTouchListener touch = new OnTouchListener() { // 定义手指开始触摸的坐标 float startX; float startY; @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { // 用户按下动作 case MotionEvent.ACTION_DOWN: // 第一次绘画初始化内存图片,指定背景为白色 if (baseBitmap == null) { baseBitmap = Bitmap.createBitmap(ivcanvas.getWidth(), ivcanvas.getHeight(), Bitmap.Config.ARGB_8888); canvas = new Canvas(baseBitmap); canvas.drawColor(Color.WHITE); } // 记录开始接触的点的坐标 startX = event.getX(); startY = event.getY(); break; // 当用户手指在屏幕上移动的动作 case MotionEvent.ACTION_MOVE: float stopX = event.getX(); float stopY = event.getY(); // 绘制 canvas.drawLine(startX, startY, stopX, stopY, paint); // 更新 startX = event.getX(); startY = event.getY(); // 把图片展示到imageView中 ivcanvas.setImageBitmap(baseBitmap); break; case MotionEvent.ACTION_UP: break; default: break; } return true; } }; //clear canvas public void clearCanvas(){ if(baseBitmap!=null){ baseBitmap = Bitmap.createBitmap(ivcanvas.getWidth(), ivcanvas.getHeight(),Bitmap.Config.ARGB_8888); canvas = new Canvas(baseBitmap); canvas.drawColor(Color.WHITE); ivcanvas.setImageBitmap(baseBitmap); Toast.makeText(getApplicationContext(), "clear successfully", 3000).show(); } } public class clronClickListner implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub clearCanvas(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
转载于:https://my.oschina.net/LaVictoria/blog/535014