ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android主萤幕

2019-11-02 23:24:47  阅读:300  来源: 互联网

标签:viewflipper launcher android


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

我想开发一个应用程序,您可以像在主屏幕上一样浏览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;
}

@Override
public 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();
}

@Override
protected 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;
        }
    }

}

@Override
protected 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);      
}  

@Override
public 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>

标签:viewflipper,launcher,android
来源: https://codeday.me/bug/20191102/1995090.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有