App Inventor 2 - 图像随时间变化

时间:2015-10-23 03:36:12

标签: android app-inventor

我需要这样做,以便图像在大约27秒后自动更改为与声音文件相关的另一个图像。 我正在使用 App Inventor 2 来做这件事,当涉及到这类事情时,我也是一个菜鸟。

2 个答案:

答案 0 :(得分:0)

试试这个: XML代码

private ImageSwitcher sw;
private TextView tv1;
CountDownTimer myCountDown;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv1 = (TextView) findViewById(R.id.tv1);
    sw = (ImageSwitcher) findViewById(R.id.imageSwitcher);
    sw.setFactory(new ViewFactory() {

        @Override
        public View makeView() {
            ImageView myView = new ImageView(getApplicationContext());
            myView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            myView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            return myView;
        }
    });
    sw.setImageResource(R.drawable.love1);

    myCountDown =  new CountDownTimer(27000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            tv1.setText(millisUntilFinished/1000 + "");
        }

        @Override
        public void onFinish() {
            tv1.setText("0");
            sw.setImageResource(R.drawable.love2);
        }
    };
    myCountDown.start();
}

活动:

public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

    private GestureDetector gestureDetector;
    private ClickListener clickListener;

    public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
        this.clickListener = clickListener;
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && clickListener != null) {
                    clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child));
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

        View child = rv.findChildViewUnder(e.getX(), e.getY());
        if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
            clickListener.onClick(child, rv.getChildPosition(child));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }


    public interface ClickListener {

        void onClick(View view, int position);

        void onLongClick(View view, int position);
    }

}

答案 1 :(得分:0)

首先,您需要使用Clock组件。你可以在传感器部分找到它。只需将其拖入屏幕即可。其次,只需制作一个全局的图像列表,以便在播放声音时查看。

然后,只为时间倒计时和图像索引创建一个全局整数变量。你只需要将时间变量设置为" 27"和图像索引变量为" 0"单击声音按钮时。

然后,添加一个检查来检测倒计时变量。请参阅下面的图片以获取完整代码:

enter image description here

最后,您需要将倒计时变量设置为" 0"何时发声结束。您还可以将画布显示为false以隐藏图像。