描画用のAndroidアプリケーションを開発しています。そのため、MVCパターンを使用する必要があります。アプリケーションが描画に使用するViewクラスがあります。
public class PainterView extends View implements DrawingListener {
private Painter painter;
private Bitmap bitmap;
private Paint bitmapPaint;
private Path path;
private Paint paint;
public PainterView(Context context, Painter painter) {
super(context);
this.painter=painter;
this.painter.addDrawingListener(this);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
if (bitmap!=null) {
canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
canvas.drawPath(path, paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
painter.touchStart(x, y);
break;
case MotionEvent.ACTION_MOVE:
painter.touchMove(x, y);
break;
case MotionEvent.ACTION_UP:
painter.touchUp();
break;
}
return true;
}
@Override
public void update(Bitmap bitmap, Paint bitmapPaint, Path path, Paint paint) {
this.bitmap=bitmap;
this.bitmapPaint=bitmapPaint;
this.path=path;
this.paint=paint;
invalidate();
}
}
主な活動:
public class MainScreenActivity extends Activity {
/** Called when the activity is first created. */
private PainterView mMainView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Display display = getWindow().getWindowManager().getDefaultDisplay();
Painter painter=new Painter(display.getWidth(), display.getHeight());
mMainView = new PainterView(this, painter);
setContentView(mMainView);
}
}
そして、すべてのアルゴリズム(モデル)を保持するPainterクラス。注意してください、すべてのアルゴリズムはうまく機能します:
public class Painter {
private List<DrawingListener> mDrawingListeners;
private static final float TOUCH_TOLERANCE = 4;
private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;
public Paint mPaint;
public Bitmap mBitmap;
public Canvas mCanvas;
public Path mPath;
public Paint mBitmapPaint;
private float mX, mY;
public Painter(int width, int height) {
initializeGraphic(width, height);
}
private void initializeGraphic(int width, int height) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mCanvas.drawRect(new Rect(0, 0, width, height), new Paint(Color.BLACK));
}
private void drawingChanged() {
notifyDrawingListeners();
}
public void touchStart(float x, float y) {
Log.e("event", "start");
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
drawingChanged();
}
public void touchMove(float x, float y) {
Log.e("event", "move");
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
drawingChanged();
}
public void touchUp() {
Log.e("event", "up");
mPath.lineTo(mX, mY);
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
drawingChanged();
}
public void addDrawingListener(DrawingListener listener) {
if (mDrawingListeners==null) {
mDrawingListeners=new ArrayList<DrawingListener>();
}
mDrawingListeners.add(listener);
}
public void removeDrawingListener(DrawingListener listener) {
mDrawingListeners.remove(listener);
}
public void notifyDrawingListeners() {
for (DrawingListener listener:mDrawingListeners) {
listener.update(mBitmap, mBitmapPaint, mPath, mPaint);
}
}
}
しかし、私にはいくつかの問題があります。画面でタッチして描画すると機能しますが、画面から指を上げると、画面が再び真っ暗になります。つまり、アプリケーションは描画の結果を保存しません。したがって、この行をonDrawメソッドに追加すると、次のようになります。
@Override
protected void onDraw(Canvas canvas) {
if (bitmap!=null) {
canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
canvas.drawPath(path, paint);
painter.mBitmap=bitmap;
painter.mBitmapPaint=bitmapPaint;
painter.mPaint=paint;
painter.mPath=path;
}
}
その後、それは動作します!でも理由がわかりません!アプリケーションは同じリンクを使用しますが、描画後に再度変更する必要があるのはなぜですか?ありがとうございました。