当前位置:编程学习 > wap >>

有点顶不住了,求launcher的两个问题

需要做两个launcher的效果,版本是2.3的

一.桌面显示的两个view之间要加个空隙,我想了好多方法都不行,有点晕了.

二.滑动桌面到最左边的时候,还能再向右滑出来一些,最右边也是一样

求解决思路,如果谁有范例代码能提供下就更好了,

卡在这里好几天了,无奈上网求助,谢谢大家. --------------------编程问答-------------------- 没人关注? --------------------编程问答-------------------- 都和launcher的计算有关
1、新出现的一页 把空隙算进去
2、取消对最左面和最有面的判断 --------------------编程问答-------------------- 提供些资料:
http://www.cnblogs.com/playing/archive/2011/04/22/2024980.html --------------------编程问答-------------------- mark 一下

一.桌面显示的两个view之间要加个空隙,我想了好多方法都不行,有点晕了.

你指的空隙是多大?如果只要几个像素,将两个view的background 设置成9-patch的,并且这个9-patch周围有几个透明的像素。
类似于两个button之间空隙的效果。 --------------------编程问答-------------------- 关于luancher我是自己做了一个滑动控件,当到最左边或最右边的时候做一下判断就OK了。 --------------------编程问答-------------------- 一:第一点很好解决啊。在你的view外套一个layout 然后设置Layout margin XX就可以了

二.滑动桌面到最左边的时候,还能再向右滑出来一些,最右边也是一样
   第2点本来就是这样啊大哥。。。
其实你的问题真的都很简单。。。
代码给你贴下吧:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical"
  android:background="@color/White">
  <LinearLayout android:layout_width="fill_parent" 
      android:layout_height="300dip" 
      android:orientation="vertical">
</LinearLayout>
<LinearLayout android:layout_width="fill_parent" 
  android:layout_height="fill_parent"
  android:layout_marginLeft="15dip"
  android:layout_marginRight="15dip"
  android:layout_marginBottom="20dip"
  android:background="@drawable/view_yuan_morelist">
<com.renrenwei.view.ScrollLayout android:id="@+id/ScrollLayoutTest"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
这里就是一个view第一点你只要加上Layout margin XX
<LinearLayout android:layout_width="fill_parent"
     android:layout_height="fill_parent">
     <ImageView android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:src="@drawable/one"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
     android:layout_height="fill_parent">
      <ImageView android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/two"/>
   </LinearLayout>
<LinearLayout android:layout_width="fill_parent"
     android:layout_height="fill_parent">
      <ImageView android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/three"/>
   </LinearLayout>
</com.renrenwei.view.ScrollLayout>
</LinearLayout>
</LinearLayout>



package com.renrenwei.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.Scroller;

public class ScrollLayout extends ViewGroup {

private static final String TAG = "ScrollLayout";
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
private int mCurScreen;
private int mDefaultScreen = 0;
private static final int TOUCH_STATE_REST = 0;
private static final int TOUCH_STATE_SCROLLING = 1;
private static final int SNAP_VELOCITY = 600;
private int mTouchState = TOUCH_STATE_REST;
private int mTouchSlop;
private float mLastMotionX;
private float mLastMotionY;

public ScrollLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public ScrollLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mScroller = new Scroller(context);
mCurScreen = mDefaultScreen;
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (changed) {
int childLeft = 0;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View childView = getChildAt(i);
if (childView.getVisibility() != View.GONE) {
final int childWidth = childView.getMeasuredWidth();
childView.layout(childLeft, 0, childLeft + childWidth,
childView.getMeasuredHeight());
childLeft += childWidth;
}
}
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.e(TAG, "onMeasure");
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int width = MeasureSpec.getSize(widthMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException(
"ScrollLayout only canmCurScreen run at EXACTLY mode!");
}
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException(
"ScrollLayout only can run at EXACTLY mode!");
}
final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}
scrollTo(mCurScreen * width, 0);
}

public void snapToDestination() {
final int screenWidth = getWidth();
final int destScreen = (getScrollX() + screenWidth / 2) / screenWidth;
snapToScreen(destScreen);
}

public void snapToScreen(int whichScreen) {
whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
if (getScrollX() != (whichScreen * getWidth())) {
final int delta = whichScreen * getWidth() - getScrollX();
mScroller.startScroll(getScrollX(), 0, delta, 0,
Math.abs(delta) * 2);
mCurScreen = whichScreen;
invalidate(); // Redraw the layout
}
}

public void setToScreen(int whichScreen) {
whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
mCurScreen = whichScreen;
scrollTo(whichScreen * getWidth(), 0);
}

public int getCurScreen() {
return mCurScreen;
}

@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}

@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();
final float y = event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG, "event down!");
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
mLastMotionX = x;
break;
case MotionEvent.ACTION_MOVE:
int deltaX = (int) (mLastMotionX - x);
mLastMotionX = x;
scrollBy(deltaX, 0);
break;
case MotionEvent.ACTION_UP:
Log.e(TAG, "event : up");
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000);
int velocityX = (int) velocityTracker.getXVelocity();
Log.e(TAG, "velocityX:" + velocityX);
if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
// Fling enough to move left
Log.e(TAG, "snap left");
snapToScreen(mCurScreen - 1);

} else if (velocityX < -SNAP_VELOCITY
&& mCurScreen < getChildCount() - 1) {
// Fling enough to move right
Log.e(TAG, "snap right");
snapToScreen(mCurScreen + 1);
} else {
snapToDestination();
}
// if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
//
// // Fling enough to move left
//
// Log.e(TAG, "snap left");
// if (mCurScreen > getChildCount() - 1) {
// View v1 = getChildAt(0);
// View v2 = getChildAt(1);
// View v3 = getChildAt(2);
//
// }
// snapToScreen(mCurScreen - 1);
//
// } else if (velocityX < -SNAP_VELOCITY
//
// && mCurScreen < getChildCount() - 1) {
//
// // Fling enough to move right
//
// Log.e(TAG, "snap right");
//
// snapToScreen(mCurScreen + 1);
//
// } else {
//
// snapToDestination();
//
// }
//
// if (mVelocityTracker != null) {
//
// mVelocityTracker.recycle();
//
// mVelocityTracker = null;
//
// }
// }
mTouchState = TOUCH_STATE_REST;
break;
case MotionEvent.ACTION_CANCEL:
mTouchState = TOUCH_STATE_REST;
break;
}
return true;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
Log.e(TAG, "onInterceptTouchEvent-slop:" + mTouchSlop + "; action = "
+ ev.getAction());
final int action = ev.getAction();
if ((action == MotionEvent.ACTION_MOVE)
&& (mTouchState != TOUCH_STATE_REST)) {
Log.e(TAG, "onInterceptTouchEvent-action:" + action);
return true;
}
final float x = ev.getX();
final float y = ev.getY();
switch (action) {
case MotionEvent.ACTION_MOVE:

final int xDiff = (int) Math.abs(mLastMotionX - x);
Log.e(TAG, "onInterceptTouchEvent-move: xDiff = " + xDiff
+ "; mLastMotionX = " + mLastMotionX);
if (xDiff > mTouchSlop) {

mTouchState = TOUCH_STATE_SCROLLING;
}
break;
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
Log.e(TAG, "onInterceptTouchEvent-down: x = " + x + "; y = " + y);
mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST
: TOUCH_STATE_SCROLLING;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
Log.e(TAG, "onInterceptTouchEvent-up: mLastMotionX = "
+ mLastMotionX);
mTouchState = TOUCH_STATE_REST;
break;
}
return mTouchState != TOUCH_STATE_REST;
}
}


--------------------编程问答-------------------- 都很简单的 ,我的qq  81553652   --------------------编程问答-------------------- 泪奔啊,内容还没看,先感谢所有回复的同学!!! --------------------编程问答-------------------- 一:第一点很好解决啊。在你的view外套一个layout 然后设置Layout margin XX就可以了

二.滑动桌面到最左边的时候,还能再向右滑出来一些,最右边也是一样
  第2点本来就是这样啊大哥。。。
其实你的问题真的都很简单。。。
代码给你贴下吧:
 
--------------------编程问答-------------------- 9楼的是错误的
你熟悉 Launcher吗?其实是有变量来控制的 ,改改就可以了啊 --------------------编程问答-------------------- <?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<com.android.launcher3.CellLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher3"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    launcher:cellWidth="80dip"
    launcher:cellHeight="103dip"
    launcher:longAxisStartPadding="20dip"
    launcher:longAxisEndPadding="68dip"
    launcher:shortAxisStartPadding="0dip"
    launcher:shortAxisEndPadding="0dip"
    launcher:shortAxisCells="4"
    launcher:longAxisCells="4" />


看看这个吧 workspace_screen.xml --------------------编程问答-------------------- 关注下 --------------------编程问答-------------------- 第二个问题 你看看   com.android.launcher3.Workspace.onTouchEvent(MotionEvent)   中的   availableToScroll ,相信你看了这个 就知道怎么做了   --------------------编程问答-------------------- 看到此贴,讨论深度定制的内容,非常想知道android该按照怎样的步骤学习呢

--------------------编程问答-------------------- 也做android快3个月了,学习方法就是项目中用到啥就学啥,感觉这样很无序 --------------------编程问答-------------------- 非常好的话题,我也来学习一下 --------------------编程问答--------------------
引用 10 楼 hehuihehui 的回复:
9楼的是错误的
你熟悉 Launcher吗?其实是有变量来控制的 ,改改就可以了啊


6楼的回复完全可以好吗。我项目就是这么做的。 --------------------编程问答--------------------
引用 17 楼 peijiangping1989 的回复:
引用 10 楼 hehuihehui 的回复:

9楼的是错误的
你熟悉 Launcher吗?其实是有变量来控制的 ,改改就可以了啊


6楼的回复完全可以好吗。我项目就是这么做的。
 那你且不是把Launcher的代码改了好多了啊 --------------------编程问答-------------------- 进度:

第一点
使用了6楼peijiangping1989的第一点的方法,似乎有点问题,也许是我搞错了,还没有成功

第二点
直接将peijiangping1989代码中的onTouchEvent中的case MotionEvent.ACTION_MOVE:下的程序块拷贝到我的对应的代码中,成功!!!  感激不尽啊!!!

顺便问下该怎么给分?

--------------------编程问答-------------------- 进度:

第一点,我尝试了下6楼peijiangping1989的方法,还没成功,也许是我没搞对

第二点,我直接将peijiangping1989代码中的onTouchEvent中case MotionEvent.ACTION_MOVE:下面的程序块拷贝到我对应的代码中,成功了!!!感激不尽啊!!!

最后问一下怎么给人加分?本人还是csdn的新手. --------------------编程问答-------------------- 早上了,先顶起来 --------------------编程问答-------------------- 按照我给你的方法啊 
你们公司也做Launcher 啊 ? --------------------编程问答-------------------- 郁闷,第一个问题还不行,我也没说的很清楚,我的桌面的每一个view都有一个底图,底图的目的是给每个view都加一个边框,边框在静止下是看不到的,滑动时才能看见,所以我修改了onMeasure函数和onLayout,桌面只显示边框内的view,现在又要在两个view的边框之间加上一个可以看见的间隔,我尝试了嵌套layout的方法,结果程序崩溃了,前面有人说可以用计算的方法,不知能不能说详细一点? --------------------编程问答-------------------- 自己研究吧 这个我以前都做过的 --------------------编程问答-------------------- 问题已经解决了,大概说下第一点

通过计算每个桌面view的left位置,增加view之间的间隔,然后修改默认滚屏的滑动值,就可以实现此功能

感谢大家
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,