Android主萤幕

Android主萤幕,第1张

概述大家好,这是我的第一篇文章,所以对我好一点!我想开发一个应用程序,您可以像在主屏幕上一样浏览7个左右的屏幕.我已经看到了一些关于此的帖子,但是我似乎无法使其正常工作,也似乎也无法对其添加评论(我敢肯定这是我的菜鸟)因此,我需要一个有效的项目,以便可以对其进行查看和分解,

大家好,这是我的第一篇文章,所以对我好一点!

我想开发一个应用程序,您可以像在主屏幕上一样浏览7个左右的屏幕.

我已经看到了一些关于此的帖子,但是我似乎无法使其正常工作,也似乎也无法对其添加评论(我敢肯定这是我的菜鸟)

因此,我需要一个有效的项目,以便可以对其进行查看和分解,并使用与我相关的位.

对此很感兴趣,但我无法使其正常工作.我使用Eclipse,而SDK是其他所有人使用的吗?

Developing an Android Homescreen

当我实现它时,我需要告诉应用程序如何启动,因为它安装了它,但说没有活动.

希望你能帮忙

Ť

我的Main.Java中有什么

package com.matthIEu.launcher;import androID.app.Activity;import androID.content.Intent;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.VIEwFlipper;public class main extends Activity {   /** Called when the activity is first created. */   @OverrIDe   public voID onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentVIEw(R.layout.main);      button onebutton = (button)findVIEwByID(R.ID.soundsone);      onebutton.setonClickListener(new OnClickListener() {         public voID onClick(VIEw v) {            Intent i = new Intent(main.this, serIEs1button.class);            startActivity(i);            overrIDePendingTransition(R.anim.push_up_in, R.anim.push_up_out);         }      });      button twobutton = (button)findVIEwByID(R.ID.soundstwo);      twobutton.setonClickListener(new OnClickListener() {         public voID onClick(VIEw v) {            Intent i = new Intent(main.this, serIEs2button.class);            startActivity(i);            overrIDePendingTransition(R.anim.push_up_in, R.anim.push_up_out);         }      });      button threebutton = (button)findVIEwByID(R.ID.soundsthree);      threebutton.setonClickListener(new OnClickListener() {         public voID onClick(VIEw v) {            Intent i = new Intent(main.this, serIEs3button.class);            startActivity(i);            overrIDePendingTransition(R.anim.push_up_in, R.anim.push_up_out);                    }      });      button fourbutton = (button)findVIEwByID(R.ID.soundsfour);      fourbutton.setonClickListener(new OnClickListener() {         public voID onClick(VIEw v) {            Intent i = new Intent(main.this, serIEs4button.class);            startActivity(i);            overrIDePendingTransition(R.anim.push_up_in, R.anim.push_up_out);         }      });      button fivebutton = (button)findVIEwByID(R.ID.soundsfive);      fivebutton.setonClickListener(new OnClickListener() {         public voID onClick(VIEw v) {            Intent i = new Intent(main.this, serIEs5button.class);            startActivity(i);            overrIDePendingTransition(R.anim.push_up_in, R.anim.push_up_out);         }      });      button sixbutton = (button)findVIEwByID(R.ID.soundssix);      sixbutton.setonClickListener(new OnClickListener() {         public voID onClick(VIEw v) {            Intent i = new Intent(main.this, serIEs6button.class);            startActivity(i);            overrIDePendingTransition(R.anim.push_up_in, R.anim.push_up_out);         }      });      button sevenbutton = (button)findVIEwByID(R.ID.soundsseven);      sevenbutton.setonClickListener(new OnClickListener() {         public voID onClick(VIEw v) {            Intent i = new Intent(main.this, serIEs7button.class);            startActivity(i);            overrIDePendingTransition(R.anim.push_up_in, R.anim.push_up_out);                    }      });      button eightbutton = (button)findVIEwByID(R.ID.soundseight);      eightbutton.setonClickListener(new OnClickListener() {         public voID onClick(VIEw v) {            Intent i = new Intent(main.this, serIEs8button.class);            startActivity(i);            overrIDePendingTransition(R.anim.push_up_in, R.anim.push_up_out);                    }      });   }}

然后Dragablespace.java

package com.matthIEu.launcher;import androID.content.Context;import androID.app.Activity;import androID.util.Log;import androID.content.res.TypedArray;import androID.util.AttributeSet;import androID.vIEw.MotionEvent;import androID.vIEw.VeLocityTracker;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.vIEw.VIEwConfiguration;import androID.Widget.Scroller;public class DragableSpace extends VIEwGroup {   private Scroller mScroller;   private VeLocityTracker mVeLocityTracker;   private int mScrollX = 0;   private int mCurrentScreen = 0;   private float mLastMotionX;   private static final String LOG_TAG = "DragableSpace";   private static final int SNAP_VELociTY = 1000;   private final static int touch_STATE_REST = 0;   private final static int touch_STATE_SCRolliNG = 1;   private int mtouchState = touch_STATE_REST;   private int mtouchSlop = 0;   public DragableSpace(Context context) {      super(context);      mScroller = new Scroller(context);      mtouchSlop = VIEwConfiguration.get(getContext()).getScaledtouchSlop();      this.setLayoutParams(new VIEwGroup.LayoutParams(                                   VIEwGroup.LayoutParams.WRAP_CONTENT,                                   VIEwGroup.LayoutParams.FILL_PARENT));   }   public DragableSpace(Context context, AttributeSet attrs) {      super(context, attrs);      mScroller = new Scroller(context);      mtouchSlop = VIEwConfiguration.get(getContext()).getScaledtouchSlop();      this.setLayoutParams(new VIEwGroup.LayoutParams(                                   VIEwGroup.LayoutParams.WRAP_CONTENT ,                                   VIEwGroup.LayoutParams.FILL_PARENT));      TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.DragableSpace);      mCurrentScreen = a.getInteger(R.styleable.DragableSpace_default_screen, 0);   }   @OverrIDe   public boolean onIntercepttouchEvent(MotionEvent ev) {      /*       * This method JUST determines whether we want to intercept the motion.       * If we return true, ontouchEvent will be called and we do the actual       * scrolling there.       */      /*       * Shortcut the most recurring case: the user is in the dragging state       * and he is moving his finger. We want to intercept this motion.       */      final int action = ev.getAction();      if ((action == MotionEvent.ACTION_MOVE) && (mtouchState != touch_STATE_REST)) {        return true;      }      final float x = ev.getX();      switch (action) {         case MotionEvent.ACTION_MOVE:            /*             * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check             * whether the user has moved far enough from his original down touch.             */            /*             * Locally do absolute value. mLastMotionX is set to the y value             * of the down event.             */            final int xDiff = (int) Math.abs(x - mLastMotionX);            boolean xMoved = xDiff > mtouchSlop;            if (xMoved) {                // Scroll if the user moved far enough along the X axis                mtouchState = touch_STATE_SCRolliNG;            }            break;        case MotionEvent.ACTION_DOWN:            // Remember location of down touch            mLastMotionX = x;            /*             * If being flinged and user touches the screen, initiate drag;             * otherwise don't.  mScroller.isFinished should be false when             * being flinged.             */            mtouchState = mScroller.isFinished() ? touch_STATE_REST : touch_STATE_SCRolliNG;            break;        case MotionEvent.ACTION_CANCEL:        case MotionEvent.ACTION_UP:            // Release the drag            mtouchState = touch_STATE_REST;            break;    }    /*     * The only time we want to intercept motion events is if we are in the     * drag mode.     */    return mtouchState != touch_STATE_REST;}@OverrIDepublic boolean ontouchEvent(MotionEvent event) {    if (mVeLocityTracker == null) {        mVeLocityTracker = VeLocityTracker.obtain();    }    mVeLocityTracker.addMovement(event);    final int action = event.getAction();    final float x = event.getX();    switch (action) {        case MotionEvent.ACTION_DOWN:            Log.i(LOG_TAG, "event : down");            /*             * If being flinged and user touches, stop the fling. isFinished             * will be false if being flinged.             */            if (!mScroller.isFinished()) {                mScroller.abortAnimation();            }            // Remember where the motion event started            mLastMotionX = x;            break;        case MotionEvent.ACTION_MOVE:            // Log.i(LOG_TAG,"event : move");            // if (mtouchState == touch_STATE_SCRolliNG) {            // Scroll to follow the motion event            final int deltaX = (int) (mLastMotionX - x);            mLastMotionX = x;            //Log.i(LOG_TAG, "event : move, deltaX " + deltaX + ", mScrollX " + mScrollX);            if (deltaX < 0) {                if (mScrollX > 0) {                    scrollBy(Math.max(-mScrollX, deltaX), 0);                }            } else if (deltaX > 0) {                final int availabletoScroll = getChildAt(getChildCount() - 1)                    .getRight()                    - mScrollX - getWIDth();                if (availabletoScroll > 0) {                    scrollBy(Math.min(availabletoScroll, deltaX), 0);                }            }            // }            break;        case MotionEvent.ACTION_UP:            Log.i(LOG_TAG, "event : up");            // if (mtouchState == touch_STATE_SCRolliNG) {            final VeLocityTracker veLocityTracker = mVeLocityTracker;            veLocityTracker.computeCurrentVeLocity(1000);            int veLocityX = (int) veLocityTracker.getXVeLocity();            if (veLocityX > SNAP_VELociTY && mCurrentScreen > 0) {                // Fling hard enough to move left                snapToScreen(mCurrentScreen - 1);            } else if (veLocityX < -SNAP_VELociTY                    && mCurrentScreen < getChildCount() - 1) {                // Fling hard enough to move right                snapToScreen(mCurrentScreen + 1);            } else {                snapToDestination();            }            if (mVeLocityTracker != null) {                mVeLocityTracker.recycle();                mVeLocityTracker = null;            }            // }            mtouchState = touch_STATE_REST;            break;        case MotionEvent.ACTION_CANCEL:            Log.i(LOG_TAG, "event : cancel");            mtouchState = touch_STATE_REST;    }    mScrollX = this.getScrollX();    return true;}private voID snapToDestination() {    final int screenWIDth = getWIDth();    final int whichScreen = (mScrollX + (screenWIDth / 2)) / screenWIDth;    Log.i(LOG_TAG, "from des");    snapToScreen(whichScreen);}public voID snapToScreen(int whichScreen) {             Log.i(LOG_TAG, "snap To Screen " + whichScreen);    mCurrentScreen = whichScreen;    final int newX = whichScreen * getWIDth();    final int delta = newX - mScrollX;    mScroller.startScroll(mScrollX, 0, delta, 0, Math.abs(delta) * 2);                 invalIDate();}public voID setToScreen(int whichScreen) {    Log.i(LOG_TAG, "set To Screen " + whichScreen);    mCurrentScreen = whichScreen;    final int newX = whichScreen * getWIDth();    mScroller.startScroll(newX, 0, 0, 0, 10);                 invalIDate();}@OverrIDeprotected voID onLayout(boolean changed, int l, int t, int r, int b) {    int childleft = 0;    final int count = getChildCount();    for (int i = 0; i < count; i++) {        final VIEw child = getChildAt(i);        if (child.getVisibility() != VIEw.GONE) {            final int chilDWIDth = child.getMeasureDWIDth();            child.layout(childleft, 0, childleft + chilDWIDth, child                    .getMeasuredHeight());            childleft += chilDWIDth;        }    }}@OverrIDeprotected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) {    super.onMeasure(wIDthMeasureSpec, heightmeasureSpec);    final int wIDth = MeasureSpec.getSize(wIDthMeasureSpec);    final int wIDthMode = MeasureSpec.getMode(wIDthMeasureSpec);    if (wIDthMode != MeasureSpec.EXACTLY) {        throw new IllegalStateException("error mode.");    }    final int heightmode = MeasureSpec.getMode(heightmeasureSpec);    if (heightmode != MeasureSpec.EXACTLY) {        throw new IllegalStateException("error mode.");    }    // The children are given the same wIDth and height as the workspace    final int count = getChildCount();    for (int i = 0; i < count; i++) {        getChildAt(i).measure(wIDthMeasureSpec, heightmeasureSpec);    }    Log.i(LOG_TAG, "moving to screen "+mCurrentScreen);    scrollTo(mCurrentScreen * wIDth, 0);      }  @OverrIDepublic voID computeScroll() {    if (mScroller.computeScrollOffset()) {        mScrollX = mScroller.getCurrX();        scrollTo(mScrollX, 0);        postInvalIDate();    }}}

然后我的main.xml布局

<?xml version="1.0" enCoding="utf-8"?><com.matthIEu.launcher.DragableSpace xmlns:app="http://schemas.androID.com/apk/res/com.matthIEu.launcher" xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/space" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" app:default_screen="1">   <include androID:ID="@+ID/left" layout="@layout/left_screen" />   <include androID:ID="@+ID/center" layout="@layout/initial_screen" />   <include androID:ID="@+ID/right" layout="@layout/right_screen" /></com.matthIEu.launcher.DragableSpace>

我还有3个其他的布局文件,分别称为initial,left和right(_screen)

解决方法:

您需要编辑清单,以便应用程序知道首次启动时要使用哪个活动.

  <application>      <activity androID:name=".YourActivityname" androID:label="Your Label">        <intent-filter>            <action androID:name="androID.intent.action.MAIN"/>            <category androID:name="androID.intent.category.LAUNCHER"/>        </intent-filter>    </activity>  </application>
总结

以上是内存溢出为你收集整理的Android主萤幕全部内容,希望文章能够帮你解决Android主萤幕所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1083712.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存