Android Studio 和 Firebase:全屏显示 ImageView

Android Studio & Firebase: Show ImageView on Full Screen

我有一个屏幕显示我的电影名称及其图片,它们在我的 Firebase 中。它显示 them with the pictures.

我的数据库结构类似于 thisname 是我的电影名称的字符串。 profilePic 是包含来自 Firebase 存储的图片 link 的字符串。

我想要的是,当我点击列出的任何图片时,我想将它显示在另一张更大尺寸​​的 activity 上(比如打开某人的 WhatsApp 或 Facebook 个人资料图片)

这些是我的 类,效果很好。我该怎么做?

Movies.java

package com.example.XXXX;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class Movies extends AppCompatActivity {

    DatabaseReference reference;
    RecyclerView recyclerView;
    ArrayList<Profile> list;
    MyAdapter adapter;

    private String message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movies);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview_films);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        reference = FirebaseDatabase.getInstance().getReference().child("Films");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                list  = new ArrayList<Profile>();
                for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
                    Profile p = dataSnapshot1.getValue(Profile.class);
                    list.add(p);
                }
                adapter = new MyAdapter(Movies.this,list);
                recyclerView.setAdapter(adapter);
                findViewById(R.id.loadingPanel).setVisibility(View.GONE);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(Movies.this,"Ooops... Something is wrong.",Toast.LENGTH_SHORT).show();
            }
        });

        Intent intent = getIntent();
        message = intent.getStringExtra("EXTRA_MESSAGE");

        ImageButton backButton = (ImageButton) findViewById(R.id.imageButton13);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Movies.this, Menu.class);
                i.putExtra("EXTRA_MESSAGE",message);
                startActivity(i);
                overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
            }
        });
    }

    @Override
    public void onBackPressed() {
        Intent i = new Intent(Movies.this, Menu.class);
        i.putExtra("EXTRA_MESSAGE",message);
        startActivity(i);
        overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
    }
}

MyAdapter.java

package com.example.XXXX;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    Context context;
    ArrayList<Profile> profiles;

    public MyAdapter(Context c, ArrayList<Profile> p){
        context = c;
        profiles = p;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.cardview,viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        myViewHolder.name.setText(profiles.get(i).getName());
        Picasso.get().load(profiles.get(i).getProfilePic()).into(myViewHolder.profilePic);
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView name;
        ImageView profilePic;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.film_name);
            profilePic = (ImageView) itemView.findViewById(R.id.filmProfile);
        }
    }
}

Profile.java

package com.example.XXXX;

public class Profile {
    private String name;
    private String profilePic;

    public Profile() {
    }

    public Profile(String name, String profilePic) {
        this.name = name;
        this.profilePic = profilePic;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getProfilePic() {
        return profilePic;
    }

    public void setProfilePic(String profilePic) {
        this.profilePic = profilePic;
    }

}

activity_movies.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Movies"
    android:background="@drawable/background">

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

    <RelativeLayout
        android:id="@+id/loadingPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="true" />
    </RelativeLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="88dp"
        android:layout_marginBottom="10dp">

        <LinearLayout
            android:id="@+id/layoutoffilms"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginBottom="10dp"
            >

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview_films"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

    </ScrollView>

</LinearLayout>

cardview.xml

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
    android:orientation="horizontal">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@drawable/border"
            android:layout_marginTop="10dp">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    android:id="@+id/filmProfile"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/film_name"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginBottom="8dp"
                    android:layout_gravity="center_vertical"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.08"
                    android:textSize="20sp"/>
        </LinearLayout>

</LinearLayout>

您可以将此库用于放大和缩小功能,例如 link Zoom library

您需要在您的项目中添加这个gradle

implementation 'com.otaliastudios:zoomlayout:1.6.1'

检查下面的示例布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dark_grey"
    android:orientation="vertical"
    android:weightSum="13">


    <ImageView
        android:id="@+id/iv_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_margin="@dimen/margin_small"
        android:padding="@dimen/margin_small"
        android:src="@drawable/ic_skip" />


    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="6" />

    <com.otaliastudios.zoom.ZoomImageView
        android:id="@+id/zoom_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:scrollbars="vertical|horizontal"
        app:alignment="center"
        app:animationDuration="280"
        app:flingEnabled="true"
        app:horizontalPanEnabled="true"
        app:maxZoom="2.5"
        app:maxZoomType="zoom"
        app:minZoom="0.7"
        app:minZoomType="zoom"
        app:overPinchable="true"
        app:overScrollHorizontal="true"
        app:overScrollVertical="true"
        app:transformation="centerInside"
        app:transformationGravity="auto"
        app:verticalPanEnabled="true"
        app:zoomEnabled="true" />

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="5" />
</LinearLayout>

而在 JAVA class 你需要像下面这样加载图像,我使用 Glide 来像下面这样加载图像

 GlideApp.with(this)
                    .asBitmap()
                    .load(YOUR_IMAGE_URL)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                            YOUR_IMAGEVIEW.setImageBitmap(resource);
                        }
                    });

检查 ZoomActivity.java 用于演示目的,如下所示

 public class ZoomActivity extends AppCompatActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_zoom);

      //  ActivityZoomBinding mBinding = DataBindingUtil.setContentView(this, R.layout.activity_zoom);


        ImageView zoomImage = findViewById(R.id.zoom_image);


        ///GETTING INTENT DATA
        Intent intent = getIntent();
        if (intent.hasExtra("ZOOM_IMAGE")) {

            String imageUrl = intent.getStringExtra("ZOOM_IMAGE");

            GlideApp.with(this)
                    .asBitmap()
                    .load(imageUrl)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                            zoomImage.setImageBitmap(resource);
                        }
                    });


        }


    }


}

调整位图大小:

Bitmap resizedBitmap = Bitmap.createScaledBitmap(
    originalBitmap, newWidth, newHeight, false);
  1. 修改 view holder with view as

    class MyViewHolder extends RecyclerView.ViewHolder
    {
            TextView name;
            ImageView profilePic;
            View mView;
            public MyViewHolder(@NonNull View itemView) 
        {
                super(itemView);
                mView = itemView;
                name = (TextView) itemView.findViewById(R.id.film_name);
                profilePic = (ImageView) itemView.findViewById(R.id.filmProfile);
            }
        }
    
  2. 在适配器中实现视图持有者的点击侦听器,然后用图像 url.

    启动 activity
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        myViewHolder.name.setText(profiles.get(i).getName());
        Picasso.get().load(profiles.get(i).getProfilePic()).into(myViewHolder.profilePic);  
        mViewHolder.mView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                startImageActivity(profiles.get(i).getProfilePic());
            }
        });
    }
    

3.Create activity 并从所需尺寸的图像视图开始。

    void startImageActivity(String imgUrl)
    {
    Intent intent = new Intent(context,ViewImage.class);
    intent.putExtra("profilePic",imgUrl);
    context.startActivity(intent);
    }

或者尝试在外部应用程序中打开图像

    void startImageActivity(String imgUrl)
    {
   Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(imgUrl));             
   context.startActivity(intent);
    }