Android基于ImageSwitcher實現(xiàn)圖片切換功能
左右切換圖片控件大家都用ViewPager, ViewFipper比較多吧,我之前也用ViewPager實現(xiàn)了,使用ViewPager實現(xiàn)左右循環(huán)滑動圖片,有興趣的可以去看下,今天介紹的是基于ImageSwitcher實現(xiàn)的左右切換圖片,先上截圖吧
好了,接下來來看代碼吧,第一張圖是一個GridView,點擊item跳轉(zhuǎn)到第二個界面,第一個界面可以忽略,主要是講解ImageSwitcher的左右卻換圖片,先看布局文件
<?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/imageSwitcher1" 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/viewGroup" 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>
然后就是Activity代碼啦,總體來說比較簡單,代碼我添加了注釋
package com.example.photoalbum; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout.LayoutParams; import android.widget.Toast; import android.widget.ViewSwitcher.ViewFactory; public class ShowPhotoActivity extends Activity implements ViewFactory, OnTouchListener{ /** * ImagaSwitcher 的引用 */ private ImageSwitcher mImageSwitcher; /** * 圖片id數(shù)組 */ private int[] imgIds; /** * 當前選中的圖片id序號 */ private int currentPosition; /** * 按下點的X坐標 */ private float downX; /** * 裝載點點的容器 */ private LinearLayout linearLayout; /** * 點點數(shù)組 */ private ImageView[] tips; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.show_photo); imgIds = new int[]{R.drawable.item01,R.drawable.item02,R.drawable.item03,R.drawable.item04, R.drawable.item05, R.drawable.item06, R.drawable.item07, R.drawable.item08,R.drawable.item09, R.drawable.item10, R.drawable.item11, R.drawable.item12}; //實例化ImageSwitcher mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1); //設(shè)置Factory mImageSwitcher.setFactory(this); //設(shè)置OnTouchListener,我們通過Touch事件來切換圖片 mImageSwitcher.setOnTouchListener(this); linearLayout = (LinearLayout) findViewById(R.id.viewGroup); 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); } //這個我是從上一個界面?zhèn)鬟^來的,上一個界面是一個GridView currentPosition = getIntent().getIntExtra("position", 0); mImageSwitcher.setImageResource(imgIds[currentPosition]); setImageBackground(currentPosition); } /** * 設(shè)置選中的tip的背景 * @param selectItems */ 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.FILL_PARENT, LayoutParams.FILL_PARENT)); return i ; } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN:{ //手指按下的X坐標 downX = event.getX(); break; } case MotionEvent.ACTION_UP:{ float lastX = event.getX(); //抬起的時候的X坐標大于按下的時候就顯示上一張圖片 if(lastX > downX){ if(currentPosition > 0){ //設(shè)置動畫,這里的動畫比較簡單,不明白的去網(wǎng)上看看相關(guān)內(nèi)容 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; } }
上面切換圖片主要用到的就是動畫了,用的是translate移動動畫,這里我就不介紹了,接下來我吧動畫代碼貼出來,在res新建一個anim的目錄,如下圖
左邊進入的動畫,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"/> </set>
左邊出去的動畫,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"/> </set>
右邊進入的動畫,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"/> </set>
右邊出去的動畫,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"/> </set>
好了,介紹完了,代碼寫的不是很好,寫的不好的地方希望大家諒解,小編一定更加努力。
- Android開發(fā)實現(xiàn)圖片切換APP
- Android UI控件之ImageSwitcher實現(xiàn)圖片切換效果
- Android自定義ImageView實現(xiàn)點擊兩張圖片切換效果
- Android使用ViewFlipper實現(xiàn)圖片切換功能
- Android 圖片切換器(dp、sp、px) 的單位轉(zhuǎn)換器
- Android中使用imageviewswitcher 實現(xiàn)圖片切換輪播導(dǎo)航的方法
- Android控件ImageSwitcher實現(xiàn)左右圖片切換功能
- Android自定義ViewPager實現(xiàn)個性化的圖片切換效果
- Android中ViewPager組件的基本用法及實現(xiàn)圖片切換的示例
- android實現(xiàn)點擊按鈕控制圖片切換
相關(guān)文章
詳解Retrofit 動態(tài)參數(shù)(非固定參數(shù)、非必須參數(shù))(Get、Post請求)
這篇文章主要介紹了詳解Retrofit 動態(tài)參數(shù)(非固定參數(shù)、非必須參數(shù))(Get、Post請求),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04Android ListView實現(xiàn)無限循環(huán)滾動
這篇文章主要為大家詳細介紹了Android ListView實現(xiàn)無限循環(huán)滾動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06