2012-11-24 11 views
1

사용자가 화면에서 도형을 드래그 할 수있는 앱이 있습니다. 드래그를 위해 사용자 지정 표면 뷰를 사용합니다. 표면 뷰는 OnTouchEvent를 얻을 때마다 그래픽 이미지의 위치를 ​​변경하고 OnDraw를 호출하여 화면을 업데이트합니다.무효가 화면을 업데이트하지 않습니다.

그래서 touchevent가 발생할 때까지 비트 맵 이미지는 그려지지 않습니다. 나는 Costume 표면보기를 무효화하려고 시도했지만, 디버거에 따르면 OnDraw는 결코 호출되지 않습니다.

몇 가지 중 하나가 화면에게 닿을 때까지 그래서 지금 내 화면이 비어

// 코드

public class cPlay extends cBase implements OnClickListener { 
    /** Called when the activity is first created. */ 
    // sound 
    private SoundPool soundPool; 
    private int soundID; 
    boolean loaded = false; 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
     //  setContentView(R.layout.play); 


      int w=getWindowManager().getDefaultDisplay().getWidth(); 
      int h=getWindowManager().getDefaultDisplay().getHeight(); 

      BallView ballView=new BallView(this,w,h); 
      setContentView(ballView); 

      // Does not update screen?????? 
      ballView.invalidate(); 
       .... 
... 
....} // end of class 

public class BallView extends SurfaceView implements SurfaceHolder.Callback { 
    private Bitmap bitmap ; 
    private int x=20,y=20;int width,height; 
    cShapeParent MyShapes; 
    cTarget MyTargetShapes; 
    Context context; 
    int counter=0; 

    // sound 
    private SoundPool soundPool; 
    private int soundID; 
    boolean loaded = false; 

    public BallView(Context InContext,int w,int h) { 
     super(InContext); 

     context=InContext; 
     int counter=0; 
     int Page=0; 
     width=w; 
     height=h; 
     getHolder().addCallback(this); 
     setFocusable(true); 
     MyShapes=new cShapeParent(context,w,h); 
     MyTargetShapes= new cTarget(context,w,h); 
     MyShapes.SetTargets( MyTargetShapes); 
     // load in bitmaps 



    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 




     bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.ball_green); 
     canvas.drawColor(Color.BLUE);//To make background 
    //  canvas.drawBitmap(bitmap,x-(bitmap.getWidth()/2),y-(bitmap.getHeight()/2),null); 

     // See if we shoyld drag it 
     for(int i=MyShapes.Amount-1; i>=0; i--) 
     { 
      if (MyShapes.Intersect(i,x,y))   
      { 
      // see if we can drag it 
       if (MyShapes.getDragFlag(i)==false) 
       break; 

       // yes drag this image 
       MyShapes.SetCenter(i, x, y); 

       // if the target was found 
       // make it so we can no loner drag it 
       if (MyShapes.CheckTarget(i)) 
       { 
        MyShapes.SetDrag(i,false); 
        MyShapes.SetToTarget(i); 
        if (++counter>2) 
         DisplayNextScreen(); 
       } 
       break; 
      } 
     }  

     // Draw target shapes 
     // NOTE: draw these first so they are on the bottom 
     for(int i=0; i<MyShapes.Amount;i++) 
     { 
      int x=MyTargetShapes.GetX(i); 
      int y=MyTargetShapes.GetY(i); 
      int w=MyTargetShapes.GetW(i); 
      int h=MyTargetShapes.GetH(i); 

      Rect br=new Rect(x, y,w+x, h+y); 
      Bitmap m= MyTargetShapes.MyImages[i]; 
      canvas.drawBitmap(m, null, br, null); 
     }  

     // Draw shapes that can be dragged 
     for(int i=0; i<MyShapes.Amount;i++) 
     { 
      int x=MyShapes.GetX(i); 
      int y=MyShapes.GetY(i); 
      int w=MyShapes.GetW(i); 
      int h=MyShapes.GetH(i); 
      Rect br=new Rect(x, y, x+w,y+h); 
      Bitmap m= MyShapes.MyImages[i]; 
      canvas.drawBitmap(m, null, br, null); 
     } 




    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     x=(int)event.getX(); 
     y=(int)event.getY(); 

     if(x<25) 
      x=25; 
     if(x> width) 
      x=width; 
     if(y <25) 
      y=25; 
     if(y > height) 
      y=height; 

     updateBall(); 

     return true; 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
    } 

    private void updateBall() { 
     Canvas canvas = null; 
     try { 
      canvas = getHolder().lockCanvas(null); 
      synchronized (getHolder()) { 
       this.onDraw(canvas); 
      } 
     } 
     finally { 
      if (canvas != null) { 
       getHolder().unlockCanvasAndPost(canvas); 
      } 
     } 
    } 

    void DisplayNextScreen() 
    { 
      ///////////////////////////////////// 
     // call up dialog 
     final Dialog dialog = new Dialog(context); 
     dialog.setContentView(R.layout.custom); 
     dialog.setTitle("Title..."); 

     // set the custom dialog components - text, image and button 
     TextView text = (TextView) dialog.findViewById(R.id.text); 
     text.setText("Android custom dialog example!"); 
     ImageView image = (ImageView) dialog.findViewById(R.id.image); 
     image.setImageResource(R.drawable.ic_launcher); 

     Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); 
     // if button is clicked, close the custom dialog 

     dialogButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dialog.dismiss(); 
       MyTargetShapes.SetUp(1); 
       MyShapes.SetUp(1); 
       MyShapes.SetTargets( MyTargetShapes); 
       counter =0; 
      } 
     }); 

     dialog.show(); 
    } // end methed 


    void StartSound(int soundID) 
    { 
     AudioManager audioManager = (AudioManager) context.getSystemService("audio"); 
     float actualVolume = (float) audioManager 
       .getStreamVolume(AudioManager.STREAM_MUSIC); 
     float maxVolume = (float) audioManager 
       .getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
     float volume = actualVolume/maxVolume; 
     // Is the sound loaded already? 

      soundPool.play(soundID, volume, volume, 1, 0, 1f); 
     // Log.e("Test", "Playe"); 

    } 

} // end class 

답변

0

내 응용 프로그램에서 비슷한 문제가 발생했습니다. invalidate()는 도형을 다시 그리지 않습니다. 그래서 내가 찾은 해결책은 UI의 컴포넌트를 표시하는 클래스에 모양을 추출한 다음 UI 클래스에서 요소에 액세스하여 주요 활동의 구성 요소를 업데이트하는 것이 었습니다.

서피스 뷰 SurfaceView 클래스 :

@Override 공공 무효의 onDraw (캔버스 캔버스) {

Log.d("check","="+color); 
    Log.e("check","after set:"+buf); 
    Log.i("new view ", " on draw "); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeWidth(5); 
    paint.setColor(Color.WHITE); 

    path = new Path(); 
    path.moveTo(0, 30);        /*For Borders*/ 
    path.lineTo(30, 0); 
    path.lineTo(-30, 0); 
    path.close(); 
    path.offset(20, -5); 
    canvas.drawPath(path, paint); 

UI 클래스 :

공공 무효 쇼 (보기 앵커, INT 색상) { preShow ();

int xPos, yPos, arrowPos; 

    mDidAction = false; 
    } 

    showArrow(((onTop) ? R.id.arrow_down : R.id.arrow_up), arrowPos,color); 

    setAnimationStyle(screenWidth, anchorRect.centerX(), onTop); 
    mWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos); 


} 

R.id.arrow_down

NewView은 클래스를 사용하여 그려진 도형이다. 행사의 주요 활동에

은 다음과 같이 함수를 호출 : 필요에 따라

btn1 = (Button) this.findViewById(R.id.btn1); 
    btn1.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 


      Log.d("In","View"); 
      quickAction.show(v,Color.BLUE); 


     } 
    }); 

이보기를 업데이트합니다. 희망이 도움이되었습니다

0

대신에서 그것을 호출하는 BallView(Context InContext,int w,int h) 생성자의 마지막에 invalidate(); 호출을 추가하려고 할 수 있습니다 다른 클래스.

관련 문제