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

首先我们先罗列一下本次实现所要用的知识点:

(1)首页界面,我们需要可以通过GridView来实现,有关GridView的实现代码以及讲解请参照本人相关博文:

Android中GridView通过自定义适配器(未优化)实现图文视图排列

(2)具体照片的展示以及左右切换效果,可以实现这个效果的方法很多(ViewPager,ViewFlipper,ImageViewSwitcher等),可以参照相关博文:

Android中使用ViewPager实现屏幕页面切换和页面切换效果

Android中使用ViewFlipper实现屏幕页面切换(关于坐标轴的问题已补充更改)

Android中使用ImageViewSwitcher实现图片切换轮播导航效果

(3)实现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:

这里涉及到的知识点请参照

Android中GridView通过自定义适配器(未优化)实现图文视图排列

本次自定义适配器中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:

可参照博文

Android中使用ImageViewSwitcher实现图片切换轮播导航效果

 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.a2,R.mipmap.a3,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_in);
is.setOutAnimation(this,android.R.anim.fade_out);
}
//设置ImageSwitcher的图片资源
is.setImageResource(images[index]);
//调用方法设置圆点对应状态
setImageBackground(index);
}
return true;
}
}

本次代码展示到这里就结束了,按照前文所讲,大家可以尝试多种实现方法,本次使用到的intent,后面会有详细讲述,敬请关注

Android中使用GridView和ImageViewSwitcher实现电子相册简单功能的更多相关文章

  1. Android笔记(二十二) Android中的GridView

    GridView和ListView一样,是Android中比较常见的布局控件,譬如我们的手机桌面其实就是一个GridView. 效果: 实现过程和ListView类似,也是通过Adapter将数据源展 ...

  2. Android中Universal Image Loader开源框架的简单使用

    UIL (Universal Image Loader)aims to provide a powerful, flexible and highly customizable instrument ...

  3. android之使用GridView+仿微信图片上传功能

    由于工作要求最近在使用GridView完成图片的批量上传功能,我的例子当中包含仿微信图片上传.拍照.本地选择.相片裁剪等功能,如果有需要的朋友可以看一下,希望我的实际经验能对您有所帮助. 直接上图,下 ...

  4. Android中取消GridView & ListView默认的点击背景色

    方法一: gridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); listView.setSelector(new ColorDrawa ...

  5. 详细解读Android中的搜索框(一)—— 简单小例子

    这次开的是一个讲解SearchView的栏目,第一篇主要是给一个小例子,让大家对这个搜索视图有一个了解,之后再分布细化来说. 目标: 我们先来定个目标,我们通过搜索框来输入要搜索的联系人名字,输入的时 ...

  6. ASP.NET中的GridView自带的编辑更新功能

    string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].Connec ...

  7. Android中通过ImageSwitcher实现相册滑动查看照片功能(附代码下载)

    场景 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将需要滚动查看的照 ...

  8. Android 中图片压缩分析(上)

    作者: shawnzhao,QQ音乐技术团队一员 一.前言 在 Android 中进行图片压缩是非常常见的开发场景,主要的压缩方法有两种:其一是质量压缩,其二是下采样压缩. 前者是在不改变图片尺寸的情 ...

  9. 【Android】Android 中string-array的用法

    在Android中,用string-array是一种简单的提取XML资源文件数据的方法. 例子如下: 把相应的数据放到values文件夹的arrays.xml文件里 <?xml version= ...

随机推荐

  1. 在ASP.NET MVC 4 on Mono中使用OracleClient in CentOS 6.x的问题记录

    在ASP.NET MVC 4 on Mono中使用OracleClient in CentOS 6.x的问题记录 前言 最近有个Web项目,业务功能不复杂,但是这个客户(某政府部门)有两个硬性要求:1 ...

  2. 最新Linux部署.NET,Mono and DNX

    这几天一直在折腾在Linux下的ASP.NET 5,就下在看来在其它操作系统中ASP.NET 5或.NET应用,要想在完整的MS VM(CoreCLR)上运行还不远远达不到,应用的效果. 目前只能在M ...

  3. AutoMapper(六)

    返回总目录 List和数组 AutoMapper只要求元素类型的配置而不要求可能会用到的任何数组或者list类型.比如,我们有一个简单的源和目标类型: public class Source{ pub ...

  4. TODO:一不顺眼就换字体Go之应用篇

    TODO:一不顺眼就换字体Go之应用篇 字体,文字的外在形式特征.就是文字的风格,是文字的外衣.好的字体让人看得舒服,让人更有看.写的欲望哦.2016-11-16 GO官方博客发布了同名Go字体,并没 ...

  5. C#服务器获取客户端IP地址以及归属地探秘

    背景:博主本是一位Windows桌面应用程序开发工程师,对网络通信一知半解.一日老婆逛完某宝,问:"为什么他们知道我的地址呢,他们是怎么获取我的地址的呢?" 顺着这个问题我们的探秘 ...

  6. Atitit. 破解  拦截 绕过 网站 手机 短信 验证码  方式 v2 attilax 总结

    Atitit. 破解  拦截 绕过 网站 手机 短信 验证码  方式 v2 attilax 总结 1. 验证码的前世今生11.1. 第一代验证码 图片验证码11.2. 第二代验证码  用户操作 ,比如 ...

  7. 【原】SDWebImage源码阅读(三)

    [原]SDWebImage源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1.SDWebImageDownloader中的downloadImageWithURL 我们 ...

  8. Beginners Guide To Web Development

    Web Development Front End Development Back End Development

  9. Android开发之Activity的生命周期以及加载模式

    本篇博客就来好好的搞一下Activity的生命周期,如果搞过iOS的小伙伴的话,Activity的生命周期和iOS中ViewController的生命周期非常类似.生命周期,并不难理解.一个人的生命周 ...

  10. ashx中Response.ContentType的常用类型

    ashx中Response.ContentType的常用类型: text/plaintext/htmltext/xmlapplication/jsonimage/GIFapplication/x-cd ...