0

即使我的程序多次绘制包含在同一块中的线条,我也不能在 android 中多次绘制位图。这是我的代码:

Bitmap cache,backup;
cache = Bitmap.createBitmap(480,800,paint);

switch(event.getAction()){

case MotionEvent.ACTION_DOWN:

backup = Bitmap.createBitmap(cache);
return true;

case MotionEvent.ACTION_MOVE:
canvas.drawLine(downX,downY,moveX,moveY,paint);
canvas.drawBitmap(backup,0,0,paint);

return true;

}
4

1 回答 1

0

在你的活动课上

public class MyActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState); 
MyView mv= new MyView(this);
setContentView(mv);
}

}

 public class MyView extends View {


 private Bitmap  mBitmap;
 private Canvas  mCanvas;
  private Paint   mBitmapPaint;
 private Paint   mBitmapPaint;
 Context mcontext;

 public MyView(Context c) {
    super(c);
    mcontext=c;
    mBitmapPaint = new Paint();
    mBitmapPaint.setColor(Color.RED);//paint color set to red
}
// is called when your view is first assigned a size, and again if the size of your view changes for any reason.
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
}
//called everytime view is refreshed.
@Override
protected void onDraw(Canvas canvas) {
   //do your draw here. draw lines and bitmaps.

            Display display = ( (Activity) mcontext).getWindowManager().getDefaultDisplay();  
        float w = display.getWidth(); 
        float h = display.getHeight();
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            //draw lines as border for the screen 
            canvas.drawLine(0, 0, w, 0,mBitmapPaint);
        canvas.drawLine(0, 0, 0, h,mBitmapPaint);
        canvas.drawLine(w,h,w,0,mBitmapPaint);
        canvas.drawLine(w, h, 0,h , mBitmapPaint);
}
  @Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();//refresh 
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();//refresh 
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();//refresh view
            break;
    }
    return true;
}
   private void touch_start(float x, float y) {
   //do want should be done on touch start
}
private void touch_move(float x, float y) {
  //do want should be done on touch move
}
private void touch_up() {
    //do want should be done on touch up
}
} 

根据您的需要修改上述内容。更多信息可在 androdi 开发人员站点中找到。http://developer.android.com/training/custom-views/custom-drawing.html

于 2013-03-14T06:18:34.440 回答