温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android实现漂亮的Gallery画廊

发布时间:2020-09-13 22:30:16 来源:脚本之家 阅读:134 作者:Vivinia_Vivinia 栏目:移动开发

本文实例为大家分享了Android实现Gallery画廊的具体代码,供大家参考,具体内容如下

仅是实现基本功能,页面粗糙请见谅

图片下标0开始

Android实现漂亮的Gallery画廊

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();
      }
    });
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI