2012-10-23 57 views
1

我的代码:如何在布局中保持画布?

public class MainActivity extends GraphicsActivity{  

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(new MyView(this)); 

     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setDither(true); 
     mPaint.setColor(0xffffffff); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(12); 

     mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 
             0.4f, 6, 3.5f); 
     mPaint.setMaskFilter(mEmboss); 

     //mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL); 
    } 

    private Paint  mPaint; 
    private MaskFilter mEmboss; 

    public class MyView extends View { 

     private Bitmap mBitmap; 
     private Canvas mCanvas; 
     private Path mPath; 
     private Paint mBitmapPaint; 

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

      Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.t01); 
      Display display = getWindowManager().getDefaultDisplay(); 
      int height = display.getWidth(); 
      int width = display.getHeight(); 
      mBitmap = Bitmap.createScaledBitmap(bmp, height, width, true); 
      //mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888); 
      mCanvas = new Canvas(mBitmap); 
      mPath = new Path(); 
      mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 

      canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 

      canvas.drawPath(mPath, mPaint); 
     } 

     private float mX, mY; 
     private static final float TOUCH_TOLERANCE = 4; 

     private void touch_start(float x, float y) { 
      mPath.reset(); 
      mPath.moveTo(x, y); 
      mX = x; 
      mY = y; 
     } 
     private void touch_move(float x, float y) { 
      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; 
      } 
     } 
     private void touch_up() { 
      mPath.lineTo(mX, mY); 
      // commit the path to our offscreen 
      mCanvas.drawPath(mPath, mPaint); 
      // kill this so we don't double draw 
      mPath.reset(); 
     } 

     @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(); 
        break; 
       case MotionEvent.ACTION_MOVE: 
        touch_move(x, y); 
        invalidate(); 
        break; 
       case MotionEvent.ACTION_UP: 
        touch_up(); 
        invalidate(); 
        break; 
      } 
      return true; 
     } 
    } 

} 

我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:src="@drawable/t01" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" /> 

</RelativeLayout> 

现在我想在活动中添加视图,以这种XML和我也希望把这种观点在布局,使得图像必须被看见并且也是看法。其实我试图在图像中的字母的行中绘制。像跟踪信件。 而且我也不希望我的画布绘制在图像中排除我的Letter的轮廓。如果它交叉,那么我想要发出一些警告声。 enter image description here 请问我该怎么做? 在此先感谢。

回答

1

当我尝试MarvinLabs给出的答案时,我得到膨胀异常。所以试图用java本身编写代码。我的代码解决了我的问题:

public class MainActivity extends GraphicsActivity{ 

    private Button clearButton; 
    MyView signature; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     signature = new MyView(this); 

     RelativeLayout myLayout = new RelativeLayout(this); 
     myLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 

     clearButton = new Button(this); 
     clearButton.setText("Clear"); 
     clearButton.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // To clear the drawn lines with finger. 
       Intent intent = new Intent(MainActivity.this, MainActivity.class); 
      startActivity(intent); 
      finish(); 
      } 
     }); 

     myLayout.addView(signature); 
     myLayout.addView(clearButton); 

     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT); 
     params.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
     params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
     clearButton.setLayoutParams(params); 

     clearButton.bringToFront(); 
     this.setContentView(myLayout); 

     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setDither(true); 
     mPaint.setColor(0xffffffff); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(12); 

     mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 
             0.4f, 6, 3.5f); 
     mPaint.setMaskFilter(mEmboss); 

    } 

    private Paint  mPaint; 
    private MaskFilter mEmboss; 


    public class MyView extends View { 

     private static final float MINP = 0.25f; 
     private static final float MAXP = 0.75f; 

     private Bitmap mBitmap; 
     private Canvas mCanvas; 
     private Path mPath; 
     private Paint mBitmapPaint; 

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

      Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.t01); 
      Display display = getWindowManager().getDefaultDisplay(); 
      int height = display.getWidth(); 
      int width = display.getHeight(); 
      mBitmap = Bitmap.createScaledBitmap(bmp, height, width, true); 
      mCanvas = new Canvas(mBitmap); 
      mPath = new Path(); 
      mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
     } 


     @Override 
     protected void onDraw(Canvas canvas) { 

      canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 

      canvas.drawPath(mPath, mPaint); 
     } 

     private float mX, mY; 
     private static final float TOUCH_TOLERANCE = 4; 

     private void touch_start(float x, float y) { 
      mPath.reset(); 
      mPath.moveTo(x, y); 
      mX = x; 
      mY = y; 
     } 
     private void touch_move(float x, float y) { 
      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; 
      } 
     } 
     private void touch_up() { 
      mPath.lineTo(mX, mY); 
      // commit the path to our offscreen 
      mCanvas.drawPath(mPath, mPaint); 
      // kill this so we don't double draw 
      mPath.reset(); 
     } 

     @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(); 
        break; 
       case MotionEvent.ACTION_MOVE: 
        touch_move(x, y); 
        invalidate(); 
        break; 
       case MotionEvent.ACTION_UP: 
        touch_up(); 
        invalidate(); 
        break; 
      } 
      return true; 
     } 
    } 
} 

感谢您的支持和帮助。

+0

添加视图时,添加视图的顺序很重要。 –

0

我想你需要

void onMeasure(int widthScrean, int heightScrean) 

这可以设置您绘制的大小,搜索一些例子,有很多帖子在这里SO。

+0

ei marzel:--- D – Sipty

0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:src="@drawable/t01" /> 

    <view 
     class="com.mypackage.MainActivity$MyView" 
     id="@+id/my_view" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</RelativeLayout> 

将com.mypack替换为您的实际活动包。

关于声音,请覆盖您的视图的onTouch方法,并查看是否有任何ACTION_OUTSIDE事件。

+0

实际上,我希望我的视图能够覆盖imageview,并且视图不能隐藏背景中的图像。 –

+0

由于你的'canvas.drawColor(0xFFAAAAAA);'你的视图隐藏了图像。你现在应该拥有自己需要的全部工作。这是我对答案和最后评论的最后编辑。为下一个问题展示一些工作。 –

+0

是的,我会尽我所能。感谢您的建议和答案。 –