Android實現(xiàn)圖片輪播效果的兩種方法
大家在使用APP的過程中,經常會看到上部banner圖片輪播的效果,那么今天我們就一起來學習一下,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)導入:在layout布局文件中直接導入
(2) 動態(tài)導入:addView()方法
ViewPlipper常用方法:
setInAnimation:設置View進入屏幕時候使用的動畫
setOutAnimation:設置View退出屏幕時候使用的動畫
showNext:調用該函數(shù)來顯示ViewFlipper里面的下一個View
showPrevious:調用該函數(shù)來顯示ViewFlipper里面的上一個View
setFlipInterval:設置View之間切換的時間間隔
startFlipping使用上面設置的時間間隔來開始切換所有的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)導入的方式為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)圖片輪播效果的兩種方法,希望大家喜歡。
相關文章
Android ListView中headerview的動態(tài)顯示和隱藏的實現(xiàn)方法
這篇文章主要介紹了Android ListView中headerview的動態(tài)顯示和隱藏的實現(xiàn)方法的相關資料,這里提供兩種方法幫助實現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08
Android SwipereFreshLayout下拉刷新
這篇文章主要介紹了Android SwipereFreshLayout下拉刷新的相關資料,需要的朋友可以參考下2017-06-06
Android 自定義日期段選擇控件功能(開始時間-結束時間)
這篇文章主要介紹了Android 自定義日期段選擇控件功能,開始時間-結束時間。本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Android 通過TCP協(xié)議上傳指定目錄文件的方法
這篇文章主要介紹了Android 通過TCP協(xié)議上傳指定目錄文件的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03

