Android 实现仿微信朋友圈九宫格图片+NineGridView+ImageWatcher(图片查看:1.预览,2.拖动,3.放大,4.左右...
Android 实现仿微信朋友圈九宫格图片+NineGridView+ImageWatcher
在本文中,我们将一步步地实现一个仿微信朋友圈的九宫格图片展示效果,使用 NineGridView 和 ImageWatcher 库。
一、测试环境和依赖包首先,我们需要创建一个新的 Android项目,并添加必要的依赖包:
```groovy// build.gradleplugins {
id 'com.android.application'
}
android {
compileSdkVersion29 defaultConfig {
applicationId "com.example.ninegridview"
minSdkVersion15 targetSdkVersion29 versionCode1 versionName "1.0"
}
buildTypes {
release {
minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.github.iielse:ImageWatcher:1.1.5'
}
```
二、添加 NineGridView接下来,我们需要在项目中添加 NineGridView 库。NineGridView 是一个用于展示九宫格图片的控件。
```xml
android:layout_height="match_parent"> android:layout_width="match_parent" android:layout_height="wrap_content" />
```
三、添加 ImageWatcher接下来,我们需要在项目中添加 ImageWatcher 库。ImageWatcher 是一个用于查看图片的控件。
```xml
android:layout_height="match_parent"> android:layout_width="match_parent" android:layout_height="wrap_content" />
```
四、实现 NineGridView现在,我们需要实现 NineGridView 的点击事件和图片展示效果。
```java// MainActivity.javapublic class MainActivity extends AppCompatActivity {
private NineGridView nineGridView;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nineGridView = findViewById(R.id.nine_grid_view);
// 添加九宫格图片 List
for (int i =0; i < 9; i++) {
images.add(" + i + ".jpg");
}
nineGridView.setAdapter(new NineGridAdapter(images));
// 设置点击事件 nineGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView> parent, View view, int position, long id) {
String imageUrl = (String) parent.getAdapter().getItem(position);
Intent intent = new Intent(MainActivity.this, ImageWatcherActivity.class);
intent.putExtra("image_url", imageUrl);
startActivity(intent);
}
});
}
public class NineGridAdapter extends BaseAdapter {
private List
public NineGridAdapter(List
this.images = images;
}
@Override public int getCount() {
return images.size();
}
@Override public Object getItem(int position) {
return images.get(position);
}
@Override public long getItemId(int position) {
return position;
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(MainActivity.this).load(images.get(position)).into(imageView);
return imageView;
}
}
}
```
五、实现 ImageWatcher最后,我们需要实现 ImageWatcher 的点击事件和图片展示效果。
```java// ImageWatcherActivity.javapublic class ImageWatcherActivity extends AppCompatActivity {
private ImageWatcher imageWatcher;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_watcher);
imageWatcher = findViewById(R.id.image_watcher);
// 设置点击事件 imageWatcher.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView> parent, View view, int position, long id) {
String imageUrl = (String) parent.getAdapter().getItem(position);
Intent intent = new Intent(ImageWatcherActivity.this, ImageDetailActivity.class);
intent.putExtra("image_url", imageUrl);
startActivity(intent);
}
});
}
public class ImageWatcherAdapter extends BaseAdapter {
private List
public ImageWatcherAdapter(List
this.images = images;
}
@Override public int getCount() {
return images.size();
}
@Override public Object getItem(int position) {
return images.get(position);
}
@Override public long getItemId(int position) {
return position;
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(ImageWatcherActivity.this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(ImageWatcherActivity.this).load(images.get(position)).into(imageView);
return imageView;
}
}
public class ImageDetailAdapter extends BaseAdapter {
private List
public ImageDetailAdapter(List
this.images = images;
}
@Override public int getCount() {
return images.size();
}
@Override public Object getItem(int position) {
return images.get(position);
}
@Override public long getItemId(int position) {
return position;
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(ImageDetailActivity.this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(ImageDetailActivity.this).load(images.get(position)).into(imageView);
return imageView;
}
}
public class ImageDetailActivity extends AppCompatActivity {
private ImageWatcher imageWatcher;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_detail);
imageWatcher = findViewById(R.id.image_watcher);
// 设置点击事件 imageWatcher.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView> parent, View view, int position, long id) {
String imageUrl = (String) parent.getAdapter().getItem(position);
Intent intent = new Intent(ImageDetailActivity.this, ImageDetailActivity.class);
intent.putExtra("image_url", imageUrl);
startActivity(intent);
}
});
}
public class ImageDetailAdapter extends BaseAdapter {
private List
public ImageDetailAdapter(List
this.images = images;
}
@Override public int getCount() {
return images.size();
}
@Override public Object getItem(int position) {
return images.get(position);
}
@Override public long getItemId(int position) {
return position;
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(ImageDetailActivity.this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(ImageDetailActivity.this).load(images.get(position)).into(imageView);
return imageView;
}
}
}
}
```
以上就是实现仿微信朋友圈九宫格图片+NineGridView+ImageWatcher的详细步骤。
朋友圈androidstudioRecyclerViewNineGridViewImageWatcher仿微信朋友圈九宫格图片