Android仿Launcher效果

來源:互聯網
上載者:User

效果1

 

效果2

 

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.cn.npass.nj"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="10" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:label="@string/app_name"            android:name=".MainActivity" >            <intent-filter >                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

 

 

scroll_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/myView"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_weight="1" >    <com.cn.npass.nj.ScrollViewGroup        android:id="@+id/scrollViewGroup"        android:layout_width="fill_parent"        android:layout_height="fill_parent" />    <com.cn.npass.nj.PageControlView        android:id="@+id/pageControl"        android:layout_width="fill_parent"        android:layout_height="80px"        android:layout_alignParentBottom="true"        android:background="#8f00000f"        android:gravity="center" /></RelativeLayout>

 

 

scroll_layout_0.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="請輸入姓名:" />    <EditText        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="" />    <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="提交" /></LinearLayout>

 

 

MainActivity.java

package com.cn.npass.nj;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.ImageView;/** */public class MainActivity extends Activity {@SuppressWarnings("unused")private static final String TAG = "scroller";private ScrollViewGroup viewGroup;private PageControlView pageControl;private int[] argImages = {R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4};@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.scroll_main);viewGroup = (ScrollViewGroup) findViewById(R.id.scrollViewGroup);ImageView imageView = new ImageView(this);imageView.setImageDrawable(getResources().getDrawable(R.drawable.a1));viewGroup.addView(imageView);viewGroup.addView(View.inflate(this, R.layout.scroll_layout_0, null));imageView = new ImageView(this);imageView.setImageDrawable(getResources().getDrawable(R.drawable.a1));viewGroup.addView(imageView);imageView = new ImageView(this);imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));viewGroup.addView(imageView);for (int i = 0; i < argImages.length; i++) {imageView = new ImageView(this);imageView.setImageDrawable(getResources().getDrawable(argImages[i]));viewGroup.addView(imageView);}// 設定圖片填充ImageView//imageView.setScaleType(ScaleType.CENTER);viewGroup.setCurrentScreenIndex(0);pageControl = (PageControlView) findViewById(R.id.pageControl);pageControl.bindScrollViewGroup(viewGroup);}}

PageControlView.java

package com.cn.npass.nj;import android.content.Context;import android.util.AttributeSet;import android.widget.ImageView;import android.widget.LinearLayout;import com.cn.npass.nj.ScrollViewGroup.OnScreenChangeListener;/** */public class PageControlView extends LinearLayout {private Context context;private int count;public void bindScrollViewGroup(ScrollViewGroup scrollViewGroup) {this.count = scrollViewGroup.getChildCount();generatePageControl(scrollViewGroup.getCurrentScreenIndex());scrollViewGroup.setOnScreenChangeListener(new OnScreenChangeListener() {@Overridepublic void onScreenChange(int currentIndex) {generatePageControl(currentIndex);}});}public PageControlView(Context context) {super(context);this.init(context);}public PageControlView(Context context, AttributeSet attrs) {super(context, attrs);this.init(context);}private void init(Context context) {this.context = context;}private void generatePageControl(int currentIndex) {this.removeAllViews();for (int i = 0; i < this.count; i++) {ImageView imageView = new ImageView(context);if (currentIndex == i) {imageView.setImageResource(R.drawable.page_indicator_focused);} else {imageView.setImageResource(R.drawable.page_indicator);}this.addView(imageView);}}}

 

ScrollViewGroup.java

package com.cn.npass.nj;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.GestureDetector;import android.view.GestureDetector.OnGestureListener;import android.view.MotionEvent;import android.view.View;import android.view.ViewConfiguration;import android.view.ViewGroup;import android.widget.Scroller;/** */public class ScrollViewGroup extends ViewGroup {private static final String TAG = "scroller";private Scroller scroller;private int currentScreenIndex;//當前屏的索引public int getCurrentScreenIndex() {return currentScreenIndex;}public void setCurrentScreenIndex(int currentScreenIndex) {this.currentScreenIndex = currentScreenIndex;}private GestureDetector gestureDetector;// 設定一個標誌位,防止底層的onTouch事件重複處理UP事件private boolean fling;public ScrollViewGroup(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);initView(context);}public ScrollViewGroup(Context context, AttributeSet attrs) {super(context, attrs);initView(context);}public ScrollViewGroup(Context context) {super(context);initView(context);}private void initView(final Context context) {this.scroller = new Scroller(context);this.gestureDetector = new GestureDetector(new OnGestureListener() {@Overridepublic boolean onSingleTapUp(MotionEvent e) {return false;}@Overridepublic void onShowPress(MotionEvent e) {}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) {if ((distanceX > 0 && currentScreenIndex < getChildCount() - 1)// 防止移動過最後一頁|| (distanceX < 0 && getScrollX() > 0)) {// 防止向第一頁之前移動scrollBy((int) distanceX, 0);}return true;}@Overridepublic void onLongPress(MotionEvent e) {}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2,float velocityX, float velocityY) {Log.d(TAG, "min velocity >>>"+ ViewConfiguration.get(context).getScaledMinimumFlingVelocity()+ " current velocity>>" + velocityX);// 判斷是否達到最小輕鬆速度,取絕對值的if (Math.abs(velocityX) > ViewConfiguration.get(context).getScaledMinimumFlingVelocity()) {if (velocityX > 0 && currentScreenIndex > 0) {// 手指從左往右劃Log.d(TAG, ">>>>fling to left");fling = true;scrollToScreen(currentScreenIndex - 1);} else if (velocityX < 0&& currentScreenIndex < getChildCount() - 1) {Log.d(TAG, ">>>>fling to right");fling = true;scrollToScreen(currentScreenIndex + 1);}}return true;}@Overridepublic boolean onDown(MotionEvent e) {return false;}});}@Overrideprotected void onLayout(boolean changed, int left, int top, int right,int bottom) {Log.d(TAG, ">>left: " + left + " top: " + top + " right: " + right+ " bottom:" + bottom);/** * 設定布局,將子視圖順序橫屏排列 */for (int i = 0; i < getChildCount(); i++) {View child = getChildAt(i);child.setVisibility(View.VISIBLE);child.measure(right - left, bottom - top);child.layout(0 + i * getWidth(), 0, getWidth() + i * getWidth(),getHeight());}// 初始化顯示第幾個介面int delta = currentScreenIndex * getWidth() - getScrollX();scroller.startScroll(getScrollX(), 0, delta, 0, 0);invalidate();}@Overridepublic void computeScroll() {if (scroller.computeScrollOffset()) {scrollTo(scroller.getCurrX(), 0);postInvalidate();}}@Overridepublic boolean onTouchEvent(MotionEvent event) {gestureDetector.onTouchEvent(event);switch (event.getAction()) {case MotionEvent.ACTION_DOWN:break;case MotionEvent.ACTION_MOVE:break;case MotionEvent.ACTION_UP:if (!fling) {snapToDestination();}fling = false;break;default:break;}return true;}/** * 切換到指定屏 *  * @param whichScreen */private void scrollToScreen(int whichScreen) {if (getFocusedChild() != null && whichScreen != currentScreenIndex&& getFocusedChild() == getChildAt(currentScreenIndex)) {getFocusedChild().clearFocus();}final int delta = whichScreen * getWidth() - getScrollX();scroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta) * 2);invalidate();currentScreenIndex = whichScreen;if (onScreenChangeListener != null) {onScreenChangeListener.onScreenChange(currentScreenIndex);}}/** * 根據當前x座標位置確定切換到第幾屏 */private void snapToDestination() {scrollToScreen((getScrollX() + (getWidth() / 2)) / getWidth());}public interface OnScreenChangeListener {void onScreenChange(int currentIndex);}private OnScreenChangeListener onScreenChangeListener;public void setOnScreenChangeListener(OnScreenChangeListener onScreenChangeListener) {this.onScreenChangeListener = onScreenChangeListener;}}

 

資源為:http://download.csdn.net/detail/niejing654092427/4500937

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.