『壹』 android怎麼實現滑動效果
工具/原料
ViewFlipper類和OnGestureListener介面
animation動畫效果translate和alpha
方法/步驟
1、設置布局文件,其中使用到ViewFlipper控制項,內容如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_optopns_vertical_margin"
android:paddingLeft="@dimen/activity_options_horizontal_margin"
android:paddingRight="@dimen/activity_options_horizontal_margin"
android:paddingTop="@dimen/activity_optopns_vertical_margin"
tools:context=".ImageFlipperActivity" >
<RelativeLayout
android:id="@id/rl_image_flipper_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_image_flipper_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp"
android:background="@drawable/custom_button"
android:text="@string/back"
android:textColor="@color/textColor"
android:textSize="16sp"
android:visibility="visible" />
<TextView
android:id="@id/tv_image_flipper_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/image_flipper"
android:textColor="@color/textColor"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
<LinearLayout
android:id="@id/ll_image_flipper_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/rl_image_flipper_title"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="vertical" >
<ViewFlipper
android:id="@id/vf_image_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
2、從左邊進入的動畫文件,其內容如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- translate:畫面轉換位置移動動畫效果 -->
<translate
android:ration="500"
android:fromXDelta="100%p"
android:toXDelta="0" />
<!-- alpha:漸變透明度動畫效果 -->
<alpha
android:ration="500"
android:fromAlpha="0.1"
android:toAlpha="1.0" />
</set>
3、從左邊退出的動畫文件,其內容如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:ration="500"
android:fromXDelta="0"
android:toXDelta="-100%p" />
<alpha
android:ration="500"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
4、從右邊進入的動畫文件,其內容如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:ration="500"
android:fromXDelta="-100%p"
android:toXDelta="0" />
<alpha
android:ration="500"
android:fromAlpha="0.1"
android:toAlpha="1.0" />
</set>
5、從右邊退出的動畫文件,其內容如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:ration="500"
android:fromXDelta="0"
android:toXDelta="100%p" />
<alpha
android:ration="500"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
6、載入布局文件和動畫文件的類,其源碼內容為:
/**
*
*/
package com.i114gbox.aglieguy;
import android.content.Context;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ViewFlipper;
import com.i114gbox.sdk.activity.I114gBoxActivity;
import com.i114gbox.sdk.utils.I114gBoxCollectActivityUtils;
import com.i114gbox.sdk.utils.I114gBoxLogUtils;
import com.i114gbox.sdk.utils.I114gBoxResourceUtils;
/**
* 圖片滑動Activity
*
* @author SJC
*
*/
public class ImageFlipperActivity extends I114gBoxActivity implements
OnGestureListener {
private static String TAG = "ImageFlipperActivity";
private Context ctx = null;
private GestureDetector gestureDetector;// 手勢監聽
private ViewFlipper viewFlipper;// 視圖輪播
@Override
protected void onCreate(Bundle savedInstanceState) {
I114gBoxLogUtils.d(TAG, "The onCreate method execute.");
super.onCreate(savedInstanceState);
I114gBoxCollectActivityUtils.getInstance().addActivity(this);// 收集Activity
ctx = this;
setContentView(I114gBoxResourceUtils.getLayoutId(ctx,
"activity_image_flipper"));
gestureDetector = new GestureDetector(this);
viewFlipper = (ViewFlipper) findViewById(I114gBoxResourceUtils.getId(
ctx, "vf_image_flipper"));
viewFlipper.addView(addImageView(I114gBoxResourceUtils.getDrawableId(
ctx, "flipper_01")));
viewFlipper.addView(addImageView(I114gBoxResourceUtils.getDrawableId(
ctx, "flipper_02")));
viewFlipper.addView(addImageView(I114gBoxResourceUtils.getDrawableId(
ctx, "flipper_03")));
viewFlipper.addView(addImageView(I114gBoxResourceUtils.getDrawableId(
ctx, "flipper_04")));
viewFlipper.addView(addImageView(I114gBoxResourceUtils.getDrawableId(
ctx, "flipper_05")));
viewFlipper.addView(addImageView(I114gBoxResourceUtils.getDrawableId(
ctx, "flipper_06")));
// viewFlipper.addView(addImageView(R.drawable.one));
// viewFlipper.addView(addImageView(R.drawable.two));
// viewFlipper.addView(addImageView(R.drawable.three));
// viewFlipper.addView(addImageView(R.drawable.four));
// viewFlipper.addView(addImageView(R.drawable.five));
Button backButton = (Button) findViewById(I114gBoxResourceUtils.getId(
ctx, "btn_image_flipper_back"));
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
/** 添加ImageView控制項 **/
private View addImageView(int id) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(id);
return imageView;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
I114gBoxLogUtils.d(TAG, "The onTouchEvent method execute.");
return gestureDetector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
I114gBoxLogUtils.d(TAG, "The onDown method execute.");
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
I114gBoxLogUtils.d(TAG, "The onFling method execute.");
I114gBoxLogUtils.i(TAG, "e1.x:" + e1.getX() + "|" + "e2.x:" + e2.getX()
+ "|" + "velocityX:" + velocityX + "|" + "velocityY:"
+ velocityY);
if (e1.getX() - e2.getX() > 120) {
this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(ctx,
R.anim.push_left_in));// 進入屏幕的動畫
this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(ctx,
R.anim.push_left_out));// 離開屏幕的動畫
this.viewFlipper.showNext();// 手動顯示下一個視圖
return true;
} else if (e1.getX() - e2.getX() < -120) {
this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(ctx,
R.anim.push_right_in));
this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(ctx,
R.anim.push_right_out));
this.viewFlipper.showPrevious();// 手動顯示前一個視圖
return true;
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
I114gBoxLogUtils.d(TAG, "The onLongPress method execute.");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
I114gBoxLogUtils.d(TAG, "The onScroll method execute.");
I114gBoxLogUtils.i(TAG, "e1.X:" + e1.getX() + "|" + "e2.X:" + e2.getX()
+ "|" + "distanceX:" + distanceX + "|" + "distanceY:"
+ distanceY);
return false;
}
@Override
public void onShowPress(MotionEvent e) {
I114gBoxLogUtils.d(TAG, "The onShowPress method execute.");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
I114gBoxLogUtils.d(TAG, "The onSingleTapUp method execute.");
return false;
}
}
『貳』 android怎麼實現頁面的左右滑動
Android上有一個控制項叫做ViewPager,該控制項可以根據item的多少實現左右滑動的效果。
Android上還有一個東西叫做Fragment,這是一個依賴於Activity而又獨立的頁面。
綜合這兩個控制項的特性,可以使用ViewPager+Fragment的方式,即在ViewPager里嵌入Fragment的方式,實現頁面左右滑動的效果。
『叄』 android的ListView中如何設置長按Item的時候該Item背景變色,能分享下嗎
定義一個color,再給listview設置setonItemLongClickListener,在onItemLongClick方法中調用listview的setBackGroup()方法
『肆』 android Launcher的滑動效果怎麼實現
滑動功能主要分兩步:
1、在onInterceptTouchEvent中進行攔截。
2、在onTouchEvent中進行滑動。
1,onInterceptTouchEvent(MotionEvent en)
在這個方法中,決定了什麼時候截獲MotionEvent來實現滑動,避免了子View的其他事件的影響(如點擊事件)。
[java] view plain
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.
**/
//獲取速度跟蹤器,記錄各個時刻的速度。並且添加當前的MotionEvent以記錄更行速度值。
(ev);
......
/**
* Shortcut the most recurring case: the user is in the dragging
* state and he is moving his finger. We want to intercept this
* motion.
* 最常見的需要攔截的情況:用戶已經進入滑動狀態,並且正在滑動手指。
* 對這種情況直接進行攔截,執行onTouchEvent()繼續執行滑動操作。
**/
final int action = ev.getAction();
if ((action == MotionEvent.ACTION_MOVE) &&
(mTouchState == TOUCH_STATE_SCROLLING)) {
return true;
}
switch (action & MotionEvent.ACTION_MASK) {
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.
*/
/**
* 當在這里接受到ACTION_MOVE時,說明mTouchState!=TOUCH_STATE_SCROLLING並且mIsBeingDragged的值應該為false,
* 否則DragLayer就應該截獲了MotionEvent用於實現拖拽。
* 此時還沒有進入滑動狀態,當mActivePointerId == INVALID_POINTER時,也就是在此之前沒有接收到任何touch事件。
* 這種情況發生在Workspace變小時,也就是之前Workspace處於SPRING_LOADED狀態。當出現這種情況時直接把當前的事件當作ACTION_DOWN進行處理。
* 反之,則通過determineScrollingStart()嘗試能夠進入滑動狀態。
*/
if (mActivePointerId != INVALID_POINTER) {
determineScrollingStart(ev);
break;
}
// if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
// event. in that case, treat the first occurence of a move event as a ACTION_DOWN
// i.e. fall through to the next case (don't break)
// (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
// while it's small- this was causing a crash before we checked for INVALID_POINTER)
}
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
// Remember location of down touch
//記錄按下的x的坐標值
mDownMotionX = x;
//記錄前次發生touch時的坐標
mLastMotionX = x;
mLastMotionY = y;
//因為在ScrollBy時只能使用int,而記錄的x和y都是float,會產生誤差,故這里用mLastMotionXRemainder記錄余數
//用於消除誤差
mLastMotionXRemainder = 0;
//x方向上的總位移
mTotalMotionX = 0;
mActivePointerId = ev.getPointerId(0);
//設置mAllowLongPress=true,允許LongClick事件發生。LongClick事件定義在Launcher中
//處理的內容包括啟動對shortcut的拖拽或彈出壁紙選擇的對話框,若mAllowLongPress=false,
//則不會響應以上事件。
mAllowLongPress = true;
/**
* If being flinged and user touches the screen, initiate drag;
* otherwise don't. mScroller.isFinished should be false when
* being flinged.
* 當屏幕處於flinged狀態(快速滑動)時,若此時用戶觸摸了屏幕,需要使滑動停止。
* 並且初始化拖拽的條件
**/
final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
if (finishedScrolling) {
mTouchState = TOUCH_STATE_REST;
mScroller.abortAnimation();
} else {
mTouchState = TOUCH_STATE_SCROLLING;
}
// check if this can be the beginning of a tap on the side of the pages
// to scroll the current page
if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
if (getChildCount() > 0) {
if (hitsPreviousPage(x, y)) {
mTouchState = TOUCH_STATE_PREV_PAGE;
} else if (hitsNextPage(x, y)) {
mTouchState = TOUCH_STATE_NEXT_PAGE;
}
}
}
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mTouchState = TOUCH_STATE_REST;
mAllowLongPress = false;
mActivePointerId = INVALID_POINTER;
releaseVelocityTracker();
break;
case MotionEvent.ACTION_POINTER_UP:
onSecondaryPointerUp(ev);
releaseVelocityTracker();
break;
}
/**
* The only time we want to intercept motion events is if we are in the
* drag mode.
* 只有進入了滑動狀態,才進行攔截,進入onTouchEvent執行滑動操作。當mTouchState != TOUCH_STATE_REST
* 時,就說明沒有進入滑動狀態。
**/
return mTouchState != TOUCH_STATE_REST;
}
2,onTouchEvent(MotionEvent en)
在這個方法中,執行各種關於滑動的工作的計算,界面的刷新等工作。
[java] view plain
public boolean onTouchEvent(MotionEvent ev) {
......
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
/*
* If being flinged and user touches, stop the fling. isFinished
* will be false if being flinged.
*/
/**
* 如果Workspace此時已經被「擲出去」(靠慣性滑動)。
* 此時發生ACTION_DOWN則需要停止滑動。
*/
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
// Remember where the motion event started
mDownMotionX = mLastMotionX = ev.getX();
mLastMotionXRemainder = 0;
mTotalMotionX = 0;
mActivePointerId = ev.getPointerId(0);
if (mTouchState == TOUCH_STATE_SCROLLING) {
pageBeginMoving();
}
break;
case MotionEvent.ACTION_MOVE:
if (mTouchState == TOUCH_STATE_SCROLLING) {
......
if (Math.abs(deltaX) >= 1.0f) {
......
if (!mDeferScrollUpdate) {
//調用scrollBy滑動桌面
scrollBy((int) deltaX, 0);
......
} else {
......
}
mLastMotionX = x;
mLastMotionXRemainder = deltaX - (int) deltaX;
} else {
awakenScrollBars();
}
} else {
/**
* 如果條件滿足,則進入滑動狀態,開始滑動。
*/
determineScrollingStart(ev);
}
break;
case MotionEvent.ACTION_UP:
if (mTouchState == TOUCH_STATE_SCROLLING) {
......
boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
boolean returnToOriginalPage = false;
final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
Math.signum(velocityX) != Math.signum(deltaX)) {
returnToOriginalPage = true;
}
boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Math.abs(velocityX) > snapVelocity;
int finalPage;
//判斷拿起手指之後應該進入哪個分屏
if (((isSignificantMove && deltaX > 0 && !isFling) ||
(isFling && velocityX > 0)) && mCurrentPage > 0) {
finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
snapToPageWithVelocity(finalPage, velocityX);
} else if (((isSignificantMove && deltaX < 0 && !isFling) ||
(isFling && velocityX < 0)) &&
mCurrentPage < getChildCount() - 1) {
finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
snapToPageWithVelocity(finalPage, velocityX);
} else {
snapToDestination();
}
} else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
//直接進入前一屏
int nextPage = Math.max(0, mCurrentPage - 1);
if (nextPage != mCurrentPage) {
snapToPage(nextPage);
} else {
snapToDestination();
}
} else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
//直接進入後一屏
int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
if (nextPage != mCurrentPage) {
snapToPage(nextPage);
} else {
snapToDestination();
}
} else {
onUnhandledTap(ev);
}
mTouchState = TOUCH_STATE_REST;
mActivePointerId = INVALID_POINTER;
releaseVelocityTracker();
break;
case MotionEvent.ACTION_CANCEL:
if (mTouchState == TOUCH_STATE_SCROLLING) {
snapToDestination();
}
mTouchState = TOUCH_STATE_REST;
mActivePointerId = INVALID_POINTER;
releaseVelocityTracker();
break;
case MotionEvent.ACTION_POINTER_UP:
onSecondaryPointerUp(ev);
break;
}
return true;
}
『伍』 android客戶端的分類頁面滑動效果怎麼實現
在這里,我們需要用到google提到的一個包——android-support-v4.jar,這個包包含了一些非常有用的類,其中就是ViewPager類來實現頁面之間的切換操作,關於android-support-v4.jar的詳細信息,大家可以訪問google官方網站
『陸』 android 怎麼改變list view頂部滑動陰影顏色
需要說明的是,在正常默認情況下都是選擇Item橙黃色,點擊Item橙黃色,但是如果你設置了Item整體布局的背景色 android:background="#000000"則,你的上面兩個都會不起作用,導致你selected了Item看不出來是否選擇了。
那如何改橙黃色為綠色呢?????
step1:list_item.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="45dp"
android:orientation="vertical"
android:background="@drawable/list_item_bg"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
>
<TextView
.....
/>
<ImageView
....
/>
</RelativeLayout>
</LinearLayout>
『柒』 android的ListView中如何設置長按Item的時候該Item背景變色
看看這個http://haric.javaeye.com/blog/409863
設置一下android:cacheColorHint="#00000000" 在xml裡面
『捌』 Android 滑動菜單(slide menu) 背景 亮度和透明度設置
首先在sdk目錄下source源碼目錄下搜索DrawerLayout文件,把相關的java文件導入工程
以5.0為例
@Override
protectedbooleandrawChild(Canvascanvas,Viewchild,longdrawingTime){
finalintheight=getHeight();
finalbooleandrawingContent=isContentView(child);
intclipLeft=0,clipRight=getWidth();
finalintrestoreCount=canvas.save();
if(drawingContent){
finalintchildCount=getChildCount();
for(inti=0;i<childCount;i++){
finalViewv=getChildAt(i);
if(v==child||v.getVisibility()!=VISIBLE||
!hasOpaqueBackground(v)||!isDrawerView(v)||
v.getHeight()<height){
continue;
}
if((v,Gravity.LEFT)){
finalintvright=v.getRight();
if(vright>clipLeft)clipLeft=vright;
}else{
finalintvleft=v.getLeft();
if(vleft<clipRight)clipRight=vleft;
}
}
canvas.clipRect(clipLeft,0,clipRight,getHeight());
}
finalbooleanresult=super.drawChild(canvas,child,drawingTime);
canvas.restoreToCount(restoreCount);
if(mScrimOpacity>0&&drawingContent){
/*
*這里是直接用畫筆畫了一層有透明度的方塊覆蓋在主內容上方,產生慢*慢變暗的效果,所以我們只需要在這里修改即可,
*根據代碼邏輯可以知道我們只需要改變imag即可
*/
finalintbaseAlpha=(mScrimColor&0xff000000)>>>24;
finalintimag=(int)(baseAlpha*mScrimOpacity);
finalintcolor=imag<<24|(mScrimColor&0xffffff);
mScrimPaint.setColor(color);
canvas.drawRect(clipLeft,0,clipRight,getHeight(),mScrimPaint);
}elseif(mShadowLeft!=null&&(child,Gravity.LEFT)){
finalintshadowWidth=mShadowLeft.getIntrinsicWidth();
finalintchildRight=child.getRight();
finalintdrawerPeekDistance=mLeftDragger.getEdgeSize();
finalfloatalpha=
Math.max(0,Math.min((float)childRight/drawerPeekDistance,1.f));
//這里是繪制菜單的覆蓋物
mShadowLeft.setBounds(childRight,child.getTop(),
childRight+shadowWidth,child.getBottom());
mShadowLeft.setAlpha((int)(0xff*alpha));
mShadowLeft.draw(canvas);
}elseif(mShadowRight!=null&&(child,Gravity.RIGHT)){
finalintshadowWidth=mShadowRight.getIntrinsicWidth();
finalintchildLeft=child.getLeft();
finalintshowing=getWidth()-childLeft;
finalintdrawerPeekDistance=mRightDragger.getEdgeSize();
finalfloatalpha=
Math.max(0,Math.min((float)showing/drawerPeekDistance,1.f));
mShadowRight.setBounds(childLeft-shadowWidth,child.getTop(),
childLeft,child.getBottom());
mShadowRight.setAlpha((int)(0xff*alpha));
mShadowRight.draw(canvas);
}
returnresult;
}
改變代碼之後保存
這時候在布局文件中就不要使用android-supportv4包下的DrawerLayout的,改成你工程里修改後的DrawerLayout,運行就達到效果了
『玖』 我想在android 平台上實現多頁滑動效果,每頁都有自己獨立的按鈕,請問如何實現。謝謝!
這個簡單。只要做一個頁面就行了,將需要滑動的那部分用ViewFlipper實現就行了,然後在ViewFlipper里添加具體的頁面。ViewFlipper可以嵌套。