|
Demo介绍:首页是一个GridView加载图片,竖屏时显示3列图片,横屏时显示4列图片;并且对图片进行大小限制和加灰色边框处理。
点击某一张图片,会链接到Gallery页面,由于Android自带的Gallery控件滑动效果很不好(滑动一次会加载好多张图片),这里对Gallery进行了扩展,滑动一次只加载一张图片。
1、首页Activity页面,GridViewActivity.java介绍:
public class GridViewActivity extends Activity {
002
private DisplayMetrics dm;
003
private GridImageAdapter ia;
004
private GridView g;
005
private int imageCol = 3;
006
007
@Override
008
protected void onCreate(Bundle savedInstanceState) {
009
// TODO Auto-generated method stub
010
super.onCreate(savedInstanceState);
011
// requestWindowFeature(Window.FEATURE_NO_TITLE);
012
ia = new GridImageAdapter(this);
013
setContentView(R.layout.mygridview);
014
g = (GridView) findViewById(R.id.myGrid);
015
g.setAdapter(ia);
016
g.setOnItemClickListener(new OnItemClick(this));
017
// 得到屏幕的大小
018
dm = new DisplayMetrics();
019
getWindowManager().getDefaultDisplay().getMetrics(dm);
020
021
}
022
023
/**
024
* 屏幕切换时进行处理 如果屏幕是竖屏,则显示3列,如果是横屏,则显示4列
025
*/
026
@Override
027
public void onConfigurationChanged(Configuration newConfig) {
028
try {
029
030
super.onConfigurationChanged(newConfig);
031
// 如果屏幕是竖屏,则显示3列,如果是横屏,则显示4列
032
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
033
imageCol = 4;
034
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
035
imageCol = 3;
036
}
037
g.setNumColumns(imageCol);
038
g.setAdapter(new ImageAdapter(this));
039
// ia.notifyDataSetChanged();
040
} catch (Exception ex) {
041
ex.printStackTrace();
042
}
043
}
044
045
/**
046
*
047
* @author 空山不空 点击具体的小图片时,会链接到GridViewActivity页面,进行加载和展示
048
*/
049
public class OnItemClick implements OnItemClickListener {
050
public OnItemClick(Context c) {
051
mContext = c;
052
}
053
054
@Override
055
public void onItemClick(AdapterView aview, View view, int position,
056
long arg3) {
057
Intent intent = new Intent();
058
intent.setClass(GridViewActivity.this, GalleryActivity.class);
059
intent.putExtra("position", position);
060
GridViewActivity.this.startActivity(intent);
061
}
062
063
private Context mContext;
064
}
065
066
/**
067
*
068
* @author 空山不空 设置GridView的图片适配器
069
*/
070
public class GridImageAdapter extends BaseAdapter {
071
072
Drawable btnDrawable;
073
074
public GridImageAdapter(Context c) {
075
mContext = c;
076
Resources resources = c.getResources();
077
btnDrawable = resources.getDrawable(R.drawable.bg);
078
}
079
080
public int getCount() {
081
return ImageSource.mThumbIds.length;
082
}
083
084
public Object getItem(int position) {
085
return position;
086
}
087
088
public long getItemId(int position) {
089
return position;
090
}
091
092
public View getView(int position, View convertView, ViewGroup parent) {
093
ImageViewExt imageView;
094
095
if (convertView == null) {
096
imageView = new ImageViewExt(mContext);
097
// 如果是横屏,GridView会展示4列图片,需要设置图片的大小
098
if (imageCol == 4) {
099
imageView.setLayoutParams(new GridView.LayoutParams(
100
dm.heightPixels / imageCol - 6, dm.heightPixels
101
/ imageCol - 6));
102
} else {// 如果是竖屏,GridView会展示3列图片,需要设置图片的大小
103
imageView.setLayoutParams(new GridView.LayoutParams(
104
dm.widthPixels / imageCol - 6, dm.widthPixels
105
/ imageCol - 6));
106
}
107
imageView.setAdjustViewBounds(true);
108
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
109
} else {
110
imageView = (ImageViewExt) convertView;
111
}
112
imageView.setImageResource(ImageSource.mThumbIds[position]);
113
114
return imageView;
115
}
116
117
private Context mContext;
118
}
119
}
复制代码
加载GridView页面,如果屏幕是竖屏,则显示3列,如果是横屏,则显示4列;并且显示的图片加上灰色边框,这里在适配器GridImageAdapter的getView方法里引用了ImageViewExt类来对图片进行处理,这个类扩展自ImageView;点击其中的某一张图片,会跳转到GalleryActivity页面。
2、ImageViewExt.java文件
/**
02
*
03
* @author 空山不空
04
* 扩展ImageView类,将图片加上边框,并且设置边框为灰色
05
*/
06
public class ImageViewExt extends ImageView {
07
//将图片加灰色的边框
08
private int color;
09
10
public ImageViewExt(Context context) {
11
super(context);
12
// TODO Auto-generated constructor stub
13
color=Color.GRAY;
14
}
15
16
public ImageViewExt(Context context, AttributeSet attrs) {
17
super(context, attrs);
18
// TODO Auto-generated constructor stub
19
color=Color.GRAY;
20
}
21
22
23
@Override
24
protected void onDraw(Canvas canvas) {
25
// TODO Auto-generated method stub
26
27
super.onDraw(canvas);
28
//画边框
29
Rect rec=canvas.getClipBounds();
30
rec.bottom--;
31
rec.right--;
32
Paint paint=new Paint();
33
paint.setColor(color);
34
paint.setStrokeWidth(5);
35
paint.setStyle(Paint.Style.STROKE);
36
canvas.drawRect(rec, paint);
37
}
38
}
复制代码
通过重载onDraw方法来画边框和设置边框颜色
3、相册GalleryActivity.java
/**
02
*
03
* @author 空山不空
04
* Gallery图片页面,通过Intent得到GridView传过来的图片位置,加载图片,再设置适配器
05
*/
06
public class GalleryActivity extends Activity {
07
public int i_position = 0;
08
private DisplayMetrics dm;
09
10
@Override
11
public void onCreate(Bundle savedInstanceState) {
12
super.onCreate(savedInstanceState);
13
requestWindowFeature(Window.FEATURE_NO_TITLE);
14
setContentView(R.layout.mygallery);
15
dm = new DisplayMetrics();
16
getWindowManager().getDefaultDisplay().getMetrics(dm);
17
// 获得Gallery对象
18
GalleryExt g = (GalleryExt) findViewById(R.id.ga);
19
//通过Intent得到GridView传过来的图片位置
20
Intent intent = getIntent();
21
i_position = intent.getIntExtra("position", 0);
22
// 添加ImageAdapter给Gallery对象
23
ImageAdapter ia=new ImageAdapter(this);
24
g.setAdapter(ia);
25
g.setSelection(i_position);
26
27
//加载动画
28
Animation an= AnimationUtils.loadAnimation(this,R.anim.scale );
29
g.setAnimation(an);
30
31
}
32
}
复制代码
这里有三点:
(1)、扩展Gallery组件,即GalleryExt控件,设置滑动一次只加载一张图片,并且, 如果是第一张图片时,向左滑动会提示“已到第一页”,如果是最后一张图片时,向右滑动会提示“已到第后页”
(2)、接收GridViewActivity页面传过来的参数position,通过这个参数找到具体的图片,通过ImageAdapter适配器加载
(3)、ImageAdapter图片适配器,用来加载图片
4、GalleryExt.java文件
/**
02
*
03
* @author 空山不空
04
* 扩展Gallery组件,设置滑动一次只加载一张图片,并且,
05
* 如果是第一张图片时,向左滑动会提示“已到第一页”
06
* 如果是最后一张图片时,向右滑动会提示“已到第后页”
07
*/
08
public class GalleryExt extends Gallery {
09
boolean is_first=false;
10
boolean is_last=false;
11
public GalleryExt(Context context) {
12
super(context);
13
// TODO Auto-generated constructor stub
14
}
15
16
public GalleryExt(Context context,AttributeSet paramAttributeSet)
17
{
18
super(context,paramAttributeSet);
19
20
}
21
22
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2)
23
{
24
return e2.getX() > e1.getX();
25
}
26
27
@Override
28
public boolean onFling(MotionEvent e1, MotionEvent e2, float distanceX,
29
float distanceY) {
30
//通过重构onFling方法,使Gallery控件滑动一次只加载一张图片
31
//获取适配器
32
ImageAdapter ia=(ImageAdapter)this.getAdapter();
33
//得到当前图片在图片资源中的位置
34
int p=ia.getOwnposition();
35
//图片的总数量
36
int count=ia.getCount();
37
int kEvent;
38
if(isScrollingLeft(e1, e2)){
39
//Check if scrolling left
40
if(p==0&&is_first){
41
//在第一页并且再往左移动的时候,提示
42
Toast.makeText(this.getContext(), "已到第一页", Toast.LENGTH_SHORT).show();
43
}else if(p==0){
44
//到达第一页时,把is_first设置为true
45
is_first=true;
46
}else{
47
is_last=false;
48
}
49
50
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
51
} else{
52
//Otherwise scrolling right
53
if(p==count-1&&is_last){
54
Toast.makeText(this.getContext(), "已到最后一页", Toast.LENGTH_SHORT).show();
55
}else if( p==count-1){
56
is_last=true;
57
}else{
58
is_first=false;
59
}
60
61
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
62
}
63
onKeyDown(kEvent, null);
64
return true;
65
}
复制代码5、ImageAdapter.java文件 /**
02
*
03
* @author 空山不空
04
* 图片适配器,用来加载图片
05
*/
06
public class ImageAdapter extends BaseAdapter {
07
//图片适配器
08
// 定义Context
09
private int ownposition;
10
11
12
public int getOwnposition() {
13
return ownposition;
14
}
15
16
public void setOwnposition(int ownposition) {
17
this.ownposition = ownposition;
18
}
19
20
private Context mContext;
21
22
// 定义整型数组 即图片源
23
24
// 声明 ImageAdapter
25
public ImageAdapter(Context c) {
26
mContext = c;
27
}
28
29
// 获取图片的个数
30
public int getCount() {
31
return ImageSource.mThumbIds.length;
32
}
33
34
// 获取图片在库中的位置
35
public Object getItem(int position) {
36
ownposition=position;
37
return position;
38
}
39
40
// 获取图片ID
41
public long getItemId(int position) {
42
ownposition=position;
43
return position;
44
}
45
46
public View getView(int position, View convertView, ViewGroup parent) {
47
48
49
ownposition=position;
50
ImageView imageview = new ImageView(mContext);
51
imageview.setBackgroundColor(0xFF000000);
52
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
53
imageview.setLayoutParams(new GalleryExt.LayoutParams(
54
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
55
56
imageview.setImageResource(ImageSource.mThumbIds[position]);
57
// imageview.setAdjustViewBounds(true);
58
// imageview.setLayoutParams(new GridView.LayoutParams(320, 480));
59
// imageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
60
return imageview;
61
}
62
}
复制代码
6、配置文件
(1)AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ajie"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".GalleryActivity" android:label="@string/title" />
<activity android:name=".GridViewActivity" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
复制代码(2)mygridview.xml,即GridView页面 <?xml version="1.0" encoding="utf-8"?>
02
03
04
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
05
android:id="@+id/myGrid"
06
android:layout_width="fill_parent"
07
android:layout_height="fill_parent"
08
android:verticalSpacing="6dp"
09
android:numColumns="3"
10
android:paddingTop="5dp"
11
android:stretchMode="columnWidth"
12
android:gravity="fill_vertical|fill_horizontal"
13
/>
复制代码(3)mygallery.xml,加载图片页面 <?xml version="1.0" encoding="utf-8"?>
02
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
03
android:id="@+id/myGrid"
04
android:layout_width="fill_parent"
05
android:layout_height="fill_parent"
06
android:verticalSpacing="6dp"
07
android:numColumns="3"
08
android:paddingTop="5dp"
09
android:stretchMode="columnWidth"
10
android:gravity="fill_vertical|fill_horizontal"
11
/>
复制代码 |
|