当前位置: 代码迷 >> Android >> android 幕滑动成效
  详细解决方案

android 幕滑动成效

热度:100   发布时间:2016-05-01 20:53:01.0
android 幕滑动效果

MainActivity.java  //主入口类


package com.myviewgroup.chen;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
private MyViewGroup viewGroup;
private PageControlView pageControl;
private LinearLayout linearLayout3 ;
private LinearLayout linearLayout2;
private LinearLayout linearLayout ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

viewGroup = (MyViewGroup) this.findViewById(R.id.homemenuMyViewGroup);
pageControl = (PageControlView) findViewById(R.id.homemenuPageControl);

viewGroup.removeAllViews();

//这里加几个页面就显示几个画面和几个点
viewGroup.addView(View.inflate(MainActivity.this, R.layout.myview1, null));
viewGroup.addView(View.inflate(MainActivity.this, R.layout.myview2, null));
viewGroup.addView(View.inflate(MainActivity.this, R.layout.myview3, null));



linearLayout  = (LinearLayout) View.inflate(this, R.layout.myview1, null);
linearLayout2  = (LinearLayout) View.inflate(this, R.layout.myview2, null);
linearLayout3  = (LinearLayout) View.inflate(this, R.layout.myview3, null);
viewGroup.setCurrentScreenIndex(1);
pageControl.setCount(viewGroup.getChildCount());
pageControl.generatePageControl(1);
viewGroup.setScrollToScreenCallback(pageControl);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
System.out.println();
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
if (linearLayout.isFocused()) {
viewGroup.scrollToScreen(0, 200);
} else if (linearLayout2.isFocused()) {
viewGroup.scrollToScreen(1, 200);

}else if(linearLayout3.isFocused()){
viewGroup.scrollToScreen(2, 200);
}
}

return super.onKeyDown(keyCode, event);
}
}




MyViewGroup.java    //主页面的实现继承ViewGroup

package com.myviewgroup.chen;


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


public class MyViewGroup extends ViewGroup {


private static final String TAG = "scroller";


private Scroller scroller;


private int currentScreenIndex;


private GestureDetector gestureDetector;


private ScrollToScreenCallback scrollToScreenCallback;


public void setScrollToScreenCallback(
ScrollToScreenCallback scrollToScreenCallback) {
this.scrollToScreenCallback = scrollToScreenCallback;
}



private boolean fling;


public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}


public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}


public MyViewGroup(Context context) {
super(context);
initView(context);

}


private void initView(final Context context) {
this.scroller = new Scroller(context);


this.gestureDetector = new GestureDetector(new OnGestureListener() {



public boolean onSingleTapUp(MotionEvent e) {
return false;
}



public void onShowPress(MotionEvent e) {
}



public 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;
}



public void onLongPress(MotionEvent e) {
}



public 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;
}



public boolean onDown(MotionEvent e) {
return false;
}
});


}


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


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


@Override
public 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
*/
public 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 (scrollToScreenCallback != null) {
scrollToScreenCallback
.callback(currentScreenIndex);
}
}

public void scrollToScreen(int whichScreen, int duration) {
if (getFocusedChild() != null && whichScreen != currentScreenIndex
&& getFocusedChild() == getChildAt(currentScreenIndex)) {
getFocusedChild().clearFocus();
}
final int delta = whichScreen * getWidth() - getScrollX();
scroller.startScroll(getScrollX(), 0, delta, 0, duration);
invalidate();


currentScreenIndex = whichScreen;
if (scrollToScreenCallback != null) {
scrollToScreenCallback
.callback(currentScreenIndex);
}
}


/**
*/
private void snapToDestination() {
scrollToScreen((getScrollX() + (getWidth() / 2)) / getWidth());
}


interface ScrollToScreenCallback {
public void callback(int currentIndex);
}

public void setCurrentScreenIndex(int currentScreenIndex) {   
       this.currentScreenIndex = currentScreenIndex;   
}   


public int getCurrentScreenIndex() {   
       return this.currentScreenIndex;   
}  
}


// 控制点点的实现

PageControlView.java


package com.myviewgroup.chen;


import com.myviewgroup.chen.MyViewGroup.ScrollToScreenCallback;


import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class PageControlView extends LinearLayout implements
ScrollToScreenCallback {


private int count;


private Context context;


public void setCount(int count) {
this.count = count;
}


public PageControlView(Context context, AttributeSet attrs) {
super(context, attrs);
this.init(context);
}


public PageControlView(Context context) {
super(context);
this.init(context);
}


private void init(Context context) {
this.context=context;
}



public void callback(int currentIndex) {
generatePageControl(currentIndex);
}


public void generatePageControl(int currentIndex) {
this.removeAllViews();


LayoutParams mParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mParams.rightMargin=45;

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, mParams);
}
}
}


主界面 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
<LinearLayout android:id="@+id/home_menu_layout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center" android:orientation="vertical">
<com.myviewgroup.chen.MyViewGroup
android:layout_marginTop="1px" android:id="@+id/homemenuMyViewGroup"
android:layout_width="fill_parent" android:layout_height="400px"
android:gravity="center" />
<com.myviewgroup.chen.PageControlView
android:id="@+id/homemenuPageControl" android:layout_width="fill_parent"
android:layout_height="50px" android:layout_alignParentBottom="true"
android:gravity="center" />
</LinearLayout>
</LinearLayout>


//加入的页面

myview1.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"
    android:background="#778899" >
    

</LinearLayout>

myview1.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"
    android:background="#FF0000" >


</LinearLayout>http://my.csdn.net/uploads/201206/21/1340274651_4667.png " _xhe_src="http://my.csdn.net/uploads/201206/21/1340274651_4667.png "/><img src="<font><font class=" "="">http://my.csdn.net/uploads/201206/21/1340274651_4667.png " _xhe_src="http://my.csdn.net/uploads/201206/21/1340274651_4667.png "/>

myview1.xml

<img src="<font><font class=" "="">http://my.csdn.net/uploads/201206/21/1340274651_4667.png " _xhe_src="http://my.csdn.net/uploads/201206/21/1340274651_4667.png "/>

  相关解决方案