【问题标题】:How to animate button\view from within another buttons ontouch ImageButton view?如何在另一个按钮 ontouch ImageButton 视图中为按钮\视图设置动画?
【发布时间】:2013-06-11 12:02:39
【问题描述】:

请参阅http://www.passsy.de/multitouch-for-all-views/#comment-47486,其中最相关的代码如下所示。

如果按下一个扩展按钮的“TestButton”,该视图将传递给我的“TestButton”代码,我可以为该按钮/视图设置动画。但我也想为另一个视图设置动画。如何使用我在此处创建的视图为不同的视图设置动画,或者如何通知我的活动以执行操作? 这适用于触摸的按钮:

startAnimation(animationstd);

但在另一个按钮上:

useiv.startAnimation(animationstd);

导致空指针异常。

代码:

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);
}
}

【问题讨论】:

    标签: android button multi-touch touch-event motionevent


    【解决方案1】:

    您不能从TestButton 内部调用findViewById,因为您尚未将对按钮所在布局的引用传递给它。您必须在TestButton 类之外调用findViewById() 才能找到必须设置动画的按钮,然后将该引用传递给它。像这样:

    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);
        }
    }
    

    然后在你的onCreate() 方法中:

    @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);
    

    【讨论】:

    • 我可以将一组视图发送到视图,然后在视图中选择我想要的视图吗?我会尝试并回复你。
    • 这允许我有两个视图,经过测试并且有效。谢谢你。明天我会尝试传递一个数组。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多