很贊的引導(dǎo)界面效果Android控件ImageSwitcher實(shí)現(xiàn)
本文實(shí)例為大家分享了Android控件ImageSwitcher實(shí)現(xiàn)引導(dǎo)界面的代碼,供大家參考,具體內(nèi)容如下
效果圖:

布局代碼:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ImageSwitcher>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
</FrameLayout>
頁面代碼:
public class ImageSwitcherActivity extends Activity implements ViewSwitcher.ViewFactory, View.OnTouchListener {
private int[] imgIds;//圖片id數(shù)組
private int currentPosition;//當(dāng)前選中的圖片id序號(hào)
private ImageSwitcher mImageSwitcher;//ImagaSwitcher 的引用
private float downX;//按下點(diǎn)的X坐標(biāo)
private ImageView[] tips;//點(diǎn)點(diǎn)數(shù)組
private LinearLayout linearLayout;//裝載點(diǎn)點(diǎn)的容器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_switcher);
imgIds = new int[]{R.drawable.bg, R.drawable.c2, R.drawable.c3, R.drawable.c4, R.drawable.c5, R.drawable.c6, R.drawable.c7, R.drawable.c8, R.drawable.c9};
mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);//實(shí)例化ImageSwitcher
mImageSwitcher.setFactory(this); //設(shè)置Factory
mImageSwitcher.setOnTouchListener(this);//設(shè)置OnTouchListener,我們通過Touch事件來切換圖片
linearLayout = (LinearLayout) findViewById(R.id.ll_view);//指示器布局
tips = new ImageView[imgIds.length];
for (int i = 0; i < imgIds.length; i++) {
ImageView mImageView = new ImageView(this);
tips[i] = mImageView;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layoutParams.rightMargin = 3;
layoutParams.leftMargin = 3;
mImageView.setBackgroundResource(R.drawable.page_indicator_unfocused);
linearLayout.addView(mImageView, layoutParams);
}
//上一個(gè)界面?zhèn)鬟^來的位置
currentPosition = getIntent().getIntExtra("position", 0);
mImageSwitcher.setImageResource(imgIds[currentPosition]);
setImageBackground(currentPosition);
}
//設(shè)置選中的tip的背景
private void setImageBackground(int selectItems) {
for (int i = 0; i < tips.length; i++) {
if (i == selectItems) {
tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
} else {
tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
}
}
}
@Override
public View makeView() {
final ImageView i = new ImageView(this);
i.setBackgroundColor(0xff000000);
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return i;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();//手指按下的X坐標(biāo)
break;
}
case MotionEvent.ACTION_UP: {
float lastX = event.getX();
//抬起的時(shí)候的X坐標(biāo)大于按下的時(shí)候就顯示上一張圖片
if (lastX > downX) {
if (currentPosition > 0) {
//設(shè)置動(dòng)畫
mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.left_in));
mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_out));
currentPosition--;
mImageSwitcher.setImageResource(imgIds[currentPosition % imgIds.length]);
setImageBackground(currentPosition);
} else {
Toast.makeText(getApplication(), "已經(jīng)是第一張", Toast.LENGTH_SHORT).show();
}
}
if (lastX < downX) {
if (currentPosition < imgIds.length - 1) {
mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_in));
mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.lift_out));
currentPosition++;
mImageSwitcher.setImageResource(imgIds[currentPosition]);
setImageBackground(currentPosition);
} else {
Toast.makeText(getApplication(), "到了最后一張", Toast.LENGTH_SHORT).show();
}
}
}
break;
}
return true;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android入門之Gallery+ImageSwitcher用法實(shí)例解析
- Android控件ImageSwitcher實(shí)現(xiàn)左右圖片切換功能
- Android常用控件ImageSwitcher使用方法詳解
- Android基于ImageSwitcher實(shí)現(xiàn)圖片切換功能
- Android UI控件之ImageSwitcher實(shí)現(xiàn)圖片切換效果
- Android高級(jí)組件ImageSwitcher圖像切換器使用方法詳解
- Android之ImageSwitcher的實(shí)例詳解
- 基于Android實(shí)現(xiàn)保存圖片到本地并可以在相冊(cè)中顯示出來
- android獲取相冊(cè)圖片和路徑的實(shí)現(xiàn)方法
- Android ViewPager相冊(cè)橫向移動(dòng)的實(shí)現(xiàn)方法
- Android開發(fā)之ImageSwitcher相冊(cè)功能實(shí)例分析
相關(guān)文章
新浪微博第三方登錄界面上下拉伸圖片之第三方開源PullToZoomListViewEx(二)
這篇文章主要介紹了新浪微博第三方登錄界面上下拉伸圖片之第三方開源PullToZoomListViewEx(二) 的相關(guān)資料,需要的朋友可以參考下2015-12-12
Android實(shí)現(xiàn)可復(fù)用的篩選頁面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可復(fù)用的篩選頁面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
android 中 webview 怎么用 localStorage
這篇文章主要介紹了android 中 webview 怎么用 localStorage方法的相關(guān)資料,需要的朋友可以參考下2015-07-07
Android中用onSaveInstanceState保存Fragment狀態(tài)的方法
這篇文章主要介紹了Android中用onSaveInstanceState保存Fragment狀態(tài)的方法,onSaveInstanceState可以將數(shù)據(jù)保存在Fragment或Activity中,需要的朋友可以參考下2016-04-04
Android實(shí)現(xiàn)史上最簡(jiǎn)單自定義開關(guān)按鈕的方法
在平常的開發(fā)中按鈕是經(jīng)常使用到的控件之一,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)史上最簡(jiǎn)單自定義開關(guān)按鈕的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Android Notification使用方法總結(jié)
這篇文章主要介紹了Android Notification使用方法總結(jié)的相關(guān)資料,這里提供了四種使用方法,需要的朋友可以參考下2017-09-09
詳解Android Studio3.5及使用AndroidX的一些坑
這篇文章主要介紹了詳解Android Studio3.5及使用AndroidX的一些坑,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
android APP登陸頁面適配的實(shí)現(xiàn)
這篇文章主要介紹了android APP登陸頁面適配的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09

