Some words about me

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas a urna sit amet leo sagittis cursus a at lectus. Donec bibendum facilisis ultricies. Maecenas nec arcu dui, ut lobortis erat. In nec condimentum quam. Vivamus euismod scelerisque ligula vitae fermentum. Nulla dignissim rutrum eleifend.

Donec mi eros, congue ut consectetur eget, pretium vitae orci. Aliquam pharetra ultricies lobortis. Morbi eget nisi lectus. Maecenas felis augue, molestie in vestibulum et, lacinia et leo. Suspendisse potenti. Maecenas eget est augue. Donec tincidunt mollis fermentum.

Contact me?

 
 
 

Donec mi eros, congue ut consectetur eget, pretium vitae orci. Aliquam pharetra ultricies lobortis. Morbi eget nisi lectus. Maecenas felis augue, molestie in vestibulum et, lacinia et leo. Suspendisse potenti. Maecenas eget est augue. Donec tincidunt mollis fermentum.

Send the message
 

实现一个简单但是好看的滑动栏

 哈哈,终于把滑动栏做好了,但是没有手势的滑动,只有点击按钮的效果,不过,我觉得还不错了!

这个就是我应用中用到的滑动栏的效果!,在这里我就share一下咯!

大家看代码就知道他的详细情况了:

这里我们有一个主类,这里会有两个布局文件与它相连:

 

这里显示main_slide布局文件:

请记住这里要用到framlayout哦!

              

  1 <?xml version="1.0" encoding="utf-8"?>
  2 
  3 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4 
  5     android:layout_width="fill_parent"
  6 
  7     android:layout_height="fill_parent"
  8 
  9     android:background="#ffffff" >
 10 
 11  
 12 
 13     <!-- 左边菜单 -->
 14 
 15  
 16 
 17     <RelativeLayout
 18 
 19         xmlns:android="http://schemas.android.com/apk/res/android"
 20 
 21         android:id="@+id/leftmenu"
 22 
 23         android:layout_width="250dip"
 24 
 25         android:layout_height="fill_parent"
 26 
 27         android:background="@drawable/background"
 28 
 29         android:orientation="vertical"
 30 
 31         android:visibility="invisible" >
 32 
 33  
 34 
 35         <ImageView
 36 
 37             android:id="@+id/imgHander"
 38 
 39             android:layout_width="90dip"
 40 
 41             android:layout_height="90dip"
 42 
 43             android:layout_centerHorizontal="true"
 44 
 45             android:layout_marginTop="20dip" />
 46 
 47  
 48 
 49         <TextView
 50 
 51             android:id="@+id/text_name"
 52 
 53             android:layout_width="wrap_content"
 54 
 55             android:layout_height="wrap_content"
 56 
 57             android:layout_below="@id/imgHander"
 58 
 59             android:layout_centerHorizontal="true"
 60 
 61             android:layout_marginTop="10dip"
 62 
 63             android:text="Catherine"
 64 
 65             android:textColor="#fff"
 66 
 67             android:textSize="25sp"
 68 
 69             android:textStyle="bold" />
 70 
 71  
 72 
 73         <TextView
 74 
 75             android:id="@+id/text_moto"
 76 
 77             android:layout_width="wrap_content"
 78 
 79             android:layout_height="wrap_content"
 80 
 81             android:layout_below="@id/text_name"
 82 
 83             android:layout_centerHorizontal="true"
 84 
 85             android:text="you never know that"
 86 
 87             android:textColor="#fff"
 88 
 89             android:textSize="15sp"
 90 
 91             android:textStyle="bold" />
 92 
 93  
 94 
 95         <Button
 96 
 97             android:id="@+id/btn_fav"
 98 
 99             android:layout_width="230dip"
100 
101             android:layout_height="40dip"
102 
103             android:layout_below="@id/text_moto"
104 
105             android:layout_centerHorizontal="true"
106 
107             android:layout_marginTop="30dip"
108 
109             android:background="@drawable/favourite" />
110 
111  
112 
113         <Button
114 
115             android:id="@+id/btn_set"
116 
117             android:layout_width="230dip"
118 
119             android:layout_height="40dip"
120 
121             android:layout_below="@id/btn_fav"
122 
123             android:layout_centerHorizontal="true"
124 
125             android:layout_marginTop="5dip"
126 
127             android:background="@drawable/setting" />
128 
129  
130 
131         <Button
132 
133             android:id="@+id/btn_com"
134 
135             android:layout_width="230dip"
136 
137             android:layout_height="40dip"
138 
139             android:layout_below="@id/btn_set"
140 
141             android:layout_centerHorizontal="true"
142 
143             android:layout_marginTop="5dip"
144 
145             android:background="@drawable/comment" />
146 
147  
148 
149         <Button
150 
151             android:id="@+id/btn_pro"
152 
153             android:layout_width="230dip"
154 
155             android:layout_height="40dip"
156 
157             android:layout_below="@id/btn_com"
158 
159             android:layout_centerHorizontal="true"
160 
161             android:layout_marginTop="5dip"
162 
163             android:background="@drawable/profile" />
164 
165     </RelativeLayout>
166 
167  
168 
169     <grimbo.android.demo.slidingmenu.MyHorizontalScrollView
170 
171         android:id="@+id/myScrollView"
172 
173         android:layout_width="fill_parent"
174 
175         android:layout_height="fill_parent"
176 
177         android:fadingEdge="none"
178 
179         android:scrollbars="none" >
180 
181  
182 
183         <!--
184 
185         这里按钮就是控制滑图案出来的背景颜色,当然,如果这里没有了背景颜色,就会以上面的
186 
187         背景颜色为主
188 
189         -->
190 
191  
192 
193         <LinearLayout
194 
195             android:id="@+id/top"
196 
197             android:layout_width="fill_parent"
198 
199             android:layout_height="fill_parent"
200 
201             android:orientation="horizontal" >
202 
203         </LinearLayout>
204 
205     </grimbo.android.demo.slidingmenu.MyHorizontalScrollView>
206 
207  
208 
209 </FrameLayout>

 

接下来就是main布局文件:这个布局文件就是显示我们当前不做任何操作显示的界面:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 4 
 5     android:id="@+id/app"
 6 
 7     android:layout_width="fill_parent"
 8 
 9     android:layout_height="wrap_content"
10 
11     android:background="#00f"
12 
13     android:orientation="vertical" >
14 
15  
16 
17     <RelativeLayout
18 
19         android:layout_width="fill_parent"
20 
21         android:layout_height="wrap_content"
22 
23         android:orientation="horizontal"
24 
25         android:paddingLeft="2dip" 
26 
27         android:paddingTop="4dip"
28 
29         >
30 
31  
32 
33         <ImageView
34 
35             android:id="@+id/leftButton"
36 
37             android:layout_width="60dip"
38 
39             android:layout_height="60dip" />
40 
41     </RelativeLayout>
42 
43  
44 
45 </LinearLayout>
46 
47  
48 
49  

 

这里就是主类:

  1 package grimbo.android.demo.slidingmenu;
  2 
  3  
  4 
  5 import cn.android.app.R;
  6 
  7 import android.app.Activity;
  8 
  9 import android.graphics.Bitmap;
 10 
 11 import android.graphics.Bitmap.Config;
 12 
 13 import android.graphics.BitmapFactory;
 14 
 15 import android.graphics.Canvas;
 16 
 17 import android.graphics.Color;
 18 
 19 import android.graphics.Paint;
 20 
 21 import android.graphics.PorterDuff.Mode;
 22 
 23 import android.graphics.PorterDuffXfermode;
 24 
 25 import android.graphics.Rect;
 26 
 27 import android.graphics.RectF;
 28 
 29 import android.os.Bundle;
 30 
 31 import android.view.LayoutInflater;
 32 
 33 import android.view.View;
 34 
 35 import android.widget.ImageView;
 36 
 37  
 38 
 39 /**
 40 
 41  * This example uses a FrameLayout to display a menu View and a
 42 
 43  * HorizontalScrollView (HSV).
 44 
 45  * 
 46 
 47  * The HSV has a transparent View as the first child, which means the menu will
 48 
 49  * show through when the HSV is scrolled.
 50 
 51  */
 52 
 53 public class MainActivity extends Activity {
 54 
 55 private MyHorizontalScrollView scrollView;
 56 
 57 private View leftMenu;
 58 
 59 // private View rightMenu;
 60 
 61  
 62 
 63 private View tab01;
 64 
 65 private ImageView leftButton;
 66 
 67  
 68 
 69 // private Button rightButton;
 70 
 71  
 72 
 73 @Override
 74 
 75 public void onCreate(Bundle savedInstanceState) {
 76 
 77 super.onCreate(savedInstanceState);
 78 
 79  
 80 
 81 LayoutInflater inflater = LayoutInflater.from(this);
 82 
 83 setContentView(inflater.inflate(R.layout.main_slide, null));
 84 
 85  
 86 
 87 scrollView = (MyHorizontalScrollView) findViewById(R.id.myScrollView);
 88 
 89 leftMenu = findViewById(R.id.leftmenu);
 90 
 91 // rightMenu = findViewById(R.id.rightmenu);
 92 
 93  
 94 
 95 tab01 = inflater.inflate(R.layout.tab01, null);
 96 
 97 leftButton = (ImageView) main.findViewById(R.id.leftButton);
 98 
 99 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
100 
101 R.drawable.ic_launcher);
102 
103 Bitmap output = getRoundedCornerBitmap(bitmap);
104 
105 leftButton.setImageBitmap(output);
106 
107  
108 
109 leftButton.setOnClickListener(new View.OnClickListener() {
110 
111  
112 
113 @Override
114 
115 public void onClick(View v) {
116 
117 scrollView.clickLeftButton(leftButton.getMeasuredWidth());
118 
119 }
120 
121 });
122 
123  
124 
125 // 以上的代码是设置我的头像为圆形的,以前找了好久,才知道,一句代码就ok啦!好吧,我
126 
127 // 又想错了,其实还是需要很多的代码
128 
129  
130 
131 View leftView = new View(this);// 左边透明视图
132 
133 // View rightView = new View(this);// 右边透明视图
134 
135 leftView.setBackgroundColor(Color.TRANSPARENT);
136 
137 // rightView.setBackgroundColor(Color.TRANSPARENT);
138 
139 // final View[] children = new View[] { leftView, tab01,rightView };
140 
141 final View[] children = new View[] { leftView, tab01 };
142 
143 // 初始化滚动布局
144 
145 // scrollView.initViews(children, new
146 
147 // SizeCallbackForMenu(leftButton),leftMenu,rightMenu);
148 
149 scrollView.initViews(children, new SizeCallbackForMenu(leftButton),
150 
151 leftMenu);
152 
153 }
154 
155  
156 
157 /**
158 
159  * 圆形头像
160 
161  * 
162 
163  * @param bitmap
164 
165  * @param ratio
166 
167  *            按照截取比例来获取圆形图片
168 
169  * @return
170 
171  */
172 
173 public Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
174 
175 if (bitmap == null) {
176 
177 bitmap = BitmapFactory.decodeResource(getResources(),
178 
179 R.drawable.ic_launcher);
180 
181 }
182 
183 Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(),
184 
185 bitmap.getHeight(), Config.ARGB_8888);
186 
187 Canvas canvas = new Canvas(outBitmap);
188 
189 final int color = 0xff424242;
190 
191 final Paint paint = new Paint();
192 
193 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
194 
195 final RectF rectF = new RectF(rect);
196 
197 final float roundPX = bitmap.getWidth() / 2 < bitmap.getHeight() / 2 ? bitmap
198 
199 .getWidth() : bitmap.getHeight();
200 
201 paint.setAntiAlias(true);
202 
203 canvas.drawARGB(0, 0, 0, 0);
204 
205 paint.setColor(color);
206 
207 canvas.drawRoundRect(rectF, roundPX, roundPX, paint);
208 
209 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
210 
211 canvas.drawBitmap(bitmap, rect, rect, paint);
212 
213 return outBitmap;
214 
215 }
216 
217  
218 
219 }
220 
221  
222 
223  
224 
225  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

这里是另外一个类,这些都是打辅助的了:

 

  1 package grimbo.android.demo.slidingmenu;
  2 
  3  
  4 
  5 import android.content.Context;
  6 
  7 import android.os.Handler;
  8 
  9 import android.util.AttributeSet;
 10 
 11 import android.util.Log;
 12 
 13 import android.view.GestureDetector;
 14 
 15 import android.view.GestureDetector.SimpleOnGestureListener;
 16 
 17 import android.view.MotionEvent;
 18 
 19 import android.view.VelocityTracker;
 20 
 21 import android.view.View;
 22 
 23 import android.view.ViewGroup;
 24 
 25 import android.view.GestureDetector.OnGestureListener;
 26 
 27 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
 28 
 29 import android.widget.HorizontalScrollView;
 30 
 31 import android.widget.Scroller;
 32 
 33  
 34 
 35 /**
 36 
 37  * A HorizontalScrollView (HSV) implementation that disallows touch events (so no scrolling can be done by the user).
 38 
 39  * 
 40 
 41  * This HSV MUST contain a single ViewGroup as its only child, and this ViewGroup will be used to display the children Views
 42 
 43  * passed in to the initViews() method.
 44 
 45  */
 46 
 47 public class HomePageHorizontalScrollView extends HorizontalScrollView {
 48 
 49  
 50 
 51 private final String tag = "MyHorizontalScrollView";
 52 
 53  
 54 
 55 private HomePageHorizontalScrollView me;//当前控件
 56 
 57 private View leftMenu;//左边菜单
 58 
 59 private View rightMenu;//右边菜单
 60 
 61 private boolean leftMenuOut = false;//左边菜单状态
 62 
 63 private boolean rightMenuOut = false;//左边菜单状态
 64 
 65 private final int ENLARGE_WIDTH = 20;//扩展宽度
 66 
 67  
 68 
 69     public HomePageHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
 70 
 71         super(context, attrs, defStyle);
 72 
 73         init(context);
 74 
 75     }
 76 
 77  
 78 
 79     public HomePageHorizontalScrollView(Context context, AttributeSet attrs) {
 80 
 81         super(context, attrs);
 82 
 83         init(context);
 84 
 85     }
 86 
 87  
 88 
 89     public HomePageHorizontalScrollView(Context context) {
 90 
 91         super(context);
 92 
 93         init(context);
 94 
 95     }
 96 
 97  
 98 
 99     void init(Context context) {
100 
101         // remove the fading as the HSV looks better without it
102 
103         setHorizontalFadingEdgeEnabled(false);
104 
105         setVerticalFadingEdgeEnabled(false);
106 
107         me = this;
108 
109         me.setVisibility(View.INVISIBLE);
110 
111     }
112 
113     
114 
115     
116 
117     /**
118 
119      * @param children
120 
121      *            The child Views to add to parent.
122 
123      * @param scrollToViewIdx
124 
125      *            The index of the View to scroll to after initialisation.
126 
127      * @param sizeCallback
128 
129      *            A SizeCallback to interact with the HSV.
130 
131      */
132 
133     public void initViews(View[] children, SizeCallback sizeCallback,View leftMenu,View rightMenu) {
134 
135         this.leftMenu = leftMenu;
136 
137         this.rightMenu = rightMenu;
138 
139         ViewGroup parent = (ViewGroup) getChildAt(0);
140 
141  
142 
143         // Add all the children, but add them invisible so that the layouts are calculated, but you can't see the Views
144 
145         for (int i = 0; i < children.length; i++) {
146 
147             children[i].setVisibility(View.INVISIBLE);
148 
149             parent.addView(children[i]);
150 
151         }
152 
153  
154 
155         // Add a layout listener to this HSV
156 
157         // This listener is responsible for arranging the child views.
158 
159         OnGlobalLayoutListener listener = new MyOnGlobalLayoutListener(parent, children, sizeCallback);
160 
161         getViewTreeObserver().addOnGlobalLayoutListener(listener);
162 
163     }
164 
165  
166 
167     
168 
169     
170 
171     
172 
173  
174 
175     @Override
176 
177     public boolean onInterceptTouchEvent(MotionEvent ev) {
178 
179         // Do not allow touch events.
180 
181         return false;
182 
183     }
184 
185  
186 
187     /**
188 
189      * An OnGlobalLayoutListener impl that passes on the call to onGlobalLayout to a SizeCallback, before removing all the Views
190 
191      * in the HSV and adding them again with calculated widths and heights.
192 
193      */
194 
195     class MyOnGlobalLayoutListener implements OnGlobalLayoutListener {
196 
197         ViewGroup parent;
198 
199         View[] children;
200 
201         int scrollToViewPos = 0;
202 
203         SizeCallback sizeCallback;
204 
205  
206 
207         /**
208 
209          * @param parent
210 
211          *            The parent to which the child Views should be added.
212 
213          * @param children
214 
215          *            The child Views to add to parent.
216 
217          * @param scrollToViewIdx
218 
219          *            The index of the View to scroll to after initialisation.
220 
221          * @param sizeCallback
222 
223          *            A SizeCallback to interact with the HSV.
224 
225          */
226 
227         public MyOnGlobalLayoutListener(ViewGroup parent, View[] children, SizeCallback sizeCallback) {
228 
229             this.parent = parent;
230 
231             this.children = children;
232 
233             this.sizeCallback = sizeCallback;
234 
235         }
236 
237  
238 
239         @Override
240 
241         public void onGlobalLayout() {
242 
243             // System.out.println("onGlobalLayout");
244 
245  
246 
247             //final HorizontalScrollView me = MyHorizontalScrollView.this;
248 
249  
250 
251             // The listener will remove itself as a layout listener to the HSV
252 
253             me.getViewTreeObserver().removeGlobalOnLayoutListener(this);
254 
255  
256 
257             // Allow the SizeCallback to 'see' the Views before we remove them and re-add them.
258 
259             // This lets the SizeCallback prepare View sizes, ahead of calls to SizeCallback.getViewSize().
260 
261             sizeCallback.onGlobalLayout();
262 
263  
264 
265             parent.removeViewsInLayout(0, children.length);
266 
267  
268 
269             final int w = me.getMeasuredWidth();
270 
271             final int h = me.getMeasuredHeight();
272 
273  
274 
275             // System.out.println("w=" + w + ", h=" + h);
276 
277  
278 
279             // Add each view in turn, and apply the width and height returned by the SizeCallback.
280 
281             int[] dims = new int[2];
282 
283             scrollToViewPos = 0;
284 
285             for (int i = 0; i < children.length; i++) {
286 
287                 sizeCallback.getViewSize(i, w, h, dims);
288 
289                 children[i].setVisibility(View.VISIBLE);
290 
291                 parent.addView(children[i], dims[0], dims[1]);
292 
293                 if (i == 0) {
294 
295                     scrollToViewPos += dims[0];
296 
297                 }
298 
299                 Log.d(tag, children[i]+": w=" + dims[0] + ", h=" + dims[1]);
300 
301                 Log.d(tag, "scrollToViewIdx:"+0+",scrollToViewPos:"+scrollToViewPos);
302 
303             }
304 
305  
306 
307             // For some reason we need to post this action, rather than call immediately.
308 
309             // If we try immediately, it will not scroll.
310 
311             
312 
313             new Handler().post(new Runnable() {
314 
315                 @Override
316 
317                 public void run() {
318 
319                     me.scrollBy(scrollToViewPos,0);
320 
321                     //因为这些控件默认都为隐藏,控件加载完成后,设置成显示
322 
323                     me.setVisibility(View.VISIBLE);
324 
325                     leftMenu.setVisibility(View.VISIBLE);
326 
327                     rightMenu.setVisibility(View.VISIBLE);
328 
329                 }
330 
331             });
332 
333         }
334 
335     }
336 
337     
338 
339     /**
340 
341      * 点击左边按钮
342 
343      * @param leftButtonWidth 左边按钮的宽度
344 
345      */
346 
347     public void clickLeftButton(int leftButtonWidth){
348 
349      //左边
350 
351 rightMenu.setVisibility(View.GONE);
352 
353 leftMenu.setVisibility(View.VISIBLE);
354 
355      int menuWidth = leftMenu.getMeasuredWidth()-(leftButtonWidth+ENLARGE_WIDTH);
356 
357         System.out.println("leftmenuWidth:"+menuWidth);
358 
359         if (!leftMenuOut) {
360 
361             int left = 0;
362 
363             me.smoothScrollTo(left, 0);
364 
365         } else {
366 
367             int left = menuWidth;
368 
369             me.smoothScrollTo(left, 0);
370 
371         }
372 
373         leftMenuOut = !leftMenuOut;
374 
375     }
376 
377     
378 
379     /**
380 
381      * 点击右边按钮 
382 
383      * @param rightButtonWidth 右边按钮的宽度
384 
385      */
386 
387     public void clickRightButton(int rightButtonWidth){
388 
389      //右边
390 
391 leftMenu.setVisibility(View.GONE);
392 
393 rightMenu.setVisibility(View.VISIBLE);
394 
395 int menuWidth = rightMenu.getMeasuredWidth() - (rightButtonWidth+ENLARGE_WIDTH);
396 
397         if (!rightMenuOut) {
398 
399          int right = menuWidth + me.getMeasuredWidth();
400 
401          System.out.println("rightmenuWidth:"+right);
402 
403             me.smoothScrollTo(right, 0);
404 
405         } else {
406 
407          int right = menuWidth;
408 
409          System.out.println("rightmenuWidth:"+right);
410 
411             me.smoothScrollTo(right, 0);
412 
413         }
414 
415         rightMenuOut = !rightMenuOut;
416 
417     }
418 
419     
420 
421     
422 
423  
424 
425     @Override
426 
427 public boolean onTouchEvent(MotionEvent ev) {
428 
429 // TODO Auto-generated method stub
430 
431 //return super.onTouchEvent(ev);
432 
433      return false;
434 
435 }
436 
437  
438 
439 /**
440 
441      * Callback interface to interact with the HSV.
442 
443      */
444 
445     public interface SizeCallback {
446 
447         /**
448 
449          * Used to allow clients to measure Views before re-adding them.
450 
451          */
452 
453         public void onGlobalLayout();
454 
455  
456 
457         /**
458 
459          * Used by clients to specify the View dimensions.
460 
461          * 
462 
463          * @param idx
464 
465          *            Index of the View.
466 
467          * @param w
468 
469          *            Width of the parent View.
470 
471          * @param h
472 
473          *            Height of the parent View.
474 
475          * @param dims
476 
477          *            dims[0] should be set to View width. dims[1] should be set to View height.
478 
479          */
480 
481         public void getViewSize(int idx, int w, int h, int[] dims);
482 
483     }
484 
485 }

 

在主类里面,我们就会用到水平滚动的一些操作:

  1 package grimbo.android.demo.slidingmenu;
  2 
  3  
  4 
  5 import android.content.Context;
  6 
  7 import android.os.Handler;
  8 
  9 import android.util.AttributeSet;
 10 
 11 import android.util.Log;
 12 
 13 import android.view.MotionEvent;
 14 
 15 import android.view.View;
 16 
 17 import android.view.ViewGroup;
 18 
 19 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
 20 
 21 import android.widget.HorizontalScrollView;
 22 
 23  
 24 
 25 /**
 26 
 27  * A HorizontalScrollView (HSV) implementation that disallows touch events (so
 28 
 29  * no scrolling can be done by the user).
 30 
 31  * 
 32 
 33  * This HSV MUST contain a single ViewGroup as its only child, and this
 34 
 35  * ViewGroup will be used to display the children Views passed in to the
 36 
 37  * initViews() method.
 38 
 39  */
 40 
 41 public class MyHorizontalScrollView extends HorizontalScrollView {
 42 
 43  
 44 
 45 private final String tag = "MyHorizontalScrollView";
 46 
 47  
 48 
 49 private MyHorizontalScrollView me;// 当前控件
 50 
 51 private View leftMenu;// 左边菜单
 52 
 53 private boolean leftMenuOut = false;// 左边菜单状态
 54 
 55 private final int ENLARGE_WIDTH = 20;// 扩展宽度
 56 
 57  
 58 
 59 public MyHorizontalScrollView(Context context, AttributeSet attrs,
 60 
 61 int defStyle) {
 62 
 63 super(context, attrs, defStyle);
 64 
 65 init(context);
 66 
 67 }
 68 
 69  
 70 
 71 public MyHorizontalScrollView(Context context, AttributeSet attrs) {
 72 
 73 super(context, attrs);
 74 
 75 init(context);
 76 
 77 }
 78 
 79  
 80 
 81 public MyHorizontalScrollView(Context context) {
 82 
 83 super(context);
 84 
 85 init(context);
 86 
 87 }
 88 
 89  
 90 
 91 void init(Context context) {
 92 
 93 // remove the fading as the HSV looks better without it
 94 
 95 setHorizontalFadingEdgeEnabled(false);
 96 
 97 setVerticalFadingEdgeEnabled(false);
 98 
 99 me = this;
100 
101 me.setVisibility(View.INVISIBLE);
102 
103 }
104 
105  
106 
107 /**
108 
109  * @param children
110 
111  *            The child Views to add to parent.
112 
113  * @param scrollToViewIdx
114 
115  *            The index of the View to scroll to after initialisation.
116 
117  * @param sizeCallback
118 
119  *            A SizeCallback to interact with the HSV.
120 
121  */
122 
123 public void initViews(View[] children, SizeCallback sizeCallback,
124 
125 View leftMenu) {
126 
127 this.leftMenu = leftMenu;
128 
129 ViewGroup parent = (ViewGroup) getChildAt(0);
130 
131  
132 
133 // Add all the children, but add them invisible so that the layouts are
134 
135 // calculated, but you can't see the Views
136 
137 for (int i = 0; i < children.length; i++) {
138 
139 children[i].setVisibility(View.INVISIBLE);
140 
141 parent.addView(children[i]);
142 
143 }
144 
145  
146 
147 // Add a layout listener to this HSV
148 
149 // This listener is responsible for arranging the child views.
150 
151 OnGlobalLayoutListener listener = new MyOnGlobalLayoutListener(parent,
152 
153 children, sizeCallback);
154 
155 getViewTreeObserver().addOnGlobalLayoutListener(listener);
156 
157 }
158 
159  
160 
161 @Override
162 
163 public boolean onInterceptTouchEvent(MotionEvent ev) {
164 
165 return false;
166 
167 }
168 
169  
170 
171 /**
172 
173  * An OnGlobalLayoutListener impl that passes on the call to onGlobalLayout
174 
175  * to a SizeCallback, before removing all the Views in the HSV and adding
176 
177  * them again with calculated widths and heights.
178 
179  */
180 
181 class MyOnGlobalLayoutListener implements OnGlobalLayoutListener {
182 
183 ViewGroup parent;
184 
185 View[] children;
186 
187 int scrollToViewPos = 0;
188 
189 SizeCallback sizeCallback;
190 
191  
192 
193 /**
194 
195  * @param parent
196 
197  *            The parent to which the child Views should be added.
198 
199  * @param children
200 
201  *            The child Views to add to parent.
202 
203  * @param scrollToViewIdx
204 
205  *            The index of the View to scroll to after initialisation.
206 
207  * @param sizeCallback
208 
209  *            A SizeCallback to interact with the HSV.
210 
211  */
212 
213 public MyOnGlobalLayoutListener(ViewGroup parent, View[] children,
214 
215 SizeCallback sizeCallback) {
216 
217 this.parent = parent;
218 
219 this.children = children;
220 
221 this.sizeCallback = sizeCallback;
222 
223 }
224 
225  
226 
227 @Override
228 
229 public void onGlobalLayout() {
230 
231 // System.out.println("onGlobalLayout");
232 
233  
234 
235 // final HorizontalScrollView me = MyHorizontalScrollView.this;
236 
237  
238 
239 // The listener will remove itself as a layout listener to the HSV
240 
241 me.getViewTreeObserver().removeGlobalOnLayoutListener(this);
242 
243  
244 
245 // Allow the SizeCallback to 'see' the Views before we remove them
246 
247 // and re-add them.
248 
249 // This lets the SizeCallback prepare View sizes, ahead of calls to
250 
251 // SizeCallback.getViewSize().
252 
253 sizeCallback.onGlobalLayout();
254 
255  
256 
257 parent.removeViewsInLayout(0, children.length);
258 
259  
260 
261 final int w = me.getMeasuredWidth();
262 
263 final int h = me.getMeasuredHeight();
264 
265  
266 
267 // System.out.println("w=" + w + ", h=" + h);
268 
269  
270 
271 // Add each view in turn, and apply the width and height returned by
272 
273 // the SizeCallback.
274 
275 int[] dims = new int[2];
276 
277 scrollToViewPos = 0;
278 
279 for (int i = 0; i < children.length; i++) {
280 
281 sizeCallback.getViewSize(i, w, h, dims);
282 
283 children[i].setVisibility(View.VISIBLE);
284 
285 parent.addView(children[i], dims[0], dims[1]);
286 
287 if (i == 0) {
288 
289 scrollToViewPos += dims[0];
290 
291 }
292 
293 Log.d(tag, children[i] + ": w=" + dims[0] + ", h=" + dims[1]);
294 
295 Log.d(tag, "scrollToViewIdx:" + 0 + ",scrollToViewPos:"
296 
297 + scrollToViewPos);
298 
299 }
300 
301  
302 
303 // For some reason we need to post this action, rather than call
304 
305 // immediately.
306 
307 // If we try immediately, it will not scroll.
308 
309  
310 
311 new Handler().post(new Runnable() {
312 
313 @Override
314 
315 public void run() {
316 
317 me.scrollBy(scrollToViewPos, 0);
318 
319 // 因为这些控件默认都为隐藏,控件加载完成后,设置成显示
320 
321 me.setVisibility(View.VISIBLE);
322 
323 leftMenu.setVisibility(View.VISIBLE);
324 
325 }
326 
327 });
328 
329 }
330 
331 }
332 
333  
334 
335 /**
336 
337  * 点击左边按钮
338 
339  * 
340 
341  * @param leftButtonWidth
342 
343  *            左边按钮的宽度
344 
345  */
346 
347 public void clickLeftButton(int leftButtonWidth) {
348 
349 // 左边
350 
351 leftMenu.setVisibility(View.VISIBLE);
352 
353 int menuWidth = 90+leftMenu.getMeasuredWidth()
354 
355 - (leftButtonWidth + ENLARGE_WIDTH);
356 
357 System.out.println("leftmenuWidth:" + menuWidth);
358 
359 if (!leftMenuOut) {
360 
361 int left = 0;
362 
363 me.smoothScrollTo(left, 0);
364 
365 } else {
366 
367 int left = menuWidth;
368 
369 me.smoothScrollTo(left, 0);
370 
371 }
372 
373 leftMenuOut = !leftMenuOut;
374 
375 }
376 
377  
378 
379  
380 
381 @Override
382 
383 public boolean onTouchEvent(MotionEvent ev) {
384 
385 // TODO Auto-generated method stub
386 
387 // return super.onTouchEvent(ev);
388 
389 return false;
390 
391 }
392 
393  
394 
395 /**
396 
397  * Callback interface to interact with the HSV.
398 
399  */
400 
401 public interface SizeCallback {
402 
403 /**
404 
405  * Used to allow clients to measure Views before re-adding them.
406 
407  */
408 
409 public void onGlobalLayout();
410 
411  
412 
413 /**
414 
415  * Used by clients to specify the View dimensions.
416 
417  * 
418 
419  * @param idx
420 
421  *            Index of the View.
422 
423  * @param w
424 
425  *            Width of the parent View.
426 
427  * @param h
428 
429  *            Height of the parent View.
430 
431  * @param dims
432 
433  *            dims[0] should be set to View width. dims[1] should be set
434 
435  *            to View height.
436 
437  */
438 
439 public void getViewSize(int idx, int w, int h, int[] dims);
440 
441 }
442 
443 }
444 
445 这里还会有一些对当前界面滑动后的界面美观维护类:
446 
447 package grimbo.android.demo.slidingmenu;
448 
449  
450 
451 import grimbo.android.demo.slidingmenu.MyHorizontalScrollView.SizeCallback;
452 
453 import android.widget.ImageView;
454 
455  
456 
457 /**
458 
459  * Copyright (c) 2011,
460 
461  * All rights reserved.
462 
463  * 类说明
464 
465  * @author 许美镇
466 
467  * @version Revision:1.0 Date:(2012-5-22)
468 
469  * 
470 
471  */
472 
473 public class SizeCallbackForMenu implements SizeCallback {
474 
475     private ImageView leftButton;
476 
477     private int leftButtonWidth;
478 
479  
480 
481     public SizeCallbackForMenu(ImageView leftButton) {
482 
483         super();
484 
485         this.leftButton = leftButton;
486 
487     }
488 
489  
490 
491     @Override
492 
493     public void onGlobalLayout() {
494 
495      leftButtonWidth = leftButton.getMeasuredWidth()+8;
496 
497         System.out.println("leftButtonWidth=" + leftButtonWidth);
498 
499     }
500 
501  
502 
503     @Override
504 
505     public void getViewSize(int idx, int w, int h, int[] dims) {
506 
507         dims[0] = w;
508 
509         dims[1] = h;
510 
511         if (idx != 1) {
512 
513          //当视图不是中间的视图
514 
515             dims[0] = w - leftButtonWidth;
516 
517         }
518 
519     }
520 
521 }
522 
523  
524 
525  
526 
527 这里其实对于大家最重要的就是动画了,所以这里我把动画公布了:
528 
529  
530 
531 这里面的代码依次为:
532 
533  
534 
535  
536 
537 第一个:
538 
539  
540 
541 <?xml version="1.0" encoding="utf-8"?>
542 
543  
544 
545 <set xmlns:android="http://schemas.android.com/apk/res/android">
546 
547 <translate android:fromXDelta="80%p" android:toXDelta="0" android:duration="500"/>
548 
549 <!--alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /-->
550 
551 </set>
552 
553  
554 
555 第二个:
556 
557 <?xml version="1.0" encoding="utf-8"?>
558 
559  
560 
561 <set xmlns:android="http://schemas.android.com/apk/res/android">
562 
563 <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="500"/>
564 
565 <!--alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /-->
566 
567 </set>
568 
569 第三个:
570 
571 <?xml version="1.0" encoding="utf-8"?>
572 
573 <set xmlns:android="http://schemas.android.com/apk/res/android">
574 
575 <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="500"/>
576 
577 <!--alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" /-->
578 
579 </set>
580 
581 第四个:
582 
583 <?xml version="1.0" encoding="utf-8"?>
584 
585 <set xmlns:android="http://schemas.android.com/apk/res/android">
586 
587 <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="500"/>
588 
589 <!--alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /-->
590 
591 </set>
592 
593 第五个:
594 
595 <?xml version="1.0" encoding="utf-8"?>
596 
597 <set xmlns:android="http://schemas.android.com/apk/res/android">
598 
599 <translate android:fromXDelta="0" android:toXDelta="80%p" android:duration="500"/>
600 
601 <!--alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" /-->
602 
603 </
604 
605 第六个:
606 
607 <?xml version="1.0" encoding="utf-8"?>
608 
609  
610 
611 <set xmlns:android="http://schemas.android.com/apk/res/android">
612 
613 <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="500"/>
614 
615 <!--alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" /-->
616 
617 </set>

 

但是鉴于这么多的代码,毕竟很无趣嘛,我会看情况吧源码贴出来的!谢谢支持哦!有什么好的建议随便说了!

posted @ 2014-02-11 08:03  Catherine_Brain  阅读(579)  评论(0编辑  收藏  举报