效果图
- 点击图片切换
- 缩略图同样大小显示
- 长按缩略图删除
- 去除标题栏
- 大图原比例显示
MainActivity.java
package com.liuyanzhe.zzu.task16_picture_521;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
/*暂存要显示的图片id*/
int[] imagesIds={
R.drawable.cat,R.drawable.cat1,
R.drawable.startupwp,R.drawable.dog1,
R.drawable.caption,R.drawable.color,
};
ImageSwitcher switcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
/*隐藏标题栏*/
getSupportActionBar().hide();
/*定义一个list,用来接收imagesIds中的数据*/
final List<Map<String,Object>> listItem=new ArrayList<Map<String,Object>>();
/*将数组中的数据转化到list中*/
for (int i=0;i<imagesIds.length;i++){
Map<String,Object> map=new HashMap<String,Object>();
map.put("image",imagesIds[i]);
listItem.add(map);
}
/*获取switcher*/
switcher =(ImageSwitcher)findViewById(R.id.switcher1);
/*设置图片切换效果,必须要,否则图片不知道如何切换出来*/
switcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView=new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
));
return imageView;
}
});
/*创建GridView适配器*/
SimpleAdapter simpleAdapter=new SimpleAdapter(this,listItem,R.layout.cell,new String[]{"image"},new int[]{R.id.image1});
/*获取GridView*/
GridView grid=(GridView)findViewById(R.id.grid1);
/*为GridView设置适配器*/
grid.setAdapter(simpleAdapter);
/*添加被选中图片的监听器*/
grid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
/*在switch中显示被选中的图片*/
switcher.setImageResource(imagesIds[i]);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
/*单击监听*/
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
/*在switch中显示被单击的图片*/
switcher.setImageResource(imagesIds[i]);
}
});
/*长按监听*/
grid.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this,"删除图片",Toast.LENGTH_SHORT).show();
listItem.remove(i);
/*创建GridView适配器*/
SimpleAdapter simpleAdapter=new SimpleAdapter(MainActivity.this,listItem,R.layout.cell,new String[]{"image"},new int[]{R.id.image1});
/*获取GridView*/
GridView grid=(GridView)findViewById(R.id.grid1);
/*为GridView设置适配器*/
grid.setAdapter(simpleAdapter);
return true;//返回false,还会调用onItemClick(单击事件)
}
});
}
}
layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<GridView
android:id="@+id/grid1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:columnWidth="300dp"
android:numColumns="3"
/>
<ImageSwitcher
android:id="@+id/switcher1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"
android:inAnimation="@android:anim/fade_in"
android:outAnimation="@android:anim/fade_out"
/>
</LinearLayout>
cell.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
>
<ImageView
android:id="@+id/image1"
android:layout_width="80dp"
android:layout_height="80dp"
android:adjustViewBounds="false"
android:padding="10px"
android:paddingLeft="10px"
android:paddingRight="10px"
android:scaleType="fitXY"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>