android 实现画廊

发布时间:2016-12-8 14:13:01 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"android 实现画廊",主要涉及到android 实现画廊方面的内容,对于android 实现画廊感兴趣的同学可以参考一下。

源代码下载 点击打开链接 如图   点击画廊中的图片之后,画廊会滑动,如下图   具体代码实现 main.xml <?xml version="1.0" encoding="utf-8"?> <Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Gallery01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> ImageAdapter.java package com.example.gallery; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { private Context mContext; private Integer[] mImageIds = { R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8 }; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mImageIds.length; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageview = new ImageView(mContext); imageview.setImageResource(mImageIds[position]); imageview.setScaleType(ImageView.ScaleType.FIT_CENTER); imageview.setLayoutParams(new Gallery.LayoutParams(120, 120)); return imageview; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } } MainActivity.java package com.example.gallery; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery; import android.widget.Toast; public class MainActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery g=(Gallery)findViewById(R.id.Gallery01); g.setAdapter(new ImageAdapter(this)); g.setBackgroundResource(R.drawable.bg0); g.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"you choose "+(position+1)+"th picture",Toast.LENGTH_SHORT).show(); } }); } }

上一篇:华为历年机试题2014
下一篇:[Python入门及进阶笔记]Python-基础-字符串小结

相关文章

相关评论