需求:imageview点击放大移动到中心位置,点击缩小返回到原来位置

1. 需求

imageview点击放大移动到中心位置,点击缩小返回到原来位置

2. 问题点

使用ScaleAnimation动画类imageview放大并移动后,但是点击放大后的图片不能获取点击事件,显示控件边界后 可以看到imageview其实是没有移动的;

3. 解决方案

1原有地方一个imageview1,2.中心位置放一个和放大后一样的imageview2,imageview1显示,imageview2隐藏;

1)点击imageview1后,imageview使用ScaleAnimation动画进行放大,并隐藏imageview1,等动画完成后显示imageview2,取消动画;

2)点击imageview2后,imageview2使用ScaleAnimation动画进行缩小,并隐藏imageview2,等动画结束后显示imageview1,取消动画;

4. 案例:

4.1 xml内容:

        

        

4.2 onclick事件:

    case R.id.img_samll_sn:
                img_samll_sn.setAnimation(AnimationUtil.scaleAnimationSet(true));
                img_samll_sn.setVisibility(View.GONE);
                AnimationUtil.destoryAnimSet();
                handler.sendEmptyMessageDelayed(HANDLER_HIDE_SAMLL_IMG, delayTime);
                break;
            case R.id.img_big_sn:
                img_big_sn.setAnimation(AnimationUtil.scaleAnimationSet(false));
                img_big_sn.setVisibility(View.GONE);
                AnimationUtil.destoryAnimSet();
                handler.sendEmptyMessageDelayed(HANDLER_HIDE_BIG_IMG, delayTime);

4.3 Handler内容:

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case HANDLER_HIDE_SAMLL_IMG:
                    img_big_sn.setVisibility(View.VISIBLE);
                    break;
                case HANDLER_HIDE_BIG_IMG:
                    img_samll_sn.setVisibility(View.VISIBLE);
                    break;
            }
        }
    };

4.4 工具类:

    /**
     * 图片自动循环缩放
     *
     * @return
     */
    public static AnimationSet scaleAnimationSet(boolean isStartAnimation) {
        animationSet = new AnimationSet(true);
        ScaleAnimation scaleAnimation = null;
        if (isStartAnimation) {
            scaleAnimation = new ScaleAnimation(1f, 4f, 1f, 4f,
                    Animation.RELATIVE_TO_PARENT, -0.12f, Animation.RELATIVE_TO_PARENT, 0.02f);
        } else {
            scaleAnimation = new ScaleAnimation(1f, 0.25f, 1f, 0.25f,
                    Animation.RELATIVE_TO_PARENT, -0.49f, Animation.RELATIVE_TO_PARENT, 0.082f);
        }

        scaleAnimation.setDuration(1000);                        //执行时间
//        scaleAnimation.setRepeatCount(0);                    //重复执行动画
//        scaleAnimation.setRepeatMode(Animation.REVERSE);    //重复 缩小和放大效果
        animationSet.addAnimation(scaleAnimation);
        animationSet.setFillAfter(true);//保持结束时状态
        return animationSet;
    }

 

你可能感兴趣的:(Android开发)