如何在Android中實現漸顯按鈕的左右滑動效果

來源:互聯網
上載者:User

先看下運行效果:

程式結構:

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>

push_left_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>

push_left_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>

push_right_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>

push_right_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>

最後,別忘記了在設定檔中設定許可權。

樣本下載:點擊下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.