现在的位置: 首页 > 综合 > 正文

android gallery的使用

2018年09月15日 ⁄ 综合 ⁄ 共 1815字 ⁄ 字号 评论关闭

1:

xml 

<LinearLayout 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" >

    <Gallery
        android:id="@+id/imagegallery"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />

</LinearLayout>

2:

java文件

package com.example.cloud.hdplayer2.faq;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageShowActivity extends Activity {
private Gallery imagegallery;
private ImageView image;
private String pos;
private ImageAdapter imageadapter;
private int[] images = new int[] { R.drawable.m1, R.drawable.m2,
R.drawable.m3, R.drawable.m1, R.drawable.m1, R.drawable.m1,
R.drawable.m1 };

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imageshow);


imagegallery = (Gallery) findViewById(R.id.imagegallery);
imageadapter = new ImageAdapter(getApplicationContext());
initview();
}

private void initview() {
imagegallery.setAdapter(imageadapter);
imagegallery.setSelection(0);

}

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context context) {
mContext = context;
}

public int getCount() {
return images.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(mContext);
image.setImageResource(images[position]);
image.setAdjustViewBounds(true);
image.setLayoutParams(new Gallery.LayoutParams(  
           LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return image;
}
}

}

抱歉!评论已关闭.