您好,登錄后才能下訂單哦!
ImageSwitcher:
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.p_w_picpathswitch.MainActivity" > <ImageSwitcher android:id="@+id/p_w_picpathSwitcher1_1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" > </ImageSwitcher> </RelativeLayout> <!-- android:inAnimation="@android:anim/slide_in_left" android:outAnimation="@android:anim/slide_out_right" -->
MianActivity
package com.example.p_w_picpathswitch; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity implements ViewFactory ,OnTouchListener{ private ImageSwitcher p_w_picpathSwitch; private int [] p_w_picpaths={ R.drawable.kobe0, R.drawable.kobe1, R.drawable.kobe3, R.drawable.kobe4, R.drawable.kobe6, }; private int index; //要顯示的圖片的下標(biāo) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); p_w_picpathSwitch=(ImageSwitcher) findViewById(R.id.p_w_picpathSwitcher1_1); //設(shè)置創(chuàng)建ImageView的工廠(chǎng) p_w_picpathSwitch.setFactory(this); //設(shè)置觸屏事件 p_w_picpathSwitch.setOnTouchListener(this); } //ViewFactory工廠(chǎng)接口的方法,通過(guò)這個(gè)方法,給ImageSwitch組件提供兩個(gè) @Override public View makeView() { // TODO Auto-generated method stub ImageView p_w_picpathView=new ImageView(this); p_w_picpathView.setImageResource(p_w_picpaths[0]); return p_w_picpathView; } //定義兩個(gè)x坐標(biāo)點(diǎn) float startx=0.0F; //開(kāi)始位置 float endx=0.0F; //結(jié)束位置 //觸屏事件監(jiān)聽(tīng)方法 @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_DOWN){ startx=event.getX(); return true; }else if(event.getAction()==MotionEvent.ACTION_UP){ endx=event.getX(); //判斷左滑動(dòng) if(startx-endx>20){ index=(index+1)<(p_w_picpaths.length-1)?++index:0; p_w_picpathSwitch.setImageResource(p_w_picpaths[index]); //在代碼中設(shè)置動(dòng)畫(huà)效果 p_w_picpathSwitch.setInAnimation(this, android.R.anim.fade_in); p_w_picpathSwitch.setOutAnimation(this, android.R.anim.fade_out); } //判斷右滑動(dòng) if(endx-startx>20){ index=(index-1)>0?--index:p_w_picpaths.length-1; p_w_picpathSwitch.setImageResource(p_w_picpaths[index]); p_w_picpathSwitch.setInAnimation(this, android.R.anim.slide_in_left); p_w_picpathSwitch.setOutAnimation(this, android.R.anim.slide_out_right); } } return true; } }
TextSwitcher與ImageSwitcher類(lèi)似,只需要將ImageSwitcher換成TextSwitcher
設(shè)置圖片資源修改為設(shè)置文本即可
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。