Android特效开发

0
Android Java C/C++ Go list 19232 次浏览
Android特效开发

本次我要向大家介绍一个Android特效,这个特效也是我在某款软件中看到的,至于软件叫什么,
好了,我来上一张动态效果图 在下面,屏幕小的请往下拉。

我不知道原软件是怎么个实现法,在这里我只是说说我的实现方法,代码可能不太好,这只是本人的一个idea 原理很简单!


改变按钮的宽度,每次当你点击按钮时,只有两个按钮改变宽度,一个变长,一个变短,只是这个变化是慢慢

进行,不是秒变的,你懂的,这就是动画效果。你点击短的按钮后会渐渐地变长,长的按钮会随着被压缩,其他按钮宽度不变。

 我写这篇文章想起到一个抛砖引玉的效果,希望读者看了这篇文章后继续写个更好的文章。当然你不要忘记我啊,

记得要与我和大家分享啊。

 下面是特效Demo运行后的效果图:


可以看到view被挤压的效果,点击短的view会慢慢长大。在短的view慢慢长大的同时,最长的view会慢慢缩小,而且每次动画结束后都

有一个最长的view 。我这里所说的view在本次Demo中是按钮,当然你也可以换成其他的控件,或者是ViewGroup的子类也行。
[java] view plaincopyprint?

    public class PinchActivity extends Activity implements View.OnClickListener {  
      
        private final static String TAG = "MainActivity";  
          
        // 屏幕宽度  
        private int screentWidth = 0;  
          
        // View可伸展最长的宽度  
        private int maxWidth;  
          
        // View可伸展最小宽度  
        private int minWidth;  
          
        // 当前点击的View  
        private View currentView;  
          
        // 显示最长的那个View  
        private View preView;  
          
        // 主布局ViewGroup  
        private LinearLayout mainContain;  
          
        // 标识符 动画是否结束  
        private boolean animationIsEnd = true;  
          
        // 变大操作  
        private static final int OPE_BIG = 1;  
          
        // 变小操作  
        private static final int OPE_SMALL = 2;  
          
        // 当前操作 -1表示无效操作  
        private int currentOpe = -1;  
          
        // 前进的步伐距离  
        private static final int STEP = 10;  
      
        @Override  
        protected void onCreate(Bundle savedInstanceState)   
        {  
      
            super.onCreate(savedInstanceState);  
              
            setContentView(R.layout.activity_main);  
      
            initCommonData();  
              
            initViewData();  
              
            measureWidth(screentWidth);   
      
        }  
      
        private void initViewData() {  
      
            mainContain = (LinearLayout) this.findViewById(R.id.main_contain);  
            View child;  
            int childCount = mainContain.getChildCount();  
            for (int i = 0; i < childCount; i++) {  
                child = mainContain.getChildAt(i);  
                child.setOnClickListener(this);  
            }  
        }  
          
        private void initCommonData()  
        {  
            DisplayMetrics metric = new DisplayMetrics();  
            getWindowManager().getDefaultDisplay().getMetrics(metric);  
            screentWidth = metric.widthPixels; // 屏幕宽度(像素)  
        }  
      
      
        private void setCurrentViewParams() {  
      
            if (currentView == null) {  
                return;  
            }  
            LayoutParams params = currentView.getLayoutParams();  
            if (params == null) {  
                return;  
            }  
            int realWidth = params.width;  
            int nextWidth = 0;  
            if (currentOpe == OPE_BIG) {  
                nextWidth = realWidth + STEP;  
            } else if (currentOpe == OPE_SMALL) {  
                nextWidth = realWidth - STEP;  
            }  
            if (nextWidth > maxWidth) {  
                nextWidth = maxWidth;  
            } else if (nextWidth < minWidth) {  
                nextWidth = minWidth;  
            }  
            params.width = nextWidth;  
            currentView.setLayoutParams(params);  
            if (nextWidth == maxWidth || nextWidth == minWidth) {  
                animationIsEnd = true;  
                onOffClickable();  
                stopAnimation();  
                return;  
            }  
            mHandler.sendEmptyMessageDelayed(1, 20);  
        }  
      
        // 初始化宽度 测量max min 长度  
        private void measureWidth(int screenWidth) {  
              
            int halfWidth = screenWidth / 2;  
            maxWidth = halfWidth - 50;  
            minWidth = (screenWidth - maxWidth) / (mainContain.getChildCount() - 1);  
      
            View child;  
            int childCount = mainContain.getChildCount();  
            for (int i = 0; i < childCount; i++) {  
                child = mainContain.getChildAt(i);  
                LayoutParams params = child.getLayoutParams();  
                if (i == 0) {  
                    preView = child;  
                    params.width = maxWidth;  
                } else {  
                    params.width = minWidth;  
                }  
      
                child.setLayoutParams(params);  
            }  
        }  
      
        // 这里用handler更新界面  
        private Handler mHandler = new Handler() {  
      
            @Override  
            public void handleMessage(Message msg) {  
      
                if (msg.what == 1) {  
                    setCurrentViewParams();  
                }   
            }  
      
        };  
      
        // 停止动画  
        private void stopAnimation() {  
            currentOpe = -1;  
            currentView = null;  
        }  
      
        private void startAnimation() {  
      
            if (currentView == null || currentOpe == -1) {  
                Log.d(TAG, "无效动画");  
                return;  
            }  
              
            animationIsEnd = false;  
            onOffClickable();  
            mHandler.sendEmptyMessage(1);  
        }  
      
        @Override  
        public void onClick(View v) {  
              
            int id = v.getId();  
              
            switch (id) {  
              
            case R.id.btnOne:  
                currentView = mainContain.getChildAt(0);  
                break;  
            case R.id.btnTwo:  
                currentView = mainContain.getChildAt(1);  
                break;  
            case R.id.btnThree:  
                currentView = mainContain.getChildAt(2);  
                break;  
            case R.id.btnFour:  
                currentView = mainContain.getChildAt(3);  
                break;  
            }  
      
            Log.i(TAG, ((Button) currentView).getText().toString() + " click");  
              
            if (currentView != null && animationIsEnd) {  
                  
                int currentViewWidth = currentView.getWidth();  
                  
                if (currentViewWidth == maxWidth) {  
                    currentOpe = OPE_SMALL;  
                } else {  
                    currentOpe = OPE_BIG;  
                }  
                  
                clickEvent(currentView);  
                  
                startAnimation();  
            }  
      
        }  
      
        private void clickEvent(View view) {  
            View child;  
            int childCount = mainContain.getChildCount();  
            for (int i = 0; i < childCount; i++) {  
                child = mainContain.getChildAt(i);  
                if (preView == child) {  
                    LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) child  
                            .getLayoutParams();  
                    params.weight = 1.0f;  
                    child.setLayoutParams(params);  
                } else {  
                    LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) child  
                            .getLayoutParams();  
                    params.weight = 0.0f;  
                    params.width = minWidth;  
                    child.setLayoutParams(params);  
                }  
            }  
            preView = view;  
            printWeight();  
        }  
      
        private void printWeight() {  
            View child;  
            int childCount = mainContain.getChildCount();  
            for (int i = 0; i < childCount; i++) {  
                child = mainContain.getChildAt(i);  
                LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) child  
                        .getLayoutParams();  
                Log.i("mm1", ((Button) child).getText() + ": " + params.weight);  
            }  
        }  
          
        //   
        private void onOffClickable()  
        {  
            View child;  
            boolean clickable = animationIsEnd;  
            int childCount = mainContain.getChildCount();  
            for (int i = 0; i < childCount; i++) {  
                child = mainContain.getChildAt(i);  
                child.setClickable(clickable);  
            }  
        }  
      
    }
大家学了过后,有没有帮助啊。学android特效上IT蓝豹。

请尽量让自己的答案能够对别人有帮助

47个答案

默认排序 按投票排序
1 2 3