Android实现漂亮的Gallery画廊
2020-04-28 12:04
1386 查看
本文实例为大家分享了Android实现Gallery画廊的具体代码,供大家参考,具体内容如下
仅是实现基本功能,页面粗糙请见谅
图片下标0开始
activity_main.xml页面:
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.example.gallery.MainActivity"> <Gallery android:id="@+id/galPicture" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
GalleryAdapter.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 GalleryAdapter extends BaseAdapter { private Context mContext; int[] images = {R.mipmap.apple, R.mipmap.banana, R.mipmap.bicycle, R.mipmap.chair,R.mipmap.chopsticks, R.mipmap.dog, R.mipmap.fish, R.mipmap.pear}; //本地图片 public GalleryAdapter (Context context) { this.mContext = context; } @Override public int getCount() { return images.length; } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ImageView image = new ImageView(mContext); image.setImageResource(images[i]); //设置图片 image.setAdjustViewBounds(true); //是否调整边框 image.setLayoutParams(new Gallery.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); return image; } }
MainActivity.java页面:
package com.example.gallery; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.Toast; public class MainActivity extends AppCompatActivity { GalleryAdapter galleryAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Gallery galPicture = findViewById(R.id.galPicture); galleryAdapter = new GalleryAdapter(MainActivity.this); galPicture.setAdapter(galleryAdapter); //相应的点击事件 galPicture.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this, "图片" + i, Toast.LENGTH_LONG).show(); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- Android App开发中使用RecyclerView实现Gallery画廊的实例
- Android画廊效果-Gallery和ViewPager不同实现
- Android中实现Gallery画廊(相册)
- Android开发学习之基于ViewPager实现Gallery画廊效果
- Android-画廊实现 (Gallery , ImageSwitch)
- Android 使用ViewPager实现类似gallery画廊的效果(画廊效果之ViewPager显示多个图片)
- Android中实现从SD卡的Gallery画廊带回图片
- Android 用Gallery和ImageSwicher实现画廊效果,展示图片+完整源代码
- android 使用recyclerview实现Gallery画廊效果(二)
- Android开发实现Gallery画廊效果的方法
- Android开发学习之基于ViewPager实现Gallery画廊效果
- 【转】Android 使用ViewPager实现类似gallery画廊的效果(画廊效果之ViewPager显示多个图片)
- android用户界面-组件Widget-画廊视图Gallery
- Android实现页面自动循环切换【Gallery】
- android之Gallery默认实现
- Android 实现Gallery左对齐,选中Item不滑动效果
- 我的Android进阶之旅------>Android之Gallery和GridView两种方式与ImageSwitcher实现带预览的和幻灯片方式的两种图片浏览器
- 使用Android自带Gallery组件实现CoverFlow,源码+解析
- Android 实现显示文字的Gallery
- android UI进阶之用gallery实现可滑动的Tab