按下按钮时如何更改图像scaleType

How to change Image scaleType when button is pressed

你好我正在尝试在按下按钮时更改 imageView scaleType 目前我正在使用 centercrop(默认)我希望当按下按钮时它会更改为适合中心,反之亦然

for eg : 1 Instagram :- when you upload a photo to your post there is a button to adjust the image crop

2 youtube :- in youtube if you have noticed in the home there are some comunity tabs where images are uploaded when you click the image it changes the crop and vise versa

还有一个与此相关的小问题,即如何在按下时更改按钮(按钮是 ImageView)背景,反之亦然

请注意,我想在适配器中使用它class

imagepost_buttons.xml //切换scaleType的按钮

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/relativeLayoutImagePost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"
        android:layout_marginStart="15dp"
        android:contentDescription="@string/todo"
        android:gravity="center"
        android:elevation="10dp"
        android:src="@drawable/ic_baseline_open_in_full_24" />
</RelativeLayout>

circular_scaling_background.xml // 按下按钮(ImageView)时我想改变的背景,反之亦然

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid android:color="@color/lite_grey"/>
    <stroke android:width="0dp" android:color="@color/lite_grey"/>
    <size android:width="24dp" android:height="24dp"/>

</shape>

post_item_container_home.xml // 我要更改 scaleType 的 ImageView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:background="@color/black"
    android:orientation="vertical"
    android:weightSum="5">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/Card_View"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_weight="4"
        app:cardBackgroundColor="@color/black"
        app:shapeAppearanceOverlay="@style/RoundedCornerHome">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.google.android.material.imageview.ShapeableImageView
                android:id="@+id/imagePostHome"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:contentDescription="@string/todo"
                android:scaleType="centerCrop"
                app:shapeAppearanceOverlay="@style/RoundedCornerHome" />

            <include
                android:id="@+id/imagepost_buttons"
                layout="@layout/imagepost_buttons" />
        </FrameLayout>

    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="10dp"
        android:layout_weight="1"
        app:cardBackgroundColor="@color/grey"
        app:shapeAppearanceOverlay="@style/RoundedCornerHome">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                android:id="@+id/material_cardview_snipet"
                layout="@layout/material_cardview_snipet" />
        </FrameLayout>


    </com.google.android.material.card.MaterialCardView>


</LinearLayout>

PostAdapter_Home.java // 适配器 class

public class PostAdapter_Home extends RecyclerView.Adapter<PostAdapter_Home.PostViewHolder> {
    public static List<Upload> mUploads;
    public Context mcontext;

    public PostAdapter_Home(Context context, List<Upload> uploads) {
        mUploads = uploads;
        mcontext = context;
    }


    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(mcontext).inflate(R.layout.post_item_container_home, parent, false);
        return new PostViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        Home_Fragment.saved_position = holder.getAbsoluteAdapterPosition();
        Home_Fragment.saved_position = 0;
        Upload uploadCurrent = mUploads.get(position);
        Shimmer shimmer = new Shimmer.ColorHighlightBuilder()
                .setBaseColor(Color.parseColor("#F3F3F3"))
                .setBaseAlpha(1)
                .setHighlightColor(Color.parseColor("#E7E7E7"))
                .setHighlightAlpha(1)
                .setDropoff(50)
                .build();
        ShimmerDrawable shimmerDrawable = new ShimmerDrawable();
        shimmerDrawable.setShimmer(shimmer);
        Glide.with(mcontext)
                .load(uploadCurrent.getmImageUrl())
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .placeholder(shimmerDrawable)
                .into(holder.imageView);


    }

    @Override
    public int getItemCount() {
        return mUploads.size();
    }

    public static class PostViewHolder extends RecyclerView.ViewHolder {
        ShapeableImageView imageView;

        public PostViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imagePostHome);
        }


        public int getAbsoluteAdapterPosition() {
            return 0;
        }
    }
}

更新 1

PostAdapter_Home.java // 根据答案更新 // 它不起作用我知道我把它搞砸了,我不知道如何准确地实现它

public class PostAdapter_Home extends RecyclerView.Adapter<PostAdapter_Home.PostViewHolder> {
    public static List<Upload> mUploads;
    public Context mcontext;
    View view;

    public PostAdapter_Home(Context context, List<Upload> uploads) {
        mUploads = uploads;
        mcontext = context;
    }


    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(mcontext).inflate(R.layout.post_item_container_home, parent, false);
        return new PostViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        Home_Fragment.saved_position = holder.getAbsoluteAdapterPosition();
        changeScaleType(position);


    }

    private void changeScaleType(int position) {
        ShapeableImageView imageView;
        imageView = view.findViewById(R.id.imagePostHome);
        Home_Fragment.saved_position = 0;
        Upload uploadCurrent = mUploads.get(position);
        Shimmer shimmer = new Shimmer.ColorHighlightBuilder()
                .setBaseColor(Color.parseColor("#F3F3F3"))
                .setBaseAlpha(1)
                .setHighlightColor(Color.parseColor("#E7E7E7"))
                .setHighlightAlpha(1)
                .setDropoff(50)
                .build();
        ShimmerDrawable shimmerDrawable = new ShimmerDrawable();
        shimmerDrawable.setShimmer(shimmer);
        String imgTag = imageView.getTag().toString();
        Log.e(TAG, "imgTag---" + imgTag);
        if (imgTag.equals("")) {
            Glide.with(mcontext).load(uploadCurrent.getmImageUrl()).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(shimmerDrawable).fitCenter().into(imageView);
            imageView.setTag("centerInside");
        } else {
            if (imgTag.equals("centerCrop")) {
                Glide.with(mcontext).load(uploadCurrent.getmImageUrl()).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                        .placeholder(shimmerDrawable).fitCenter().into(imageView);
                imageView.setTag("centerInside");
            } else {
                Glide.with(mcontext).load(uploadCurrent.getmImageUrl()).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                        .placeholder(shimmerDrawable).centerCrop().into(imageView);
                imageView.setTag("centerCrop");
            }
        }

    }

    @Override
    public int getItemCount() {
        return mUploads.size();
    }

    public static class PostViewHolder extends RecyclerView.ViewHolder {
        ShapeableImageView imageView;

        public PostViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imagePostHome);
        }


        public int getAbsoluteAdapterPosition() {
            return 0;
        }
    }
}

试试这个:

private void changeScaleType(){
                String imgTag = imagePostHome.getTag().toString();
                Log.e(TAG,"imgTag---"+imgTag);
                if(imgTag.equals("")){
                    Glide.with(this).load(imgUrl).fitCenter().into(imagePostHome);
                    imagePostHome.setTag("centerInside");
                }else {
                    if (imgTag.equals("centerCrop")) {
                        Glide.with(this).load(imgUrl).fitCenter().into(imagePostHome);
                        imagePostHome.setTag("centerInside");
                    }else{
                        Glide.with(this).load(imgUrl).centerCrop().into(imagePostHome);
                        imagePostHome.setTag("centerCrop");
                    }
                }
            }
  • 将标签设置为 imageView
  • 点击视图时添加 fitCentercenterCrop 代码,同时使用 Glide 将图像加载到图像视图

Edit:

在点击视图时调用 changeScaleType() 方法,负责更改 imagevew 的比例类型,例如,在您的情况下 relativeLayoutImagePost 负责更改 imagevew 的比例类型。

FindViewById of Views in PostViewHolder class,Add values to views in onBindViewHolder Adapter

方法

这是适配器的编码:

public class PostAdapter_Home extends RecyclerView.Adapter<PostAdapter_Home.PostViewHolder> {
    private static String TAG = PostAdapter_Home.class.getSimpleName();
    public static List<Upload> mUploads;
    public Context mcontext;
    View view;

    public PostAdapter_Home(Context context, List<Upload> uploads) {
        mUploads = uploads;
        mcontext = context;
    }
    
    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(mcontext).inflate(R.layout.post_item_container_home, parent, false);
        return new PostViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        Shimmer shimmer = new Shimmer.ColorHighlightBuilder()
                .setBaseColor(Color.parseColor("#F3F3F3"))
                .setBaseAlpha(1)
                .setHighlightColor(Color.parseColor("#E7E7E7"))
                .setHighlightAlpha(1)
                .setDropoff(50)
                .build();
        ShimmerDrawable shimmerDrawable = new ShimmerDrawable();
        shimmerDrawable.setShimmer(shimmer);
        Upload uploadCurrent = mUploads.get(position);
        Glide.with(mcontext).load(uploadCurrent.getmImageUrl()).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .placeholder(shimmerDrawable).fitCenter().into(holder.imagePostHome);

        holder.relativeLayoutImagePost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                changeScaleType(holder, position);
            }
        });

    }

    private void changeScaleType(PostViewHolder holder,int position) {
        Shimmer shimmer = new Shimmer.ColorHighlightBuilder()
                .setBaseColor(Color.parseColor("#F3F3F3"))
                .setBaseAlpha(1)
                .setHighlightColor(Color.parseColor("#E7E7E7"))
                .setHighlightAlpha(1)
                .setDropoff(50)
                .build();
        ShimmerDrawable shimmerDrawable = new ShimmerDrawable();
        shimmerDrawable.setShimmer(shimmer);
        
        Upload uploadCurrent = mUploads.get(position);
        String imgTag = holder.imagePostHome.getTag().toString();
        Log.e(TAG, "imgTag---" + imgTag);
        if (imgTag.equals("")) {
            Glide.with(mcontext).load(uploadCurrent.getmImageUrl()).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(shimmerDrawable) .fitCenter().into( holder.imagePostHome);
            holder.imagePostHome.setTag("centerInside");
        } else {
            if (imgTag.equals("centerCrop")) {
                Glide.with(mcontext).load(uploadCurrent.getmImageUrl()).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                        .placeholder(shimmerDrawable) .fitCenter().into( holder.imagePostHome);
                holder.imagePostHome.setTag("centerInside");
            } else {
                Glide.with(mcontext).load(uploadCurrent.getmImageUrl()).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                        .placeholder(shimmerDrawable).centerCrop().into( holder.imagePostHome);
                holder.imagePostHome.setTag("centerCrop");
            }
        }

    }

    @Override
    public int getItemCount() {
        return mUploads.size();
    }

    public static class PostViewHolder extends RecyclerView.ViewHolder {

        private MaterialCardView CardView;
        private ShapeableImageView imagePostHome;
        private RelativeLayout relativeLayoutImagePost;

        public PostViewHolder(@NonNull View itemView) {
            super(itemView);

            CardView = itemView.findViewById(R.id.Card_View);
            imagePostHome = itemView.findViewById(R.id.imagePostHome);
            relativeLayoutImagePost = itemView.findViewById(R.id.relativeLayoutImagePost);

        }


    }
}