[Android Studio] -- Day 4 Gallery与EcoGallery

前言

  • 过年爽爽放,该回来复习复习拉WW,大家新年快乐

  • 今天原本是要来练习以前都没接触过的gallery 的,但是Android 4.1 的版本以後,已经不再支援。官方给出的可以用HorizontalScrollView 或者ViewPager 来代替它。

《Android Developers》
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已经被标记为过时,但他的展示效果还是杠杠的

    • 显现居中效果
    • 触摸滑动切换效果
    • 多页面翻转的效果(ViewPager只有3页可见)
    • 页面叠加效果(间距与层级的控制)
  • 在网路上查询的结果,貌似它每次切换图片时都要新建检视造成浪费太多的资源,所以被封杀了。详细原因在最下面参考资料,为了不占版面就不放这啦。监於很多人还在使用gallery,有大神在GitHub提供了自制元件『EcoGallery』,供大家优化gallery使用。
    --> 大神的github https://github.com/falnatsheh/EcoGallery <--

  • gallery长这样


正文

  • 安装libaray
    因为大神没有用JitPack,安装起来比较麻烦,不能直接用dependencies(手动也不行,血泪教训QAQ)
    1.下载并解压缩
    https://ithelp.ithome.com.tw/upload/images/20210223/20134772WnPcEYFSfi.png

    2.File>>New>>Import Moudle
    https://ithelp.ithome.com.tw/upload/images/20210223/20134772Xw08v7Hjpn.png

    3.选择路径
    你刚刚下载的档案,选择上面EcoGallery档案,不用EcoGallerySample
    然後next-->next-->next
    中间会跟你报错误讯息,没记错的话是跟你说minSDK的问题,就直接依照系统方法解决
    https://ithelp.ithome.com.tw/upload/images/20210223/201347727qPXvx80hK.png
    成功後,你旁边会多一点东西
    https://ithelp.ithome.com.tw/upload/images/20210223/201347729G9xTL7p3q.png

    4.build.gradle(app)
    最後的最後,还要在build.gradle(app)新增
    implementation project(":ecoGallery")
    https://ithelp.ithome.com.tw/upload/images/20210223/20134772W3d5CdjFND.png

  • code
    1.Xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <us.feras.ecogallery.EcoGallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

2.MainActivity

package com.example.ecogallary;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.BaseAdapter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import us.feras.ecogallery.EcoGallery;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EcoGallery ecoGallery = findViewById(R.id.gallery);
        ecoGallery.setAdapter(new ImageAdapter(this));
    }

    private class ImageAdapter extends BaseAdapter {
        private Context context;

        ImageAdapter(Context context) {
            this.context = context;
        }

        //取得 Gallery 列表 Item 的数量
        //要显示几张图片改数字
        public int getCount() {
            return 5;
        }

        //取得 Gallery 列表於 position 位置上的 Item
        public Object getItem(int position) {
            return position;
        }

        //取得 Gallery 列表於 position 位置上的 Item 的 ID
        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // Not using convertView for sample app simplicity.
            // You should probably use it in real application to get better performance.
            ImageView imageView = new ImageView(context);
            int resId;
            //图片切换
            switch (position) {
                case 0: resId = R.drawable.one;
                    break;
                case 1: resId = R.drawable.two;
                    break;
                case 2: resId = R.drawable.three;
                    break;
                case 3: resId = R.drawable.two;
                    break;
                case 4: resId = R.drawable.one;
                    break;
                default: resId = R.drawable.one;
            }
            imageView.setImageResource(resId);
            return imageView;
        }
    }
}

参考/引用资料


<<:  laravel8 10分钟保证完成

>>:  How to fix Gmail not receiving emails on iPhone?

DAY 7:Fan-Out Fan-In Pattern,看吧世界!这就是多人解决的力量!

什麽是 Fan-Out Fan-In Pattern? 将 input 由一个 producer 分...

树选手3号:XGboost

今天来认识一下kaggle比赛的常胜军-XGboost(extreme gradient boost...

HERE API Example - 显示 GeoJSON Data

GeoJSON 是一种基於 JSON 的地理空间数据交换格式,相关定义可参考 RFC 7946,一个...

Day-25 说明一下 Rails 的 MVC 架构是什麽?优点是?

我写的 Rails 的 R+MVC 笔记图 左上角的那个人是使用者,当使用者输入网址连上网址进入网...

3. 解释 Hoisting

在我们之前提到的Execution Context,都会执行一个被称为"Hoisting的...