ImageSwitcher |
Top Previous Next |
|
Tela
Java
package com.laioutes;
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.Gallery.LayoutParams; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher;
public class LayoutsActivity extends Activity implements ViewSwitcher.ViewFactory {
// Planetas private int[] imagens = { R.drawable.mercurio, R.drawable.venus, R.drawable.terra, R.drawable.marte, R.drawable.jupiter, R.drawable.saturno, R.drawable.urano, R.drawable.netuno, R.drawable.plutao }; private ImageSwitcher imageSwitcher;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gridview);
// Configura o ImageSwitcher e os efeitos imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); imageSwitcher.setFactory(this); imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); // imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); //
// Configura o adaptador da galeria Gallery g = (Gallery) findViewById(R.id.gallery); g.setAdapter(new AdaptadorImagem(this, imagens, new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))); g.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView parent, View v, int posicao, long id) { // Avisa o ImageSwitcher que a imagem foi alterada imageSwitcher.setImageResource(imagens[posicao]); }
public void onNothingSelected(AdapterView parent) { } }); }
// Implementa "ViewSwitcher.ViewFactory" public View makeView() { ImageView img = new ImageView(this); img.setBackgroundColor(0xFF000000); img.setScaleType(ImageView.ScaleType.FIT_CENTER); img.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return img; } }
xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Exemplo de ImageSwitcher" />
<ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="305px" android:layout_height="305px" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" />
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" />
</LinearLayout>
AdaptadorImagem.java
package com.laioutes;
import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.BaseAdapter; import android.widget.ImageView;
public class AdaptadorImagem extends BaseAdapter {
private Context ctx; private final int[] imagens; private final LayoutParams params;
public AdaptadorImagem(Context c, int[] imagens, LayoutParams params) { this.ctx = c; this.imagens = imagens; this.params = params; }
public int getCount() { return imagens.length; }
public Object getItem(int posicao) { return posicao; }
public long getItemId(int posicao) { return posicao; }
public View getView(int posicao, View convertView, ViewGroup parent) { ImageView img = new ImageView(ctx); img.setImageResource(imagens[posicao]); img.setAdjustViewBounds(true); img.setLayoutParams(params); return img; }
} |