2012-06-06 4 views
0

프레임 애니메이션에서 스레드를 사용하고 있지만 스레드에서 오류가 발생합니다. 이 오류 다음 날 던지고스레드가 오류를 던지고 있습니다 (프레임 애니메이션)

public class Newton_LawsActivity extends Activity implements OnTouchListener, OnGestureListener,OnDoubleTapListener { 
    /** Called when the activity is first created. */ 
    OnTouchListener l; 
    ImageView animation; 
    ImageView hideimage; 
    TextView t; 
    Thread timer; 
    int Rid; 
    double pixel; 
    String law="null"; 
    private GestureDetector gestureDetector; 
    float x,y; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     gestureDetector = new GestureDetector(this); 
     // the frame-by-frame animation defined as a xml file within the drawable folder 
      animation = (ImageView)findViewById(R.id.image); 
      //animation.setBackgroundResource(R.drawable.anatomy_5); 
      hideimage=(ImageView)findViewById(R.id.imagein); 
      t=(TextView) findViewById(R.id.t); 
      animation.setBackgroundResource(R.anim.startanimation); 
      final AnimationDrawable newtonAnimation = (AnimationDrawable) animation.getBackground(); 

      AnimationStart(newtonAnimation); 

      animation.setOnTouchListener(this);  

     } 
    public void AnimationStart(final AnimationDrawable newanimation){ 
     timer=new Thread(){ 
     @Override 
     public void run(){ 
          try{ 

           }catch(Exception e){} 
           finally{ 
              Newton_LawsActivity.this.runOnUiThread(new Runnable() { 
               public void run(){ 
               newanimation.start(); 
              }}); 
             } 
          } 
         }; 
    timer.start(); 
    } 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "animation", Toast.LENGTH_LONG).show(); 

     return gestureDetector.onTouchEvent(event); 
    } 


    private void animationswitch(float x, float y) { 
     // TODO Auto-generated method stub 
    pixel = getmaskpixel(x,y,animation,hideimage); 
    Log.w("DEBUG","which is null:pixel" + pixel ); 
    if (pixel==-583672){ 
     animation.setBackgroundResource(R.anim.firstlaw_01); 
     final AnimationDrawable firstlaw_01 = (AnimationDrawable) animation.getBackground(); 
     law="firstlaw_02"; 
     firstlaw_01.start(); 
     animationstop_01(); 
    } 
    t.setText(Double.toString(pixel)); 
    } 


private double getmaskpixel(float x, float y, ImageView view,ImageView hideimage) { 
     // TODO Auto-generated method stub 
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.touchnewtonfinal415); 
    int width = bm.getWidth(); 
    int height = bm.getHeight(); 
    float scaleWidth = ((float) view.getWidth())/width; 
    float scaleHeight = ((float) view.getHeight())/height; 

    // create a matrix for the manipulation 
    Matrix matrix = new Matrix(); 
    // resize the bit map 

    matrix.postScale(scaleWidth, scaleHeight); 
    // matrix.postRotate(90); 
    // recreate the new Bitmap 
    Bitmap bitmap = Bitmap.createBitmap(bm, 0, 0, width, height,matrix, false); 
    Log.w("DEBUG","which is null:image " + hideimage.getWidth() + " OR " +hideimage.getHeight()); 
    double bmWidth = view.getWidth(); 
     double bmHeight = view.getHeight(); 
     if (x < 0 || y < 0 || x > hideimage.getWidth() || y >hideimage.getHeight()){ 
      return 0; //Invalid, return 0 
      }else{ 
      //Convert touched x, y on View to on Bitmap 
      int xBm = (int)(x * (bmWidth/hideimage.getWidth())); 
      int yBm = (int)(y * (bmHeight/hideimage.getHeight())); 
      return bitmap.getPixel((int)xBm,(int) yBm); 
    } 

} 

    public boolean onSingleTapUp(MotionEvent e) { 
     return false; 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public boolean onDown(MotionEvent arg0) { 
     // TODO Auto-generated method stub 
     return true; 
    } 
    @Override 
    public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2, 
      float arg3) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
    @Override 
    public void onLongPress(MotionEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, 
      float arg3) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
    @Override 
    public void onShowPress(MotionEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public boolean onDoubleTap(MotionEvent e) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
    @Override 
    public boolean onDoubleTapEvent(MotionEvent e) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
    @Override 
    public boolean onSingleTapConfirmed(MotionEvent e) { 
     // TODO Auto-generated method stub 
     x=e.getX(); 
     y= e.getY(); 
     Log.w("DEBUG","which is null:image " + x + " OR " +y); 
     animationswitch(x, y); 
     return true; 
    } 

    void animationstop_01(){ 
       if(law=="firstlaw_02"){ 
        timer=new Thread(){ 
          @Override 
          public void run(){ 
               try{ 
                 Thread.sleep(8750); 
                }catch(Exception e){} 
                finally{ 
                 Log.w("debug","it is firstlaw_02"); 
                 animation.setBackgroundResource(R.anim.firstlaw_02); 
                 final AnimationDrawable firstlaw_02 = (AnimationDrawable) animation.getBackground(); 
                 law="firstlaw_03"; 
                 AnimationStart(firstlaw_02); 
                   }}; 


              }; 

         timer.start(); 
         }         

    } 





    } 

내 코드는

06-06 14:15:00.668: E/AndroidRuntime(966): FATAL EXCEPTION: Thread-10 
06-06 14:15:00.668: E/AndroidRuntime(966): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.ViewRoot.checkThread(ViewRoot.java:2802) 
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.ViewRoot.invalidateChild(ViewRoot.java:607) 
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633) 
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505) 
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.View.invalidate(View.java:5139) 
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.View.setBackgroundDrawable(View.java:7486) 
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.View.setBackgroundResource(View.java:7395) 
06-06 14:15:00.668: E/AndroidRuntime(966): at Newtons.law.Newton_LawsActivity$2.run(Newton_LawsActivity.java:183) 
+0

실행하면'AnimationStart' 방법에했던 것처럼 runOnUiThread''에서'animationstop_01' 방법의'finally' 블록 내의 코드입니다. – Rajesh

+0

어떤 줄이 Newton_LawsActivity.java:183입니까? – Renard

+0

@Rajesh 당신은 \t \t \t \t \t \t \t \t \t animation.setBackgroundResource – Ashishsingh

답변

1

변경하여 animationstop_01 방법 :

void animationstop_01(){ 
if(law=="firstlaw_02"){ 
    timer=new Thread(){ 
     @Override 
     public void run(){ 
       try{ 
        Thread.sleep(8750); 
       }catch(Exception e){ 
       }finally{ 
        Log.w("debug","it is firstlaw_02"); 
        Newton_LawsActivity.this.runOnUiThread(new Runnable() { 
         public void run(){ 
          animation.setBackgroundResource(R.anim.firstlaw_02); 
          final AnimationDrawable firstlaw_02 = (AnimationDrawable) animation.getBackground(); 
          law="firstlaw_03"; 
          AnimationStart(firstlaw_02); 
         } 
        }); 


       } 
      }; 
     }; 
     timer.start(); 
    }         
} 
+0

+1 ... – Ashishsingh

1

오류 당신이하려고하는 mehod 이름 animationstop_01() 안에 .. .... 다음과 같다

animation.setBackgroundResource(R.anim.firstlaw_02); 

넣어 그 안에 runOnUiThread()

+0

어떻게 설명 할 수 있습니까? – Ashishsingh

+0

당신은 이미'AnimationStart (final AnimationDrawable newanimation)'메소드 –

+0

안에있는 것을 사용했습니다. void animationstop_01() – Ashishsingh

0

이것을 시도하십시오 :

animation.post(new Runnable() { 

     public void run() { 
      animation.setBackgroundResource(R.anim.firstlaw_02); 

     } 
    }); 

그런 식으로 UI 스레드에 게시하고 있습니다. 뷰는 뷰를 생성 한 원래 스레드에서만 변경할 수 있습니다.

관련 문제