Gallery is deprecated『Android系列十』

来源:互联网 发布:淘宝付款人数多久更新 编辑:程序博客网 时间:2024/05/23 00:36

        在学习Gallery控件时,IDE提示已经过期,这是怎么回事呢?又开始搜索大法,说句题外话,百度真心残废,在google上找到这样的一个和我有同样疑问的页面:http://stackoverflow.com/questions/11868503/the-type-gallery-is-deprecated-whats-the-best-alternative

        简单翻译一下,英文不太好,凑活着看吧:

        Q:I was really surprised that such a Widget gets deprecated.
        I want a simple gallery that scrolls left and right, shows a picture on the whole Activity screen, and most important is that you cant swipe more than 1 image in any direction, even if the scroll speed is fast it changes to the next image.
       So which Widget should I use? Or should I use a simple ImageView and handle all the swipes and add an animation?

        问:很奇怪这个控件已经过期。

        我想实现一个简单的画廊效果:一系列图片水平依次排列,每张都填充满整个屏幕,当滚动条左右滑动一定程度时,会显示下一张图片,而且任何方向上你不能拉动超过一张图片(感觉最后这句话太多余了)。

        这个控件过期了,该换用哪个?还是使用简单的图像视图加上动画处理来达到我要的效果?

        A:HorizontalScrollView will be closer to what you are looking for I think.
        I suspect that Gallery was deprecated because it did not properly use convertView with its adapter. Which meant that it had to create a new view for every item which was a drain on performance.
        Another option you have is to use the 3rd party created EcoGallery which someone created to overcome the but that caused Gallery not to recycle its Views. Unfortunately that pastebin link is the only reference to it I can find online now.

       答:我想HorizontalScrollView这个控件最符合你的要求。

       我怀疑Gallery过期的原因是因为它的适配器不能合适的转换视图,它每次切换图片时都要新建视图造成浪费太多的资源。

       另外你可以选择使用The third part created ecogallery,它克服了gallery不能回收视图的缺点;但不幸的是,通过网络只在pastebin上找到它的有关资料。(这句话无视吧,那个链接访问不了)


       好了,问题已经明确了,Android源生的gallery的确因为生理缺陷已经被抛弃了,投入到其他开源Gallery系列的怀抱吧。

       另外打开SDK的文档,找到Gallery看到有这么一句话:This class is deprecated.This widget is no longer supported. Other horizontally scrolling widgets include HorizontalScrollView and ViewPager from the support library.同样的,官方提示用HSV和VP代替。

       附上已经被淘汰的Gallery效果演示,main.java:(图片自行放到合适的drawable文件夹中)

package com.dazlly;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.app.Activity;import android.content.Context;@SuppressWarnings("deprecation")public class Main extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);findViews();}private void findViews() {((Gallery) findViewById(R.id.galleryFirst)).setAdapter(new ImageAdapter(this));}public class ImageAdapter extends BaseAdapter {ImageAdapter(Context c) {this.myContext = c;}private Context myContext;private int[] myImageIds = { R.drawable.sea, R.drawable.cat,R.drawable.dog, R.drawable.effer, R.drawable.girl,R.drawable.green, R.drawable.guitar, R.drawable.horse };@Overridepublic int getCount() {return this.myImageIds.length;}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View v, ViewGroup parent) {ImageView i = new ImageView(this.myContext);i.setImageResource(this.myImageIds[position]);i.setScaleType(ImageView.ScaleType.FIT_CENTER);i.setLayoutParams(new Gallery.LayoutParams(480, 800));return i;}public float getScale(boolean focused, int offset) {return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));}}}

        main.xml

<RelativeLayout 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" >    <Gallery        android:id="@+id/galleryFirst"        android:layout_width="match_parent"        android:layout_height="match_parent"         android:spacing="2dp"/></RelativeLayout>