Android中使用GridView和ImageViewSwitcher实现电子相册简单功能实例

Android中使用GridView和ImageViewSwitcher实现电子相册简单功能实例,第1张

概述我们在手机上查看相册时,首先看到的是网格状的图片展示界面,然后我们选择想要欣赏的照片点击进入,这样就可以全屏观看该照片,并且可以通过左右滑动来切换照片。如下图的显示效果:

我们在手机上查看相册时,首先看到的是网格状的图片展示界面,然后我们选择想要欣赏的照片点击进入,这样就可以全屏观看该照片,并且可以通过左右滑动来切换照片。如下图的显示效果:

实现Activity之间的跳转以及照片标记位置的传递需要用到intent,并分别使用putExtra以及getExtra,传入和获取照片的标记位置。

(关于intent,后期会有专门博文介绍具体使用,请大家持续关注哦)

下面我们开始功能的实现:

第一步:Layout中建立首页GrIDVIEw布局grID_layout.xml文件:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:orIEntation="vertical" androID:layout_wIDth="match_parent"  androID:layout_height="match_parent">  <GrIDVIEw    androID:ID="@+ID/gv"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:numColumns="auto_fit"    androID:verticalSpacing="10dp"    androID:gravity="center"    androID:horizontalSpacing="10dp"></GrIDVIEw></linearLayout>

第二步:Layout中建立GrIDVIEw布局中每个item的布局grIDitem_layout.xml文件:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:app="http://schemas.androID.com/apk/res-auto"  androID:orIEntation="vertical" androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:gravity="center">  <ImageVIEw    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:adjustVIEwBounds="true"    androID:maxWIDth="280dp"    androID:maxHeight="280dp"    androID:src="@mipmap/a1"    androID:ID="@+ID/imageVIEw" /></linearLayout>

这里的设置需要根据实际展示图片的宽度以及要展示的容器(手机)分辨率来设置等比例缩放,避免排版混乱的情况出现。

第三步:Layout中建立图片展示界面(包含导航圆点)布局activity_main.xml文件:

这里主布局使用FrameLayout,切换实现布局使用ImageSwitcher,导航圆点使用linearlayout实现(可通过配置文件实现):

<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:tools="http://schemas.androID.com/tools"  androID:ID="@+ID/activity_main"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  tools:context="com.example.administrator.switcher.MainActivity">  <ImageSwitcher    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:ID="@+ID/is">  </ImageSwitcher>  <linearLayout    androID:ID="@+ID/point_layout"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:layout_gravity="bottom"    androID:orIEntation="horizontal">    <ImageVIEw      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content"      androID:layout_weight="1"      androID:src="@mipmap/default_holo"/>    <ImageVIEw      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content"      androID:layout_weight="1"      androID:src="@mipmap/default_holo"/>    <ImageVIEw      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content"      androID:layout_weight="1"      androID:src="@mipmap/default_holo"/>    <ImageVIEw      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content"      androID:layout_weight="1"      androID:src="@mipmap/default_holo"/>  </linearLayout></FrameLayout>

第四步:Java中Activity的实现代码,首页GrIDVIEw的实现代码GrIDActivity.java:

本次自定义适配器中getvIEw方法已经优化:

import androID.content.Intent;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.AdapterVIEw;import androID.Widget.BaseAdapter;import androID.Widget.GrIDVIEw;import androID.Widget.ImageVIEw;public class GrIDActivity extends AppCompatActivity {  private GrIDVIEw gv;  int[] images={R.mipmap.a1,R.mipmap.a2,R.mipmap.a3,R.mipmap.a4};  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.grID_layout);    gv= (GrIDVIEw) findVIEwByID(R.ID.gv);    gv.setAdapter(new MyAdapter());    //设置单击GrIDVIEw中每个item的单击事件    gv.setonItemClickListener(new AdapterVIEw.OnItemClickListener() {      @OverrIDe      public voID onItemClick(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) {        //使用intend获取要交互的Activity,也就是将要跳转的界面        Intent intent = new Intent(GrIDActivity.this,MainActivity.class);        //通过intent的putExtra方法获取点击图片的下标位置(用于Activity之间数据传输)        intent.putExtra("select",position);        //启动要交互的Activity(通过传入包含该Activity的intent)        startActivity(intent);      }    });  }  class MyAdapter extends BaseAdapter{    @OverrIDe    public int getCount() {      return images.length;    }    @OverrIDe    public Object getItem(int position) {      return images[position];    }    @OverrIDe    public long getItemID(int position) {      return position;    }    @OverrIDe    public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {      VIEwHolder vh;      if(convertVIEw==null){        convertVIEw=getLayoutInflater().inflate(R.layout.grIDitem_layout,null);        vh= new VIEwHolder();        vh.iv= (ImageVIEw) convertVIEw.findVIEwByID(R.ID.imageVIEw);        convertVIEw.setTag(vh);      }      vh= (VIEwHolder) convertVIEw.getTag();      vh.iv.setimageResource(images[position]);      return convertVIEw;    }    class VIEwHolder{      ImageVIEw iv;    }  }}

第五步:Java中Activity的实现代码,跳转后的ImageSwicher的实现代码MainActivity.java:

import androID.content.Intent;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androID.Widget.ImageSwitcher;import androID.Widget.ImageVIEw;import androID.Widget.linearLayout;import androID.Widget.VIEwSwitcher;import java.util.ArrayList;/** * Created by panchengjia on 2016/12/05. */public class MainActivity extends AppCompatActivity implements VIEwSwitcher.VIEwFactory,VIEw.OntouchListener{  private ImageSwitcher is;//声明ImageSwitcher布局  private linearLayout point_layout;//声明导航圆点的布局  //图片ID数组(需要与GrIDActivity中的图片资源数组一一对应)  int[] images={R.mipmap.a1,R.mipmap.a4};  //实例化存储导航圆点的集合  ArrayList<ImageVIEw> points = new ArrayList<>();  int index;//声明index,记录图片ID数组下标  float startX;//手指接触屏幕时X的坐标(演示左右滑动)  float endX;//手指离开屏幕时的坐标(演示左右滑动)  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    //获取GrIDActivity中设置的intent    Intent intent = getIntent();    //获取GrIDActivity中得到的图片下标,并随意设置默认值    index = intent.getIntExtra("select",0);    is = (ImageSwitcher) findVIEwByID(R.ID.is);    is.setFactory(this);//通过工厂实现ImageSwitcher    initpoint();    is.setontouchListener(this);//设置触摸事件  }  //初始化导航圆点的方法  private voID initpoint() {    point_layout= (linearLayout) findVIEwByID(R.ID.point_layout);    int count = point_layout.getChildCount();//获取布局中圆点数量    for(int i =0;i<count;i++){      //将布局中的圆点加入到圆点集合中      points.add((ImageVIEw) point_layout.getChildAt(i));    }    //设置GrIDActivity中选中图片对应的圆点状态为触摸实心状态    points.get(index).setimageResource(R.mipmap.touched_holo);  }  //设选中图片对应的导航原点的状态  public voID setimagebackground(int selectimage) {    for(int i=0;i<points.size();i++){      //如果选中图片的下标等于圆点集合中下标的ID,则改变圆点状态      if(i==selectimage){        points.get(i).setimageResource(R.mipmap.touched_holo);      }else{        points.get(i).setimageResource(R.mipmap.default_holo);      }    }  }  //实现VIEwFactory的方法实例化imageVIEw(这里未设置ImageVIEw的属性)  @OverrIDe  public VIEw makeVIEw() {    //实例化一个用于切换的ImageVIEw视图    ImageVIEw iv = new ImageVIEw(this);    iv.setScaleType(ImageVIEw.ScaleType.FIT_XY);    //默认展示的第一个视图为images[index](主页面跳转过来的图片)    iv.setimageResource(images[index]);    return iv;  }  @OverrIDe  public boolean ontouch(VIEw v,MotionEvent event) {    //按下屏幕    if(event.getAction()==MotionEvent.ACTION_DOWN){      startX=event.getX();//获取按下屏幕时X轴的坐标      //手指抬起    }else if (event.getAction()==MotionEvent.ACTION_UP){      endX=event.getX();      //判断结束坐标大于起始坐标则为下一张(为避免误 *** 作,设置30的判断区间)      if(startX-endX>30){        //三目运算判断当前图片已经为最后一张,则从头开始        index = index+1<images.length?++index:0;        //使用系统自带的切换出入动画效果(也可以像VIEwFlipper中一样自定义动画效果)        is.setInAnimation(this,androID.R.anim.fade_in);        is.setoutAnimation(this,androID.R.anim.fade_out);        //判断结束坐标小于于起始坐标则为上一张(为避免误 *** 作,设置30的判断区间)      }else if(endX-startX>30){        //三目运算判断当前图片已经为第一张,则上一张为数组内最后一张图片        index = index-1>=0?--index:images.length-1;        is.setInAnimation(this,androID.R.anim.fade_out);      }      //设置ImageSwitcher的图片资源      is.setimageResource(images[index]);      //调用方法设置圆点对应状态      setimagebackground(index);    }    return true;  }}

本次代码展示到这里就结束了,按照前文所讲,大家可以尝试多种实现方法,本次使用到的intent,后面会有详细讲述,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android中使用GridView和ImageViewSwitcher实现电子相册简单功能实例全部内容,希望文章能够帮你解决Android中使用GridView和ImageViewSwitcher实现电子相册简单功能实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1147524.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存