Android-使用Gallery制作图片浏览册
严格来说,Gallery在高于或者等于API16的版本中是过时的工具类,不过在复习Android基础的时候
遇见了,还是写几个例子复习一下,取代Gallery的是HorizontalScroll 和ViewPager
不过Gallery做出的效果还是可以的。
官方文档描述:
This class was deprecated in API level 16.
This widget is no longer supported. Other horizontally scrolling widgets include HorizontalScrollView and ViewPager from the support library.
在这里除了使用到了Gallery还用到了ImageSwitcher因为可以设置动画效果,
看起来也非常炫。 嘿嘿
首先是布局文件:
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Gallery
android:id="@+id/id_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ImageSwitcher
android:id="@+id/id_imageSwitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ImageSwitcher>
</LinearLayout>
和之前的ListView和ViewPager一样都要有适配器,这里是自定义适配器,继承BaseAdapter
ImageAdapter
package com.xieth.as.gallerydemo;
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 YR on 2016/04/10.
*/
public class ImageAdapter extends BaseAdapter{
private int[]res ;
private Context context;
public ImageAdapter(int []res, Context context) {
this.res = res;
this.context = context;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
return res[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.setBackgroundResource(res[position%res.length]);
imageView.setLayoutParams(new Gallery.LayoutParams(200, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
}
}
接下来就是主活动进行获取并且设置监听事件:
需要实现两个接口AdapterView.OnItemSelectedListener和ViewSwitcher.ViewFactory
package com.xieth.as.gallerydemo;
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[] resId = {R.mipmap.item1, R.mipmap.item2, R.mipmap.item3, R.mipmap.item4,
R.mipmap.item5, R.mipmap.item6, R.mipmap.item7, R.mipmap.item8,
R.mipmap.item9, R.mipmap.item10, R.mipmap.item11, R.mipmap.item12};
private Gallery gallery = null;
private ImageAdapter adapter = null;
private ImageSwitcher imageSwitcher = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
adapter = new ImageAdapter(resId, this);
gallery.setAdapter(adapter);
gallery.setOnItemSelectedListener(this);
imageSwitcher.setFactory(this);
// 设置动画效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
}
private void initViews() {
gallery = (Gallery) findViewById(R.id.id_gallery);
imageSwitcher = (ImageSwitcher) findViewById(R.id.id_imageSwitcher);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
imageSwitcher.setImageResource(resId[position%resId.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageView;
}
}
运行效果:
推荐一篇关于Gallery的博客点击进入
OK
记录一下。。