欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)圖片輪播效果的兩種方法

 更新時間:2015年12月11日 11:30:13   投稿:mrr  
android圖片輪播效果非常漂亮,在程序開發(fā)中也經(jīng)常用到,本文給大家分享android實現(xiàn)圖片輪播效果的幾種方法,對android實現(xiàn)圖片輪播相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧

大家在使用APP的過程中,經(jīng)常會看到上部banner圖片輪播的效果,那么今天我們就一起來學(xué)習(xí)一下,android中圖片輪詢的幾種實現(xiàn)方法:

第一種:使用動畫的方法實現(xiàn):(代碼繁瑣)

這種發(fā)放需要:兩個動畫效果,一個布局,一個主類來實現(xiàn),不多說了,來看代碼吧:

public class IamgeTrActivity extends Activity {
/** Called when the activity is first created. */
public ImageView imageView;
public ImageView imageView2;
public Animation animation1;
public Animation animation2;
public TextView text;
public boolean juage = true;
public int images[] = new int[] { R.drawable.icon, R.drawable.expriment,
R.drawable.changer, R.drawable.dataline, R.drawable.preffitication };
public int count = 0;
public Handler handler = new Handler();
public Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
AnimationSet animationSet1 = new AnimationSet(true);
AnimationSet animationSet2 = new AnimationSet(true);
imageView2.setVisibility(0);
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
-1f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet1.addAnimation(ta);
animationSet1.setFillAfter(true);
ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
0f, Animation.RELATIVE_TO_SELF, 0f);
ta.setDuration(2000);
animationSet2.addAnimation(ta);
animationSet2.setFillAfter(true);
//iamgeView 出去 imageView2 進來
imageView.startAnimation(animationSet1);
imageView2.startAnimation(animationSet2);
imageView.setBackgroundResource(images[count % 5]);
count++;
imageView2.setBackgroundResource(images[count % 5]);
text.setText(String.valueOf(count));
if (juage)
handler.postDelayed(runnable, 6000);
Log.i(handler, handler);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView);
imageView2 = (ImageView) findViewById(R.id.imageView2);
text=(TextView)findViewById(R.id.text);
text.setText(String.valueOf(count));
//將iamgeView先隱藏,然后顯示
imageView2.setVisibility(4);
handler.postDelayed(runnable, 2000);
}
public void onPause() {
juage = false;
super.onPause();
}
}

布局代碼:

android:orientation=vertical
android:layout_width=fill_parent
android:layout_height=fill_parent
android:id=@+id/rl>
android:id=@+id/imageView
android:layout_width=fill_parent
android:background=@drawable/icon
android:layout_below=@+id/rl
android:layout_height=120dp />
android:id=@+id/imageView2
android:layout_width=fill_parent
android:background=@drawable/expriment
android:layout_below=@+id/rl
android:layout_height=120dp />
android:id=@+id/text
android:layout_width=fill_parent
android:layout_height=wrap_content
android:layout_below=@id/imageView/>

 第二種:使用ViewFlipper實現(xiàn)圖片的輪播

Android系統(tǒng)自帶的一個多頁面管理控件,它可以實現(xiàn)子界面的自動切換:

首先 需要為ViewFlipper加入View

(1) 靜態(tài)導(dǎo)入:在layout布局文件中直接導(dǎo)入

(2) 動態(tài)導(dǎo)入:addView()方法

ViewPlipper常用方法:

setInAnimation:設(shè)置View進入屏幕時候使用的動畫

setOutAnimation:設(shè)置View退出屏幕時候使用的動畫

showNext:調(diào)用該函數(shù)來顯示ViewFlipper里面的下一個View

showPrevious:調(diào)用該函數(shù)來顯示ViewFlipper里面的上一個View

setFlipInterval:設(shè)置View之間切換的時間間隔

startFlipping使用上面設(shè)置的時間間隔來開始切換所有的View,切換會循環(huán)進行

stopFlipping:停止View切換

講了這么多,那么我們今天要實現(xiàn)的是什么呢?

(1) 利用ViewFlipper實現(xiàn)圖片的輪播

(2) 支持手勢滑動的ViewFlipper

我們需要先準備幾張圖片:把圖片放進drawable中

創(chuàng)建兩個動畫:在res下面新建一個folder里面新建兩個xml:

left_in:
android:duration=5000
android:fromXDelta=100%p
android:toXDelta=0/>
left_out:
android:fromXDelta=0
android:toXDelta=-100%p
android:duration=5000/>

一個布局文件:

xmlns:tools=http://schemas.android.com/tools
android:layout_width=match_parent
android:layout_height=match_parent
tools:context=.MainActivity >
android:id=@+id/flipper
android:layout_width=fill_parent
android:layout_height=fill_parent/>

一個主類:

public class MainActivity extends Activity {
private ViewFlipper flipper;
private int[] resId = {R.drawable.pc1,R.drawable.pc2,R.drawable.pc3,R.drawable.pc4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flipper = (ViewFlipper) findViewById(R.id.flipper);
/*
* 動態(tài)導(dǎo)入的方式為ViewFlipper加入子View
* */
for (int i = 0; i < resId.length; i++) {
flipper.addView(getImageView(resId[i]));
}
/*
* 為ViewFlipper去添加動畫效果
* */
flipper.setInAnimation(this, R.anim.left_in);
flipper.setOutAnimation(this, R.anim.left_out);
flipper.setFlipInterval(5000);
flipper.startFlipping();
}
private ImageView getImageView(int resId){
ImageView image = new ImageView(this);
image.setBackgroundResource(resId);
return image;
}
}

那么這樣就實現(xiàn)了一個圖片輪詢的功能效果了

我們還可以添加點擊,滑動效果:

我們還需要添加兩個向右的滑動效果:

right_in:
android:fromXDelta=0
android:toXDelta=-100%p
android:duration=2000/>
right_out:
android:fromXDelta=100%p
android:toXDelta=0
android:duration=2000/>

然后我們還需要在主類里面添加(如果你不想讓圖片自動播放,只想通過手勢來實現(xiàn)圖片播放那么你需要把“為ViewFlipper添加動畫效果的代碼”刪掉):

publibooleaonTouchEvent(MotionEvenevent{
/TODAuto-generatemethostub
switc(event.getAction(){
casMotionEvent.ACTION_DOWN:
startevent.getX();
break;
casMotionEvent.ACTION_MOVE://判斷向左滑動還是向右滑動
i(event.getX(start100{
flipper.setInAnimation(thisR.anim.left_in);
flipper.setOutAnimation(thisR.anim.left_out);
flipper.showPrevious();
}elsi(startevent.getX(100{
flipper.setInAnimation(thisR.anim.right_in);
flipper.setOutAnimation(thisR.anim.right_out);
flipper.showNext();
}
casMotionEvent.ACTION_UP:
break;
}
retursuper.onTouchEvent(event);
}

這樣我們利用我們的ViewFlipper完成的圖片輪詢的功能就做完了。

以上所述是小編給大家分享的Android實現(xiàn)圖片輪播效果的兩種方法,希望大家喜歡。

相關(guān)文章

  • Android 實現(xiàn)代碼混淆的實例

    Android 實現(xiàn)代碼混淆的實例

    這篇文章主要介紹了Android 實現(xiàn)代碼混淆的實例的相關(guān)資料,希望通過本文大家能夠掌握Android代碼混淆的實現(xiàn)方法,需要的朋友可以參考下
    2017-09-09
  • Android自定義實現(xiàn)一個省份簡稱鍵盤

    Android自定義實現(xiàn)一個省份簡稱鍵盤

    這篇文章主要為大家詳細介紹了Android如何自定義實現(xiàn)一個省份簡稱鍵盤,可以用在車牌輸入等地方,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • Android ListView中headerview的動態(tài)顯示和隱藏的實現(xiàn)方法

    Android ListView中headerview的動態(tài)顯示和隱藏的實現(xiàn)方法

    這篇文章主要介紹了Android ListView中headerview的動態(tài)顯示和隱藏的實現(xiàn)方法的相關(guān)資料,這里提供兩種方法幫助實現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-08-08
  • Android SwipereFreshLayout下拉刷新

    Android SwipereFreshLayout下拉刷新

    這篇文章主要介紹了Android SwipereFreshLayout下拉刷新的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android自定義dialog可選擇展示年月日時間選擇欄

    Android自定義dialog可選擇展示年月日時間選擇欄

    這篇文章主要介紹了Android自定義dialog可選擇展示年月日時間選擇欄,非常不錯,具有參考借鑒價值,需要的的朋友參考下
    2017-03-03
  • Android 自定義日期段選擇控件功能(開始時間-結(jié)束時間)

    Android 自定義日期段選擇控件功能(開始時間-結(jié)束時間)

    這篇文章主要介紹了Android 自定義日期段選擇控件功能,開始時間-結(jié)束時間。本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Android文字基線Baseline算法的使用講解

    Android文字基線Baseline算法的使用講解

    今天小編就為大家分享一篇關(guān)于Android文字基線Baseline算法的使用講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • Android 通過TCP協(xié)議上傳指定目錄文件的方法

    Android 通過TCP協(xié)議上傳指定目錄文件的方法

    這篇文章主要介紹了Android 通過TCP協(xié)議上傳指定目錄文件的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • 詳解Android系統(tǒng)啟動過程

    詳解Android系統(tǒng)啟動過程

    這篇文章主要介紹了Android系統(tǒng)啟動過程的相關(guān)資料,幫助大家更好得理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Android圖片裁剪功能實現(xiàn)代碼

    Android圖片裁剪功能實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了Android圖片裁剪功能實現(xiàn)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11

最新評論