最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼

 更新時(shí)間:2015年12月11日 11:43:35   投稿:mrr  
利用Android的ViewFlipper和AnimationUtils實(shí)現(xiàn)圖片帶有動(dòng)畫的輪播切換,其中當(dāng)點(diǎn)擊“上一張”圖片時(shí),切換到上一張圖片;當(dāng)點(diǎn)擊“下一張”圖片時(shí),切換到下一張圖片,本文給大家介紹Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼,需要的朋友參考下

利用Android的ViewFlipper和AnimationUtils實(shí)現(xiàn)圖片帶有動(dòng)畫的輪播切換,其中當(dāng)點(diǎn)擊“上一張”圖片時(shí),切換到上一張圖片;當(dāng)點(diǎn)擊“下一張”圖片時(shí),切換到下一張圖片。其效果圖如下:

設(shè)置布局文件,其內(nèi)容如下:

activity_image_flipper_shade.xml

<?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_shade_title"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >
  <Button
   android:id="@+id/btn_image_flipper_shade_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_shade_title"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerInParent="true"
   android:text="@string/image_flipper_shade"
   android:textColor="@color/textColor"
   android:textSize="30sp"
   android:textStyle="bold" />
 </RelativeLayout>
 <LinearLayout
  android:id="@id/ll_image_flipper_shade_content"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_below="@id/rl_image_flipper_shade_title"
  android:layout_marginBottom="20dp"
  android:layout_marginTop="20dp"
  android:gravity="center"
  android:orientation="vertical" >
  <ViewFlipper
   android:id="@id/vf_image_flipper_shade"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />
 </LinearLayout>
</RelativeLayout>

動(dòng)畫效果配置文件,其內(nèi)容如下:

A.push_left_in.xml(從左邊進(jìn)入屏幕)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
 <!-- translate:畫面轉(zhuǎn)換位置移動(dòng)動(dòng)畫效果 -->
 <translate
  android:duration="500"
  android:fromXDelta="100%p"
  android:toXDelta="0" />
 <!-- alpha:漸變透明度動(dòng)畫效果 -->
 <alpha
  android:duration="500"
  android:fromAlpha="0.1"
  android:toAlpha="1.0" />
 <!-- scale:漸變尺寸伸縮動(dòng)畫效果 -->
 <!-- rotate:畫面轉(zhuǎn)換位置移動(dòng)動(dòng)畫效果 -->
</set>
B.push_left_out.xml(從左邊退出屏幕)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
 <translate
  android:duration="500"
  android:fromXDelta="0"
  android:toXDelta="-100%p" />
 <alpha
  android:duration="500"
  android:fromAlpha="1.0"
  android:toAlpha="0.1" />
</set>
C.push_right_in.xml(從右邊進(jìn)入屏幕)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
 <translate
  android:duration="500"
  android:fromXDelta="-100%p"
  android:toXDelta="0" />
 <alpha
  android:duration="500"
  android:fromAlpha="0.1"
  android:toAlpha="1.0" />
</set>
D.push_right_out.xml(從右邊退出屏幕)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
 <translate
  android:duration="500"
  android:fromXDelta="0"
  android:toXDelta="100%p" />
 <alpha
  android:duration="500"
  android:fromAlpha="1.0"
  android:toAlpha="0.1" />
</set>

實(shí)現(xiàn)圖片輪播切換的類為ImageFlipperShadeActivity.java,其內(nèi)容為:

/**
 * 
 */
package com.i114gbox.aglieguy;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
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;
/**
 * 圖片滑動(dòng)漸變Activity
 * 
 * @author SJC
 * 
 */
public class ImageFlipperShadeActivity extends I114gBoxActivity {
private static String TAG = "ImageFlipperShadeActivity";
private Context ctx = null;
private ViewFlipper viewFlipper;// 視圖輪播
private WindowManager windowManager;// 窗口管理器
private WindowManager.LayoutParams layoutParams;// 布局參數(shù)
private boolean isHide;
private int mAlpha = 0;
// 左邊圖片視圖
private ImageView leftImageView;
// 右邊圖片視圖
private ImageView rightImageView;
private int WHAT_HIDE = 0;
private int WHAT_SHOW = 1;
@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_shade"));
viewFlipper = (ViewFlipper) findViewById(I114gBoxResourceUtils.getId(
ctx, "vf_image_flipper_shade"));
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")));
Button backButton = (Button) findViewById(I114gBoxResourceUtils.getId(
ctx, "btn_image_flipper_shade_back"));
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
initImageButtonView();// 初始化ImageButton視圖
}
/** 添加ImageView控件 **/
private View addImageView(int id) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(id);
return imageView;
}
/** 初始化ImageButton視圖 **/
private void initImageButtonView() {
windowManager = (WindowManager) ctx
.getSystemService(Context.WINDOW_SERVICE);
layoutParams = new WindowManager.LayoutParams();
// 設(shè)置窗口類型
layoutParams.type = LayoutParams.TYPE_PHONE;
// 設(shè)置圖片格式,效果為背景透明
layoutParams.format = PixelFormat.RGBA_8888;
// 設(shè)置FLAG參數(shù),觸摸失效或無法獲取焦點(diǎn)
layoutParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
// 初始化話坐標(biāo)值
layoutParams.x = 0;
layoutParams.y = 0;
// 設(shè)置窗口的寬度和高度
layoutParams.width = 50;
layoutParams.height = 50;
// 創(chuàng)建左邊和右邊按鈕
createLeftButtonView();
createRightButtonView();
}
/** 創(chuàng)建左邊按鈕 **/
private void createLeftButtonView() {
leftImageView = new ImageView(ctx);
leftImageView.setBackgroundResource(I114gBoxResourceUtils
.getDrawableId(ctx, "flipper_left"));
leftImageView.setAlpha(0);// 完全透明
// 添加點(diǎn)擊監(jiān)聽事件
leftImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 設(shè)置進(jìn)入屏幕的動(dòng)畫
viewFlipper.setInAnimation(AnimationUtils.loadAnimation(ctx,
I114gBoxResourceUtils.getAnimId(ctx, "push_left_in")));
// 設(shè)置退出屏幕的動(dòng)畫
viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(ctx,
I114gBoxResourceUtils.getAnimId(ctx, "push_left_out")));
// 顯示下一個(gè)圖層
viewFlipper.showNext();
}
});
// 設(shè)置布局為左邊垂直居中
layoutParams.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
// 將左邊按鈕添加到窗口中
windowManager.addView(leftImageView, layoutParams);
}
/** 創(chuàng)建右邊按鈕 **/
private void createRightButtonView() {
rightImageView = new ImageView(ctx);
rightImageView.setBackgroundResource(I114gBoxResourceUtils.getDrawableId(
ctx, "flipper_right"));
rightImageView.setAlpha(0);// 完全透明
// 添加點(diǎn)擊監(jiān)聽事件
rightImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 設(shè)置進(jìn)入屏幕的動(dòng)畫
viewFlipper.setInAnimation(AnimationUtils.loadAnimation(ctx,
I114gBoxResourceUtils.getAnimId(ctx, "push_right_in")));
// 設(shè)置退出屏幕的動(dòng)畫
viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(ctx,
I114gBoxResourceUtils.getAnimId(ctx, "push_right_out")));
// 顯示上一個(gè)圖層
viewFlipper.showPrevious();
}
});
// 設(shè)置布局為右邊垂直居中
layoutParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
// 將右邊按鈕添加到窗口中
windowManager.addView(rightImageView, layoutParams);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
I114gBoxLogUtils.d(TAG, "The onTouchEvent method execute.");
switch (event.getAction()) {
// 移動(dòng)事件
case MotionEvent.ACTION_MOVE:
break;
// 按下事件
case MotionEvent.ACTION_DOWN:
// 顯示ImageButton視圖
showImageButtonView();
break;
// 按下后松開事件
case MotionEvent.ACTION_UP:
// 隱藏ImageButton視圖
hideImageButtonView();
break;
default:
break;
}
return true;
}
/** 顯示ImageButton視圖 **/
private void showImageButtonView() {
isHide = true;
mHandler.sendEmptyMessage(WHAT_SHOW);
}
/** 隱藏ImageButton視圖 **/
private void hideImageButtonView() {
new Thread() {
@Override
public void run() {
try {
Thread.sleep(1500);
isHide = false;
mHandler.sendEmptyMessage(WHAT_HIDE);
} catch (InterruptedException e) {
I114gBoxLogUtils.e(TAG, e.getMessage());
}
};
}.start();
}
/** 處理異步消息 **/
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
// 當(dāng)接收到顯示左右圖片的消息時(shí)
if (msg.what == 1 && mAlpha < 255) {
mAlpha += 50;
if (mAlpha > 255)
mAlpha = 255;
// 設(shè)置透明度
leftImageView.setAlpha(mAlpha);
// 刷新視圖
leftImageView.invalidate();
rightImageView.setAlpha(mAlpha);
rightImageView.invalidate();
if (!isHide && mAlpha < 255)
mHandler.sendEmptyMessageDelayed(WHAT_SHOW, 100);
}
// 當(dāng)接收到隱藏左右圖片的消息時(shí)
else if (msg.what == 0 && mAlpha > 0) {
mAlpha -= 10;
if (mAlpha < 0)
mAlpha = 0;
// 設(shè)置透明度
leftImageView.setAlpha(mAlpha);
// 刷新視圖
leftImageView.invalidate();
rightImageView.setAlpha(mAlpha);
rightImageView.invalidate();
if (isHide && mAlpha > 0)
mHandler.sendEmptyMessageDelayed(WHAT_HIDE, 100);
}
};
};
@Override
protected void onDestroy() {
I114gBoxLogUtils.d(TAG, "The onDestory method execute.");
super.onDestroy();
// 移除ImageView控件
windowManager.removeView(leftImageView);
windowManager.removeView(rightImageView);
};
}

腳本之家友情提醒大家需要注意事項(xiàng)如下:

需要設(shè)置WindowManager的屬性,包含type、format和flags等等創(chuàng)建左右邊圖片動(dòng)畫加載效果,并實(shí)現(xiàn)onTouchEvent事件,其中MotionEvent.ACTION_DOWN為按下監(jiān)聽事件,MotionEvent.ACTION_UP為按下后松開事件

以上是本文給大家敘述的Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼,希望可以幫助到大家。

相關(guān)文章

  • Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條

    Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Android ExpandableListView使用方法案例詳解

    Android ExpandableListView使用方法案例詳解

    這篇文章主要介紹了Android ExpandableListView使用方法案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android組件ContextMenu實(shí)現(xiàn)長按事件

    Android組件ContextMenu實(shí)現(xiàn)長按事件

    這篇文章主要為大家詳細(xì)介紹了Android組件ContextMenu實(shí)現(xiàn)長按事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Android使用BottomNavigationBar實(shí)現(xiàn)底部導(dǎo)航欄

    Android使用BottomNavigationBar實(shí)現(xiàn)底部導(dǎo)航欄

    這篇文章主要為大家詳細(xì)介紹了Android使用BottomNavigationBar實(shí)現(xiàn)底部導(dǎo)航欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • Android如何通過URI獲取文件路徑示例代碼

    Android如何通過URI獲取文件路徑示例代碼

    這篇文章主要給大家介紹了關(guān)于Android如何通過URI獲取文件路徑的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • Android基于訊飛語音SDK實(shí)現(xiàn)語音識(shí)別

    Android基于訊飛語音SDK實(shí)現(xiàn)語音識(shí)別

    本例子是一個(gè)調(diào)用訊飛語音識(shí)別SDK的例子源碼是一個(gè)最純凈的Demo比較容易看懂。實(shí)現(xiàn)的是點(diǎn)擊按鈕開始語音監(jiān)聽,手機(jī)需要聯(lián)網(wǎng),2/3G的均可,希望本文對(duì)大家學(xué)習(xí)Android有所幫助
    2016-06-06
  • android自定義Toast設(shè)定顯示時(shí)間

    android自定義Toast設(shè)定顯示時(shí)間

    這篇文章主要為大家詳細(xì)介紹了android自定義Toast設(shè)定顯示時(shí)間,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • android wifi信號(hào)強(qiáng)度等級(jí)區(qū)分的修改介紹

    android wifi信號(hào)強(qiáng)度等級(jí)區(qū)分的修改介紹

    calculateSignalLevel為計(jì)算信號(hào)等級(jí)函數(shù),MAX_RSSI和MIN_RSSI分別為最強(qiáng)和最弱信號(hào)強(qiáng)度等級(jí)的信號(hào)強(qiáng)度閥值
    2013-06-06
  • Android數(shù)據(jù)加密之Rsa加密

    Android數(shù)據(jù)加密之Rsa加密

    這篇文章主要為大家詳細(xì)介紹了Android數(shù)據(jù)加密之Rsa加密,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android SQLite數(shù)據(jù)庫加密的操作方法

    Android SQLite數(shù)據(jù)庫加密的操作方法

    因?yàn)锳ndroid自帶的SQLite數(shù)據(jù)庫本身是沒有實(shí)現(xiàn)加密的,那我們?nèi)绾螌?shí)現(xiàn)對(duì)數(shù)據(jù)庫的加密呢?今天通過本文給大家介紹下Android SQLite數(shù)據(jù)庫加密的操作方法,一起看看吧
    2021-09-09

最新評(píng)論

东方市| 桑日县| 通渭县| 左云县| 沅江市| 淳安县| 辽源市| 太谷县| 黄大仙区| 宣威市| 成都市| 孝感市| 阳曲县| 吉隆县| 克山县| 富蕴县| 宁晋县| 海南省| 海晏县| 咸丰县| 井研县| 那曲县| 凌源市| 阿拉善左旗| 衡南县| 沛县| 昌图县| 南安市| 太原市| 和龙市| 兴国县| 万载县| 涪陵区| 邛崃市| 休宁县| 海阳市| 乳源| 如东县| 裕民县| 田东县| 安泽县|