当前位置:   article > 正文

Android制作微信添加多个图片,放大图片

Android制作微信添加多个图片,放大图片

1.添加依赖

implementation 'com.github.bumptech.glide:glide:4.12.0' //裁剪图片等等
implementation 'androidx.recyclerview:recyclerview:1.1.0' //recycleview依赖

2.使用recycleview

  1. <androidx.recyclerview.widget.RecyclerView
  2. android:id="@+id/recyclerView"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content" />

3.编写两个item页面为recycleview的子页面

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Button xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/btnAdd"
  4. android:layout_width="100dp"
  5. android:layout_height="120dp"
  6. android:layout_margin="4dp"
  7. android:layout_weight="1"
  8. android:text="Add"/>
  9. //点击添加按钮
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:orientation="horizontal"
  6. android:padding="8dp">
  7. <ImageView
  8. android:id="@+id/imgView"
  9. android:layout_width="100dp"
  10. android:layout_height="100dp"
  11. />
  12. </LinearLayout>

4.写recycleview的适配器

  1. package com.example.myapplication;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.Button;
  9. import android.widget.ImageView;
  10. import androidx.annotation.NonNull;
  11. import androidx.recyclerview.widget.RecyclerView;
  12. import com.bumptech.glide.Glide;
  13. import java.util.ArrayList;
  14. public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  15. private final int VIEW_TYPE_IMAGE = 0;
  16. private final int VIEW_TYPE_ADD_BUTTON = 1;
  17. private ArrayList<Uri> imageUris;
  18. private Context context;
  19. private LayoutInflater inflater;
  20. private OnAddButtonClickListener addButtonClickListener;
  21. public interface OnAddButtonClickListener {
  22. void onAddButtonClick();
  23. }
  24. public ImageAdapter(Context context, OnAddButtonClickListener listener) {
  25. this.context = context;
  26. this.imageUris = new ArrayList<>();
  27. this.inflater = LayoutInflater.from(context);
  28. this.addButtonClickListener = listener;
  29. }
  30. public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  31. if (viewType == VIEW_TYPE_IMAGE) {
  32. View itemView = inflater.inflate(R.layout.iteam, parent, false);
  33. return new ImageViewHolder(itemView);
  34. } else {
  35. View itemView = inflater.inflate(R.layout.iteambutton, parent, false);
  36. Button addButton = itemView.findViewById(R.id.btnAdd);
  37. addButton.setOnClickListener(v -> addButtonClickListener.onAddButtonClick());
  38. return new AddButtonViewHolder(itemView);
  39. }
  40. }
  41. @Override
  42. public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
  43. if (getItemViewType(position) == VIEW_TYPE_IMAGE) {
  44. Uri uri = imageUris.get(position);
  45. ImageViewHolder imageHolder = (ImageViewHolder) holder;
  46. Glide.with(context)
  47. .load(uri)
  48. .centerCrop()
  49. .into( imageHolder.imageView);
  50. //点击图片 放大图片,跳转页面
  51. imageHolder.imageView.setOnClickListener(new View.OnClickListener() {
  52. @Override
  53. public void onClick(View v) {
  54. Intent intent=new Intent(context,emptyActivity.class);
  55. intent.putExtra("url",uri.toString());
  56. context.startActivity(intent);
  57. }
  58. });
  59. }
  60. }
  61. @Override
  62. public int getItemCount() {
  63. // Plus one for the add button
  64. return imageUris.size() + 1
  65. }
  66. @Override
  67. public int getItemViewType(int position) {
  68. return position == imageUris.size() ? VIEW_TYPE_ADD_BUTTON : VIEW_TYPE_IMAGE;
  69. }
  70. public void addImage(Uri uri) {
  71. imageUris.add(uri);
  72. notifyItemInserted(imageUris.size() - 1);
  73. }
  74. static class ImageViewHolder extends RecyclerView.ViewHolder {
  75. ImageView imageView;
  76. ImageViewHolder(View itemView) {
  77. super(itemView);
  78. imageView = itemView.findViewById(R.id.imgView);
  79. }
  80. }
  81. static class AddButtonViewHolder extends RecyclerView.ViewHolder {
  82. AddButtonViewHolder(View itemView) {
  83. super(itemView);
  84. }
  85. }
  86. }

5.编写java文件

  1. package com.example.myapplication;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.provider.MediaStore;
  6. import androidx.annotation.Nullable;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. import androidx.recyclerview.widget.GridLayoutManager;
  9. import androidx.recyclerview.widget.LinearLayoutManager;
  10. import androidx.recyclerview.widget.RecyclerView;
  11. import java.util.ArrayList;
  12. public class RecyCleActivity extends AppCompatActivity implements ImageAdapter.OnAddButtonClickListener{
  13. private static final int REQUEST_CODE_PICK_IMAGE = 1;
  14. private ImageAdapter imageAdapter;
  15. private RecyclerView recyclerView;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_recy_cle);
  20. recyclerView = findViewById(R.id.recyclerView);
  21. recyclerView.setLayoutManager(new GridLayoutManager(this,3));
  22. imageAdapter = new ImageAdapter(this, this);
  23. recyclerView.setAdapter(imageAdapter);
  24. }
  25. @Override
  26. public void onAddButtonClick() {
  27. Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  28. startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
  29. }
  30. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  31. super.onActivityResult(requestCode, resultCode, data);
  32. if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == RESULT_OK) {
  33. if (data != null) {
  34. Uri selectedImageUri = data.getData();
  35. if (selectedImageUri != null) {
  36. imageAdapter.addImage(selectedImageUri);
  37. }
  38. }
  39. }
  40. }
  41. }

6.编写点击图片后放大图片,创建emptyActivity页面

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical">
  8. <ImageView
  9. android:id="@+id/img"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_centerHorizontal="true"
  13. android:layout_centerVertical="true"/>
  14. </RelativeLayout>
  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.widget.ImageView;
  6. import com.bumptech.glide.Glide;
  7. import javax.microedition.khronos.opengles.GL;
  8. public class emptyActivity extends AppCompatActivity {
  9. private ImageView img;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_empty);
  14. img=findViewById(R.id.img);
  15. String imageUriString = getIntent().getStringExtra("url");
  16. Glide.with(emptyActivity.this)
  17. .load(imageUriString)
  18. .into(img);
  19. }
  20. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/274175
推荐阅读
相关标签
  

闽ICP备14008679号