어떻게 Android 에서 그 라 데 이 션 단추 의 좌우 미끄럼 효 과 를 실현 합 니까?

먼저 실행 효과 보기:
  
프로그램 구성:
MainActivity 파일 의 코드:

package com.android.buttonpageflipper;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.ViewFlipper;
/**
 * Android
 * @Description: ,
 *
 * @FileName: MainActivity.java
 *
 * @Package com.android.buttonpageflipper
 *
 * @Author Hanyonglu
 *
 */
public class MainActivity extends Activity {
    // ,
    private ImageView btnLeft=null;
    private ImageView btnRight=null;

    // WindowManager
    private WindowManager wm=null;
    private WindowManager.LayoutParams wmParams=null;

    //ImageView alpha   
    private int mAlpha = 0;
    private boolean isHide;

    private ViewFlipper viewFlipper = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setTitle("Android ");
        viewFlipper = (ViewFlipper) this.findViewById(R.id.myViewFlipper);

        //
        initImageButtonView();
    }

    /**
     *
     */
    private void initImageButtonView(){
        // WindowManager
        wm=(WindowManager)getApplicationContext().getSystemService("window");

        // LayoutParams
        wmParams = new WindowManager.LayoutParams();

        // window type
        wmParams.type=LayoutParams.TYPE_PHONE;  

        // ,
        wmParams.format=PixelFormat.RGBA_8888;  

        // Window flag
         wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL
                               | LayoutParams.FLAG_NOT_FOCUSABLE;
        // x、y
        wmParams.x=0;
        wmParams.y=0;

        //
        wmParams.width=50;
        wmParams.height=50;

        //
        createLeftButtonView();
        createRightButtonView();
    }

    /**
     *
     */
    private void createLeftButtonView(){
        btnLeft=new ImageView(this);
        btnLeft.setImageResource(R.drawable.left);
        btnLeft.setAlpha(0);

        btnLeft.setOnClickListener(new View.OnClickListener() {   
            public void onClick(View arg0) {
                //
                viewFlipper.setInAnimation(MainActivity.this, R.anim.push_left_in);
                viewFlipper.setOutAnimation(MainActivity.this, R.anim.push_left_out);
                viewFlipper.showPrevious();
            }
        });

        //
        wmParams.gravity=Gravity.LEFT|Gravity.CENTER_VERTICAL;

        //
        wm.addView(btnLeft, wmParams);
    }

    /**
     *
     */
    private void createRightButtonView(){
        btnRight=new ImageView(this);
        btnRight.setImageResource(R.drawable.right);
        btnRight.setAlpha(0);

        btnRight.setOnClickListener(new View.OnClickListener() {   
            public void onClick(View arg0) {
                //
                viewFlipper.setInAnimation(MainActivity.this, R.anim.push_right_in);
                viewFlipper.setOutAnimation(MainActivity.this, R.anim.push_right_out);
                viewFlipper.showNext();
            }
        });

        //
        wmParams.gravity=Gravity.RIGHT|Gravity.CENTER_VERTICAL;

        //
        wm.addView(btnRight, wmParams);
    }

    /**
     *
     */
    private Handler mHandler = new Handler()
    {
        public void handleMessage(Message msg) {
            if(msg.what==1 && mAlpha<255){          
                //
                mAlpha += 50;

                if(mAlpha>255)
                    mAlpha=255;

                btnLeft.setAlpha(mAlpha);
                btnLeft.invalidate();
                btnRight.setAlpha(mAlpha);
                btnRight.invalidate();

                if(!isHide && mAlpha<255)
                    mHandler.sendEmptyMessageDelayed(1, 100);
            }else if(msg.what==0 && mAlpha>0){
                mAlpha -= 10;

                if(mAlpha<0)
                    mAlpha=0;
                btnLeft.setAlpha(mAlpha);
                btnLeft.invalidate();
                btnRight.setAlpha(mAlpha);
                btnRight.invalidate();

                if(isHide && mAlpha>0)
                    mHandler.sendEmptyMessageDelayed(0, 800);
            }           
        }
    };

    private void showImageButtonView(){
        isHide = false;
        mHandler.sendEmptyMessage(1);
    }

    private void hideImageButtonView(){
        new Thread(){
            public void run() {
                try {
                    Thread.sleep(1500);
                    isHide = true;
                    mHandler.sendEmptyMessage(0);
                } catch (Exception e) {

                }
            }
        }.start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_DOWN:
                showImageButtonView();           
                break;
            case MotionEvent.ACTION_UP:
                hideImageButtonView();               
                break;
        }

        return true;
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        // (Activity )
        wm.removeView(btnLeft);
        wm.removeView(btnRight);
    }

main.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">
    <ViewFlipper
        android:id="@+id/myViewFlipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <!-- -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/one"
                android:gravity="center" />
        </LinearLayout>
        <!-- -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/two"
                android:gravity="center" />
        </LinearLayout>
        <!-- -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/three"
                android:gravity="center" />
        </LinearLayout>
        <!-- -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/four"
                android:gravity="center" />
        </LinearLayout>
        <!-- -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/five"
                android:gravity="center" />
        </LinearLayout>
    </ViewFlipper>
</LinearLayout>
pushleft_in.xml 파일 의 코드:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0"
        android:duration="500" />
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0"
        android:duration="500" />
</set> 
pushleft_out.xml 파일 의 코드:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-100%p"
        android:duration="500" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0.1"
        android:duration="500" />
</set> 
pushright_in.xml 파일 의 코드:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0"
        android:duration="500" />
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0"
        android:duration="500" />
</set> 
pushright_out.xml 파일 의 코드:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p"
        android:duration="500" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0.1"
        android:duration="500" />
</ set>
마지막 으로 설정 파일 에 권한 을 설정 하 는 것 을 잊 지 마 세 요.예시 다운로드:다운로드 클릭

좋은 웹페이지 즐겨찾기