Androidは、画像のロードショー効果を実現する2つの方法です。


皆さんはAPPを使っていますが、上のbannerの写真が交互に流れている効果がよく見られます。今日は一緒に勉強してみます。androidの中の写真ポーリングのいくつかの実現方法:
第一種類:アニメーションを使用した方法で実現する:(コードが煩雑である)
このような発給は必要です。二つのアニメーション効果、一つのレイアウト、一つのメインクラスが実現します。多くは言いません。コードを見てください。

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/>
 第二種類:View Flippperを使用して、画像のルーレットを実現する。
Androidシステムが持つ複数ページの管理コントロールは、サブインターフェースの自動切り替えが可能です。
まずView FlipperのためにViewに参加する必要があります。
(1)静的導入:ラyoutレイアウトファイルに直接導入する
(2)動的導入:addView()方法
View Plipperの一般的な方法:
set InAnimation:Viewを設定してスクリーンに入る時に使うアニメーションです。
setOutAnimation:Viewを設定して画面から退出する時に使うアニメーションです。
show Next:この関数を呼び出してView Flipperの中の次のViewを表示します。
showProvious:この関数を呼び出してView Flipperの中の前のViewを表示します。
set Flip Interval:View間で切り替える時間間隔を設定します。
startFlippingは上で設定した時間間隔を使ってすべてのViewを切り替え、切り替えはループします。
stopFlipping:Viewの切り替えを停止します。
こんなにたくさん話しましたが、今日実現するものは何ですか?
(1)View Flippperを利用して画像のロードショーを実現する
(2)ジェスチャーの滑りに対応するVieFlipper
まず何枚かの写真を準備します。画像をdrawableに入れます。
二つのアニメーションを作成します。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);
/*
*         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;
}
}
これで1つの画像ポーリングの機能効果が実現されます。
クリックも追加できます。スライド効果:
右にスライド効果を2つ追加する必要があります。

right_in:
android:fromXDelta=0
android:toXDelta=-100%p
android:duration=2000/>
right_out:
android:fromXDelta=100%p
android:toXDelta=0
android:duration=2000/>
その後、メインクラスに追加したいです。

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);
}
このように私達は私達のView Flipperで完成した写真ポーリングの機能を利用して完成しました。
以上は小编が皆さんに共有するAndroidの画像のローテーション効果を実现する二つの方法です。