2013-06-11 4 views
0

가장 관련성 높은 코드가 아래에 나타나는 http://www.passsy.de/multitouch-for-all-views/#comment-47486을 참조하십시오.다른 단추 내에서 단추 보기를 애니메이션화하는 방법 ontouch ImageButton보기?

버튼을 확장하는 하나의 "TestButton"이 눌러지면, 해당 뷰는 "TestButton"코드로 전달되고 해당 버튼/뷰에 애니메이션을 적용 할 수 있습니다. 그러나 나는 다른 뷰를 움직이기 원한다. 어떻게 작성합니까? 여기에서 작성한 뷰와 다른 뷰를 애니메이트 할 수 있습니까? 또는 어떻게하면 활동을 DO 액션으로 알릴 수 있습니까?

startAnimation(animationstd); 

을하지만 다른 버튼 : 버튼 작동이 감동

useiv.startAnimation(animationstd); 

는 널 포인터 예외가 발생합니다.

CODE : 당신이 당신의 버튼에있는 레이아웃에 대한 참조를 통과하지 않았기 때문에 당신은 TestButton 내부에서 findViewById를 호출 할 수 없습니다

package de.passsy.multitouch; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.widget.Button; 

public class TestButton extends Button { 

public TestButton(final Context context, final AttributeSet attrs) { 
    super(context, attrs); 

} 

@Override 
public boolean onTouchEvent(final MotionEvent event) { 

    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      final Animation animationstd = AnimationUtils.loadAnimation(getContext(), 
      R.anim.fromleft); 
    useiv = (TestButton) findViewById(R.id.imageButton1); 
    useiv.startAnimation(animationstd); //this line = null pointer exception 
    } 
    return super.onTouchEvent(event); 
} 
} 

답변

1

당신은에 TestButton 클래스 외부 findViewById()를 호출해야합니다. 움직여야하는 버튼을 찾아 그 참조를 전달하십시오. 이처럼 :

onCreate() 방법 그리고
public class TestButton extends Button { 

    TestButton mImageButton; // You were calling it useiv 

    public TestButton(final Context context, final AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public setAnotherButtonToAnimate(TestButton button) { 
     this.mImageButton = button; 
    } 

    @Override 
    public boolean onTouchEvent(final MotionEvent event) { 

     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      final Animation animationstd = AnimationUtils.loadAnimation(getContext(), R.anim.fromleft); 
      if (mImageButton != null) { 
       mImageButton.startAnimation(animationstd); 
      } 
     } 
     return super.onTouchEvent(event); 
    } 
} 

:

@Override 
public void onCreate(final Bundle savedInstanceState) { 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    getWindow().clearFlags(
      WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    TestButton imageButton1 = (TestButton) findViewById(R.id.imageButton1); 
    (...) 
    btn4 = (TestButton) findViewById(R.id.button4); 
    btn4.setAnotherButtonToAnimate(imageButton1); 
    btn4.setOnTouchListener(this); 
+0

내가보기까지 전망의 배열을 보낼 수는 내가보기 내에서 원하는 하나를 선택? 나는 그것을 시도하고 다시 당신에게 돌아갈거야. –

+0

이 두 가지보기, 테스트 및 작동합니다. 고맙습니다. 나는 내일 배열을 통과 시키려고 노력할 것이다. 고맙습니다. –