今天看书的时候看到图片的处理 其实我们最关心的是图片的显示效果 用Gallery与Imageswitch完美结合 给人眼前一亮的清醒感 请看效果吧
最侯就是要实现通过滑动上面的Gallery动态的在下面的Imageswitch控件里显示出来
首先在main.xml里添加这两个控件
<Gallery
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/gallery_id"
android:layout_marginTop="20dp"
/>
<ImageSwitcher
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/imageSwitcher_id"
android:layout_marginTop="30dp"
></ImageSwitcher>
然后在MainActivity中的代码编写了
package com.example.gallery;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity implements ViewFactory , OnItemSelectedListener{
private Gallery gallery;
private ImageAdaper imageAdaper;
private int[] imageIds =new int[]{
R.drawable.pic1 , R.drawable.pic2,
R.drawable.pic3 , R.drawable.pic4,
R.drawable.pic5 , R.drawable.pic6,
R.drawable.pic7 , R.drawable.pic8,
};
private ImageSwitcher imageSwitcher;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageAdaper = new ImageAdaper(this);
gallery = (Gallery)findViewById(R.id.gallery_id);
gallery.setOnItemSelectedListener(this);
imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher_id);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
gallery.setAdapter(imageAdaper);
}
public class ImageAdaper extends BaseAdapter{
Context context;
int mGalleryItemBackground;
public ImageAdaper(Context context){
this.context = context;
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
}
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return imageIds[position];
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageview = new ImageView(this.context);
imageview.setBackgroundResource(imageIds[position % imageIds.length]);
imageview.setScaleType(ImageView.ScaleType.FIT_XY);
imageview.setLayoutParams(new Gallery.LayoutParams(136 , 88));
return imageview;
}
}
public View makeView() {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(this);
imageView.setScaleType(imageView.getScaleType().FIT_CENTER);
imageView.setBackgroundColor(0xFF000000);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT));
return imageView;
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
imageSwitcher.setImageResource(imageIds[position%imageIds.length]);
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
这样就OK了 也行看看。