0

下面是我的安卓新画家应用程序。

但是,它不反映先前绘制的对象。(当我触摸屏幕时,它失去了形状)

为了反映以前绘制的对象,我尝试使用“Bitmap.createBitmap”方法,但它不起作用。

请帮我。


public class CreativePainterActivity extends Activity {

//
 //private MyView vw;
Paint mPaint;

//--Variables to store the current figure info
private float _currentStartX;       //where mouse first pressed
private float _currentStartY;
private float _currentEndX;         //where dragged to or released
private float _currentEndY;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
setContentView(new MyView(this));
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFFFF00);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(8);
}
//view class
public class MyView extends View{

private Canvas mCanvas;
private Bitmap mBitmap;
private Paint mBitmapPaint;
Bitmap bm;

//private Paint mBitmapPaint;

public MyView(Context context){
super(context);


//ADDED
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

mBitmap = Bitmap.createBitmap(metrics.widthPixels, metrics.heightPixels, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mCanvas.drawColor(0xFFFFFFFF);
bm = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
}


public void onDraw(Canvas canvas){
//canvas.drawColor(Color.LTGRAY);
canvas.drawBitmap(bm, 0, 0, mBitmapPaint);
canvas.drawLine(_currentStartX, _currentStartY, _currentEndX, _currentEndY, mPaint);
}

//Methods for touch events
public boolean onTouchEvent(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN){
_currentStartX=event.getX();
_currentStartY=event.getY();
return true;
}

if(event.getAction() == MotionEvent.ACTION_MOVE){
_currentEndX=event.getX();
_currentEndY=event.getY();
invalidate();
return true;
}
return true;
}

}//end of the class MyView

}//end of the class CreativePainterActivity
4

1 回答 1

3

您将需要更多地了解如何绘制视图。再次绘制后,您的视图将不会保留其中的任何内容。因此,您应该以某种方式保存之前的所有内容,并重新绘制旧的内容以及新的更改。

一个更简单的解决方案是将以前的绘图保存到位图,然后先在画布上再次绘制该位图,然后添加新的东西。

流量

onDraw(){
    绘制位图(bmp);
    绘制其他东西();
    bmp = saveOnScreenBitmap();
}

所以,每次都需要保存上一次绘制的位图,然后重新绘制。希望现在更清楚了。

一些示例教程:http: //developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/TouchPaint.html

http://www.tutorialforandroid.com/2009/06/drawing-with-canvas-in-android.html

于 2012-05-15T03:39:22.313 回答