转:http://www.eoeandroid.com/thread-114497-1-1.html
画直线效果图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/length_btn" android:layout_height="wrap_content" android:text="画直线" android:layout_width="wrap_content" /> <TextView android:text="画图区" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <com.android.draw.MyDrawView android:id="@+id/drawView" android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"> </com.android.draw.MyDrawView> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/clear_btn" android:text="显示" android:layout_height="wrap_content" android:layout_width="wrap_content" /> <TextView android:text="显示画图区" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" /> </LinearLayout>
import android.app.Activity;
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.ImageView; public class DrawActivity extends Activity { static boolean drawFlag = false; private Button showBtn; private Button lineBtn; private ImageView image; private MyDrawView myDrawView; private Bitmap bimap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); showBtn = (Button) findViewById(R.id.clear_btn); lineBtn = (Button) findViewById(R.id.length_btn); lineBtn.setOnClickListener(new MyClickEvent()); showBtn.setOnClickListener(new MyClickEvent()); image = (ImageView) findViewById(R.id.imageView1); myDrawView = (MyDrawView) findViewById(R.id.drawView); // 如果不用copy的方法,直接引用会对资源文件进行修改,而android是不允许在代码里修改res文件里的图片 bimap = BitmapFactory.decodeResource(getResources(), R.drawable.background).copy(Bitmap.Config.ARGB_8888, true); } class MyClickEvent implements View.OnClickListener { public void onClick(View v) { if (v == lineBtn) { // 传我们需要处理的bimap给画图类 myDrawView.setBitmap(bimap); // 开始画图的标志 DrawActivity.drawFlag = true; } if (v == showBtn) { // 显示图片 image.setImageBitmap(bimap); } } } }
import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ImageView; public class MyDrawView extends ImageView { private float beginX = 0, beginY = 0, endX = 0, endY = 0; private int eventFlag = 0;// 触屏事件点击 private Canvas canvasSelf;// 用于保存所画图像的画布 private Bitmap bimap;// 用于保存所画图像的图画 public void setBitmap(Bitmap b) { bimap = b; canvasSelf = new Canvas(bimap); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (DrawActivity.drawFlag) { Paint paint = new Paint(); /** 设置画笔 */ paint.setColor(Color.WHITE); paint.setStyle(Paint.Style.STROKE); paint.setAntiAlias(true); // 把之前画出来保存后的图再画出来 canvas.drawBitmap(bimap, 0, 0, null); // 画中间长得线段 canvas.drawLine(beginX, beginY, endX, endY, paint); if (eventFlag == MotionEvent.ACTION_UP) { // 这里将直线画到canvasself上,它就保存在了bimap这张图上 canvasSelf.drawLine(beginX, beginY, endX, endY, paint); } } } @Override public boolean onTouchEvent(MotionEvent event) { endX = event.getX(); endY = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: eventFlag = MotionEvent.ACTION_DOWN; beginX = endX; beginY = endY; invalidate(); break; case MotionEvent.ACTION_MOVE: eventFlag = MotionEvent.ACTION_MOVE; invalidate(); break; case MotionEvent.ACTION_UP: eventFlag = MotionEvent.ACTION_UP; invalidate(); break; } return true; } /** * 在XML中使用自定义View必须要使用含AttributeSet变量参数的构造函数 * @param context * @param attrs */ public MyDrawView(Context context, AttributeSet attrs) { super(context, attrs); } public MyDrawView(Context context) { super(context); } }