Gallery实现图片滑动

Gallery实现图片滑动

  1. 设计布局文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Gallery
android:id="@+id/my_gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
  1. MainActivity.java实现图片的滑动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.example.gaojikongjian;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
/***
* 实现图片的滑动,如相册
* @author hdcking.cn
*/
public class MainActivity extends Activity {
private Gallery gallery;
//m 数据层
int data[] = {R.drawable.b0,R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4,R.drawable.b5,R.drawable.b6};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//v
gallery = (Gallery)findViewById(R.id.my_gallery);
//c 控制层
gallery.setAdapter(new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ImageView im = new ImageView(MainActivity.this);
im.setImageResource(data[position]);
return im;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return data[position];
}
@Override
public int getCount() {
return data.length;
}
});
}
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!