2012-12-13 3 views
9

내 자신의보기 (R.id.view)에서 페인트하려고하지만이 코드는 효과가없는 것 같습니다. 내가 아무것도 그릴 수는 없다.안드로이드 -보기에서 그릴 방법

public class MainActivity extends Activity implements OnTouchListener 
     { 
Path mPath; 
Canvas canvas; 
Paint mPaint; 
MaskFilter mEmboss; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     View view=(View)findViewById(R.id.view1); 
     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); 

     canvas = null; 
     //view.draw(canvas); 
     mPath = new Path(); 
     Bitmap mBitmap; 
     //Paint mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
     mBitmap = Bitmap.createBitmap(210, 170, Bitmap.Config.ARGB_8888); 
     canvas = new Canvas(mBitmap); 
     canvas.drawColor(0xFFAAAAAA); 

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


     //canvas.drawPaint(mPaint); 
     view.draw(canvas); 
     canvas.drawPath(mPath, mPaint); 


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



     view.setOnTouchListener(new OnTouchListener() { 

      public boolean onTouch(View v, MotionEvent event) { 
       float x = event.getX(); 
       float y = event.getY(); 
       Log.d("x", String.valueOf(x)); 
       Log.d("y", String.valueOf(y)); 

       switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
         Log.d("a","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; 
      } 
     }); 

     // mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL); 
    } 
    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 
     canvas.drawPath(mPath, mPaint); 
     // kill this so we don't double draw 
     mPath.reset(); 
    } 





} 

자유형 핑거 페인트를 내 자신의보기로 사용하려면 어떻게해야합니까?

+0

** 내 대답 참조 [여기를 클릭하십시오] (http://stackoverflow.com/a/41135685/4629101) ** –

답변

6

확장 된보기를 만든 클래스를 콘텐츠보기로 설정하는 것이 가장 좋을 것입니다. 또한 터치 이벤트 중에 Action_Move 매개 변수가 많이 호출되므로이를 고려하는 것이 가장 좋을 수 있습니다. 그렇지 않으면 곡선을 원할 때 직선을 얻게됩니다.

편집 :
당신은 당신의 MYVIEW 클래스의 내부
View view = new MyView();
setContentView(view);

을 변경하여에서 onCreate에서 시작 것입니다 당신이 '경로'객체의 ArrayList에있을 것입니다, 매번 Action_UP 불려갑니다 arrayList에 경로를 추가하고 view.Invalidate();을 사용하면 클래스가 뷰 클래스에서 onDraw 메서드를 호출합니다. 그래서 당신은 그것을 그렇게 무시합니다.

@Override public void onDraw(Canvas c){
for(Path p: listOfPaths){
c.drawPath(p, paint);}
}

참고 :이 많은 선이 한 번 있기 때문에이 방법 태블릿 또는 휴대 전화에서 모나리자를 그릴 기대하지 않는다, 간단한 그림에 대 한 좋은 것 당신은 지연을 알 수 있습니다. 좀 더 복잡하고 전문적 이길 원한다면. view.Invalidate(Rect r) 접근법을 확인하십시오. 전체보기는 무효화되지만 방금 그린 부분은 무효화됩니다.

+0

뷰를 확장하는 모든 클래스에는 onDraw 메서드가 있습니다. Action_Up 중에 invalidate를 호출하면 onDraw 메서드가 호출됩니다. 업데이트 된보기를 다시 그립니다. 희망이 도움이되었습니다. – dannyRods

+0

자세한 정보를 얻을 수 있습니까? 코드 내에서 어떻게이 작업을 수행합니까? –

+1

희망을 되찾은 희망 – dannyRods

관련 문제