
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="horizontal" >
6 <LinearLayout
7 android:id="@+id/main_contain"
8 android:layout_marginTop="50dp"
9 android:layout_width="match_parent"
10 android:layout_height="50dp"
11 android:orientation="horizontal"
12 android:layout_centerInParent="true" >
13 <Button
14 android:id="@+id/btnOne"
15 android:layout_width="wrap_content"
16 android:layout_height="match_parent"
17 android:text="one" />
18 <Button
19 android:id="@+id/btnTwo"
20 android:layout_width="wrap_content"
21 android:layout_height="match_parent"
22 android:text="two" />
23 <Button
24 android:id="@+id/btnThree"
25 android:layout_width="wrap_content"
26 android:layout_height="match_parent"
27 android:text="three" />
28 <Button
29 android:id="@+id/btnFour"
30 android:layout_width="wrap_content"
31 android:layout_height="match_parent"
32 android:text="four" />
33 </LinearLayout>
34 </RelativeLayout>
1 package com.example.xinxindemo;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.os.Handler;
6 import android.os.Message;
7 import android.util.DisplayMetrics;
8 import android.util.Log;
9 import android.view.View;
10 import android.view.ViewGroup.LayoutParams;
11 import android.widget.Button;
12 import android.widget.LinearLayout;
13
14 public class PinchActivity extends Activity implements View.OnClickListener {
15
16 private final static String TAG = "PinchActivity";
18 // 屏幕宽度
19 private int screentWidth = 0;
21 // View可伸展最长的宽度
22 private int maxWidth;
24 // View可伸展最小宽度
25 private int minWidth;
27 // 当前点击的View
28 private View currentView;
30 // 显示最长的那个View
31 private View preView;
33 // 主布局ViewGroup
34 private LinearLayout mainContain;
36 // 标识符 动画是否结束
37 private boolean animationIsEnd = true;
39 // 变大操作
40 private static final int OPE_BIG = 1;
42 // 变小操作
43 private static final int OPE_SMALL = 2;
45 // 当前操作 -1表示无效操作
46 private int currentOpe = -1;
48 // 前进的步伐距离
49 private static final int STEP = 10;
50
51 @Override
52 protected void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
55 setContentView(R.layout.activity_main);
56
57 initCommonData();
58 initViewData();
59 measureWidth(screentWidth);
60 }
61
62 private void initViewData() {
63 mainContain = (LinearLayout) this.findViewById(R.id.main_contain);
64 View child;
65 int childCount = mainContain.getChildCount();
66 for (int i = 0; i < childCount; i++) {
67 child = mainContain.getChildAt(i);
68 child.setOnClickListener(this);
69 }
70 }
71
72 private void initCommonData() {
73 DisplayMetrics metric = new DisplayMetrics();
74 getWindowManager().getDefaultDisplay().getMetrics(metric);
75 // 屏幕宽度(像素)
76 screentWidth = metric.widthPixels;
77 }
78
79 private void setCurrentViewParams() {
80 if (currentView == null) {
81 return;
82 }
83 LayoutParams params = currentView.getLayoutParams();
84 if (params == null) {
85 return;
86 }
87 int realWidth = params.width;
88 int nextWidth = 0;
89 if (currentOpe == OPE_BIG) {
90 nextWidth = realWidth + STEP;
91 } else if (currentOpe == OPE_SMALL) {
92 nextWidth = realWidth - STEP;
93 }
94 if (nextWidth > maxWidth) {
95 nextWidth = maxWidth;
96 } else if (nextWidth < minWidth) {
97 nextWidth = minWidth;
98 }
99 params.width = nextWidth;
100 currentView.setLayoutParams(params);
101 if (nextWidth == maxWidth || nextWidth == minWidth) {
102 animationIsEnd = true;
103 onOffClickable();
104 stopAnimation();
105 return;
106 }
107 mHandler.sendEmptyMessageDelayed(1, 20);
108 }
109
110 // 初始化宽度 测量max min 长度
111 private void measureWidth(int screenWidth) {
112 int halfWidth = screenWidth / 2;
113 maxWidth = halfWidth - 50;
114 minWidth = (screenWidth - maxWidth) / (mainContain.getChildCount() - 1);
115
116 View child;
117 int childCount = mainContain.getChildCount();
118 for (int i = 0; i < childCount; i++) {
119 child = mainContain.getChildAt(i);
120 LayoutParams params = child.getLayoutParams();
121 if (i == 0) {
122 preView = child;
123 params.width = maxWidth;
124 } else {
125 params.width = minWidth;
126 }
127 child.setLayoutParams(params);
128 }
129 }
130
131 // 这里用handler更新界面
132 private Handler mHandler = new Handler() {
133 @Override
134 public void handleMessage(Message msg) {
135 if (msg.what == 1) {
136 setCurrentViewParams();
137 }
138 }
139 };
140
141 // 停止动画
142 private void stopAnimation() {
143 currentOpe = -1;
144 currentView = null;
145 }
146
147 private void startAnimation() {
148 if (currentView == null || currentOpe == -1) {
149 Log.d(TAG, "无效动画");
150 return;
151 }
152 animationIsEnd = false;
153 onOffClickable();
154 mHandler.sendEmptyMessage(1);
155 }
156
157 @Override
158 public void onClick(View v) {
159 int id = v.getId();
160 switch (id) {
161 case R.id.btnOne:
162 currentView = mainContain.getChildAt(0);
163 break;
164 case R.id.btnTwo:
165 currentView = mainContain.getChildAt(1);
166 break;
167 case R.id.btnThree:
168 currentView = mainContain.getChildAt(2);
169 break;
170 case R.id.btnFour:
171 currentView = mainContain.getChildAt(3);
172 break;
173 }
174
175 Log.i(TAG, ((Button) currentView).getText().toString() + " click");
176
177 if (currentView != null && animationIsEnd) {
178 int currentViewWidth = currentView.getWidth();
179 if (currentViewWidth == maxWidth) {
180 currentOpe = OPE_SMALL;
181 } else {
182 currentOpe = OPE_BIG;
183 }
184 clickEvent(currentView);
185 startAnimation();
186 }
187
188 }
189
190 private void clickEvent(View view) {
191 View child;
192 int childCount = mainContain.getChildCount();
193 for (int i = 0; i < childCount; i++) {
194 child = mainContain.getChildAt(i);
195 if (preView == child) {
196 LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) child
197 .getLayoutParams();
198 params.weight = 1.0f;
199 child.setLayoutParams(params);
200 } else {
201 LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) child
202 .getLayoutParams();
203 params.weight = 0.0f;
204 params.width = minWidth;
205 child.setLayoutParams(params);
206 }
207 }
208 preView = view;
209 printWeight();
210 }
211
212 private void printWeight() {
213 View child;
214 int childCount = mainContain.getChildCount();
215 for (int i = 0; i < childCount; i++) {
216 child = mainContain.getChildAt(i);
217 LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) child
218 .getLayoutParams();
219 Log.i("mm1", ((Button) child).getText() + ": " + params.weight);
220 }
221 }
222
223 //
224 private void onOffClickable() {
225 View child;
226 boolean clickable = animationIsEnd;
227 int childCount = mainContain.getChildCount();
228 for (int i = 0; i < childCount; i++) {
229 child = mainContain.getChildAt(i);
230 child.setClickable(clickable);
231 }
232 }
233
234 }