java,import android.content.Context;,import android.graphics.Canvas;,import android.graphics.Color;,import android.graphics.Paint;,import android.util.AttributeSet;,import android.view.MotionEvent;,import android.view.View;,,public class SimpleDrawingView extends View {, private Paint paint = new Paint();, private float lastX, lastY;,, public SimpleDrawingView(Context context, AttributeSet attrs) {, super(context, attrs);, paint.setColor(Color.BLACK);, paint.setStrokeWidth(5f);, },, @Override, protected void onDraw(Canvas canvas) {, super.onDraw(canvas);, canvas.drawLine(lastX, lastY, lastX, lastY, paint);, },, @Override, public boolean onTouchEvent(MotionEvent event) {, float x = event.getX();, float y = event.getY();,, switch (event.getAction()) {, case MotionEvent.ACTION_DOWN:, lastX = x;, lastY = y;, break;, case MotionEvent.ACTION_MOVE:, invalidate(); // Redraw the view, lastX = x;, lastY = y;, break;, }, return true;, },},
``,,这个简单的画画板应用通过触摸事件来绘制线条。你可以将这个自定义视图添加到你的布局文件中,或者在Activity中动态添加。Powered By Z-BlogPHP 1.7.3