Gallery可以使内部元素水平滚动并将选择的子元素设置在中间位置,一般用来浏览图片
ImageSwitcher可以显示图片,主要用于多张图片的切换显示
用法都比较简单,下面使用Gallery+ImageSwitcher来实现一个简单的图片浏览器
activity_main.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical"
android:padding="20dp">
<Gallery
android:id="@+id/gl"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageSwitcher
android:id="@+id/is"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
</LinearLayout>
MyAdapter:
[Java] 纯文本查看 复制代码 import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
/**
* Created by Administrator on 2019/1/4.
*/
public class MyAdapter extends BaseAdapter {
private Context context;
private int[] imgList;
public MyAdapter(Context context, int[] imgList) {
this.context = context;
this.imgList = imgList;
}
//返回Integer.MAX_VALUE是为了图片可以无限循环
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
return imgList[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(imgList[position % imgList.length]);
imageView.setLayoutParams(new Gallery.LayoutParams(300, 200));
return imageView;
}
}
MainActivity:[Java] 纯文本查看 复制代码 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
private int[] imgs = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6};
private Gallery mGallery;
private ImageSwitcher mImageSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mGallery = (Gallery) findViewById(R.id.gl);
mImageSwitcher = (ImageSwitcher) findViewById(R.id.is);
MyAdapter adapter = new MyAdapter(this, imgs);
mGallery.setAdapter(adapter);
mGallery.setOnItemSelectedListener(this);
mImageSwitcher.setFactory(this);
//添加动画
mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha_in));
mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha_out));
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
mImageSwitcher.setImageResource(imgs[position % imgs.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageView;
}
}
alpha_in.xml:[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1000"
android:fromAlpha="0.1"
android:toAlpha="1.0" />
</set> alpha_out.xml:[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
效果图:
|