search
HomeBackend DevelopmentPHP TutorialAndroid UI control series: Gallery (gallery view)

Gallery can display its content horizontally, and is generally used to browse pictures. The selected option is in the middle, and information can be displayed corresponding to events. The following is combined with the ImageSwitcher component to implement a program for browsing pictures through thumbnails. The specific steps are as follows

Step 1:

Create an Andorid project "GalleryTest", the entry of which is the Activity class GalleryTest inherits Activity and implements the OnItemSelectedListener and ViewFactory interfaces to create pictures and views

package org.hualang.Gallery;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ViewSwitcher.ViewFactory;
//继承Activity,实现onItemSelectedListener和ViewFactory接口
public class GalleryTest extends Activity implements OnItemSelectedListener,ViewFactory{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

        @Override
        public View makeView() {
                // TODO Auto-generated method stub
                return null;
        }

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                // TODO Auto-generated method stub

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

        }
}

Step 2:

Add 7 pictures and corresponding images in the res\drawable\ directory of the project Thumbnail

Step 3:

Create a layout file main.xml in the project res\layout\ directory, add a Gallery component and an ImageSwitcher component to it, and set the corresponding Properties

<?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"  
    >  
<ImageSwitcher android:id="@+id/switcher"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:layout_alignParentTop="true"  
        android:layout_alignParentLeft="true"  
    />  
  
    <Gallery android:id="@+id/gallery"  
        android:background="#55000000"  
        android:layout_width="fill_parent"  
        android:layout_height="60dp"  
        android:layout_alignParentBottom="true"  
        android:layout_alignParentLeft="true"  
        android:gravity="center_vertical"  
        android:spacing="16dp"  
    />  
</LinearLayout>

Step 4: Declare the used ImageSwitcher instance image resource Integer array at the top of GalleryTest

public class GalleryTest extends Activity implements OnItemSelectedListener,ViewFactory{
    /** Called when the activity is first created. */
        //声明ImageSwitcher
        private ImageSwitcher switcher;
        //缩略图片id数组
        private Integer[] thumbids={
                        R.drawable.thumb0,
                        R.drawable.thumb1,
                        R.drawable.thumb2,
                        R.drawable.thumb3,
                        R.drawable.thumb4,
                        R.drawable.thumb5,
                        R.drawable.thumb6,
                        R.drawable.thumb7
        };
        //图片id数组
        private Integer[] imgids={
                        R.drawable.img0,
                        R.drawable.img1,
                        R.drawable.img2,
                        R.drawable.img3,
                        R.drawable.img4,
                        R.drawable.img5,
                        R.drawable.img6,
                        R.drawable.img7
        };

Step 5:

In GalleryTest’s onCreate( ) method, set the window style to untitled, set the current layout view, obtain the ImageSwitcher instance, set the gradual fade-out animation, and obtain the Gallery instance

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置窗口特征无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        //通过findViewById方法获得ImageSwitcher对象
        switcher=(ImageSwitcher)findViewById(R.id.switcher);
        //为ImageSwitcher设置工厂
        switcher.setFactory(this);
        //设置动画渐入效果
        switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        //设置动画渐出效果
        switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        //通过findViewById方法获得Gallery对象
        Gallery g=(Gallery)findViewById(R.id.gallery);
    }

Step 6:

Create the internal Class ImageAdapter, which inherits BaseAdapter and sets an Adapter instance for Gallery

public class ImageAdapter extends BaseAdapter {
            //构造方法
                public ImageAdapter(Context c) {
                        mContext = c;
                }
                //获得数量
                public int getCount() {
                        return thumbids.length;
                }
                //获得当前选项
                public Object getItem(int position) {
                        return position;
                }
                //获得当前选项ID
                public long getItemId(int position) {
                        return position;
                }
                //获得View对象
                public View getView(int position, View convertView, ViewGroup parent) {
                        //实例化ImageView对象
                        ImageView i = new ImageView(mContext);
                        //设置缩略图片资源
                        i.setImageResource(thumbids[position]);
                        //设置边界对齐
                        i.setAdjustViewBounds(true);
                        //设置布局参数
                        i.setLayoutParams(new Gallery.LayoutParams(
                                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                        //设置背景资源
                        i.setBackgroundResource(R.drawable.picturefrom);
                        return i;
                }
                private Context mContext;
        }

Step 7:

Implement the onItemSelected() method and replace the image

@Override  
        public void onItemSelected(AdapterView<?> adapter, View v, int position,  
                        long id) {  
                switcher.setImageResource(imgids[position]);  
        }

Eighth Step:

Implement the makeView() method and set the layout format for ImageView

@Override  
        public View makeView() {  
                // TODO Auto-generated method stub  
                //创建ImageView  
                ImageView i=new ImageView(this);  
                //设置背景颜色  
                i.setBackgroundColor(0xFF000000);  
                //设置精度类型  
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);  
                //设置布局参数  
                i.setLayoutParams(new ImageSwitcher.LayoutParams(  
                                LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));  
                return i;  
        }

Step 9:

Add Adapter to Gallery and add OnItemSelectedListener listener

g.setAdapter(new ImageAdapter(this));  
                g.setOnItemSelectedListener(this);

At this point, everything is over. The running results are as follows

Android UI control series: Gallery (gallery view)

Complete source code:

package org.hualang.Gallery;<br><br>import android.app.Activity;<br>import android.content.Context;<br>import android.os.Bundle;<br>import android.view.View;
<br>import android.view.ViewGroup;<br>import android.view.Window;<br>import android.view.animation.AnimationUtils;<br>import android.widget.AdapterView;<br>
import android.widget.BaseAdapter;<br>import android.widget.Gallery;<br>import android.widget.ImageSwitcher;<br>import android.widget.ImageView;<br>
import android.widget.AdapterView.OnItemSelectedListener;<br>import android.widget.Gallery.LayoutParams;<br>import android.widget.ViewSwitcher.ViewFactory;<br><br>
public class GalleryTest extends Activity implements OnItemSelectedListener,<br>                ViewFactory {<br><br>        
private ImageSwitcher mSwitcher;<br><br>        private Integer[] mThumbIds = { R.drawable.thumb0,<br>                        R.drawable.thumb1, R.drawable.thumb2,
<br>                        R.drawable.thumb3, R.drawable.thumb4,<br>                        R.drawable.thumb5, R.drawable.thumb6,<br>                        
R.drawable.thumb7 };<br><br>        private Integer[] mImageIds = { R.drawable.img0, R.drawable.img1,<br>                        R.drawable.img2, R.drawable.img3,
 R.drawable.img4,<br>                        R.drawable.img5, R.drawable.img6, R.drawable.img7 };<br><br>        @Override<br>        
 public void onCreate(Bundle savedInstanceState) {<br>                super.onCreate(savedInstanceState);<br><br>                
 requestWindowFeature(Window.FEATURE_NO_TITLE);<br>                setContentView(R.layout.main);<br>                
 mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);<br>                mSwitcher.setFactory(this);<br>                
 mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,<br>                                android.R.anim.fade_in));<br>                
 mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,<br>                                android.R.anim.fade_out));<br><br>                
 Gallery g = (Gallery) findViewById(R.id.gallery);<br><br>                g.setAdapter(new ImageAdapter(this));
                g.setOnItemSelectedListener(this);<br><br>        }<br><br>        public class ImageAdapter extends BaseAdapter {<br>                
                public ImageAdapter(Context c) {<br>                        mContext = c;<br>                }<br>                public int getCount() {
                <br>                        return mThumbIds.length;<br>                }<br>                public Object getItem(int position) {
                <br>                        return position;<br>                }<br>                public long getItemId(int position) {
                <br>                        return position;<br>                }<br>                public View getView(int position, View convertView, ViewGroup 
                parent) {<br>                        ImageView i = new ImageView(mContext);<br><br>                        i.setImageResource(mThumbIds[position]);
                <br>                        i.setAdjustViewBounds(true);<br>                        
                i.setLayoutParams(new Gallery.LayoutParams(<br>                                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                <br>                        i.setBackgroundResource(R.drawable.picturefrom);<br>                        return i;<br>                
                }<br>                private Context mContext;<br>        }<br><br>        @Override<br>        public void onItemSelected(AdapterView<?> adapter, 
                View v, int position,<br>                        long id) {<br>                mSwitcher.setImageResource(mImageIds[position]);<br>        }<br>
                <br>        @Override<br>        public void onNothingSelected(AdapterView<?> arg0) {<br><br>        }<br><br>        @Override<br>        
                public View makeView() {<br>                ImageView i = new ImageView(this);<br>                i.setBackgroundColor(0xFF000000);
                <br>                i.setScaleType(ImageView.ScaleType.FIT_CENTER);<br>                i.setLayoutParams(new ImageSwitcher.LayoutParams(<br>      
                                          LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));<br>                return i;<br>        }<br>}
<?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 android:layout_width="fill_parent"  
 android:layout_height="fill_parent">  
<ImageSwitcher android:id="@+id/switcher"  
 android:layout_width="fill_parent"  
 android:layout_height="fill_parent"  
 android:layout_alignParentTop="true"  
 android:layout_alignParentLeft="true"  
 />  
<Gallery android:id="@+id/gallery"  
 android:background="#55000000"  
 android:layout_width="fill_parent"  
 android:layout_height="60dp"  
 android:layout_alignParentBottom="true"  
 android:layout_alignParentLeft="true"  
 android:gravity="center_vertical"  
 android:spacing="16dp"  
 />  
</RelativeLayout>

The above is the Android UI control series: Gallery (Gallery view), please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何在 iPhone 和 Android 上关闭蓝色警报如何在 iPhone 和 Android 上关闭蓝色警报Feb 29, 2024 pm 10:10 PM

根据美国司法部的解释,蓝色警报旨在提供关于可能对执法人员构成直接和紧急威胁的个人的重要信息。这种警报的目的是及时通知公众,并让他们了解与这些罪犯相关的潜在危险。通过这种主动的方式,蓝色警报有助于增强社区的安全意识,促使人们采取必要的预防措施以保护自己和周围的人。这种警报系统的建立旨在提高对潜在威胁的警觉性,并加强执法机构与公众之间的沟通,以共尽管这些紧急通知对我们社会至关重要,但有时可能会对日常生活造成干扰,尤其是在午夜或重要活动时收到通知时。为了确保安全,我们建议您保持这些通知功能开启,但如果

在Android中实现轮询的方法是什么?在Android中实现轮询的方法是什么?Sep 21, 2023 pm 08:33 PM

Android中的轮询是一项关键技术,它允许应用程序定期从服务器或数据源检索和更新信息。通过实施轮询,开发人员可以确保实时数据同步并向用户提供最新的内容。它涉及定期向服务器或数据源发送请求并获取最新信息。Android提供了定时器、线程、后台服务等多种机制来高效地完成轮询。这使开发人员能够设计与远程数据源保持同步的响应式动态应用程序。本文探讨了如何在Android中实现轮询。它涵盖了实现此功能所涉及的关键注意事项和步骤。轮询定期检查更新并从服务器或源检索数据的过程在Android中称为轮询。通过

如何在Android中实现按下返回键再次退出的功能?如何在Android中实现按下返回键再次退出的功能?Aug 30, 2023 am 08:05 AM

为了提升用户体验并防止数据或进度丢失,Android应用程序开发者必须避免意外退出。他们可以通过加入“再次按返回退出”功能来实现这一点,该功能要求用户在特定时间内连续按两次返回按钮才能退出应用程序。这种实现显著提升了用户参与度和满意度,确保他们不会意外丢失任何重要信息Thisguideexaminesthepracticalstepstoadd"PressBackAgaintoExit"capabilityinAndroid.Itpresentsasystematicguid

Android逆向中smali复杂类实例分析Android逆向中smali复杂类实例分析May 12, 2023 pm 04:22 PM

1.java复杂类如果有什么地方不懂,请看:JAVA总纲或者构造方法这里贴代码,很简单没有难度。2.smali代码我们要把java代码转为smali代码,可以参考java转smali我们还是分模块来看。2.1第一个模块——信息模块这个模块就是基本信息,说明了类名等,知道就好对分析帮助不大。2.2第二个模块——构造方法我们来一句一句解析,如果有之前解析重复的地方就不再重复了。但是会提供链接。.methodpublicconstructor(Ljava/lang/String;I)V这一句话分为.m

如何在2023年将 WhatsApp 从安卓迁移到 iPhone 15?如何在2023年将 WhatsApp 从安卓迁移到 iPhone 15?Sep 22, 2023 pm 02:37 PM

如何将WhatsApp聊天从Android转移到iPhone?你已经拿到了新的iPhone15,并且你正在从Android跳跃?如果是这种情况,您可能还对将WhatsApp从Android转移到iPhone感到好奇。但是,老实说,这有点棘手,因为Android和iPhone的操作系统不兼容。但不要失去希望。这不是什么不可能完成的任务。让我们在本文中讨论几种将WhatsApp从Android转移到iPhone15的方法。因此,坚持到最后以彻底学习解决方案。如何在不删除数据的情况下将WhatsApp

同样基于linux为什么安卓效率低同样基于linux为什么安卓效率低Mar 15, 2023 pm 07:16 PM

原因:1、安卓系统上设置了一个JAVA虚拟机来支持Java应用程序的运行,而这种虚拟机对硬件的消耗是非常大的;2、手机生产厂商对安卓系统的定制与开发,增加了安卓系统的负担,拖慢其运行速度影响其流畅性;3、应用软件太臃肿,同质化严重,在一定程度上拖慢安卓手机的运行速度。

Android中动态导出dex文件的方法是什么Android中动态导出dex文件的方法是什么May 30, 2023 pm 04:52 PM

1.启动ida端口监听1.1启动Android_server服务1.2端口转发1.3软件进入调试模式2.ida下断2.1attach附加进程2.2断三项2.3选择进程2.4打开Modules搜索artPS:小知识Android4.4版本之前系统函数在libdvm.soAndroid5.0之后系统函数在libart.so2.5打开Openmemory()函数在libart.so中搜索Openmemory函数并且跟进去。PS:小知识一般来说,系统dex都会在这个函数中进行加载,但是会出现一个问题,后

Android APP测试流程和常见问题是什么Android APP测试流程和常见问题是什么May 13, 2023 pm 09:58 PM

1.自动化测试自动化测试主要包括几个部分,UI功能的自动化测试、接口的自动化测试、其他专项的自动化测试。1.1UI功能自动化测试UI功能的自动化测试,也就是大家常说的自动化测试,主要是基于UI界面进行的自动化测试,通过脚本实现UI功能的点击,替代人工进行自动化测试。这个测试的优势在于对高度重复的界面特性功能测试的测试人力进行有效的释放,利用脚本的执行,实现功能的快速高效回归。但这种测试的不足之处也是显而易见的,主要包括维护成本高,易发生误判,兼容性不足等。因为是基于界面操作,界面的稳定程度便成了

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use