📜  Android中的PopView

📅  最后修改于: 2021-05-13 15:05:04             🧑  作者: Mango

在本文中, PopView是在android中添加的。当用户点击视图时,将出现带有圆形灰尘效果的弹出动画。弹出旧视图后,也会出现一个新视图。 PopView可用于隐藏视图或更改视图。它使用户界面更具吸引力。如果要制作启动器应用程序,则可以在该应用程序中关闭最近使用过的应用程序。这种观点将帮助我们获得更好的用户体验。

方法:

步骤1:build.gradle文件中添加支持库,并在“依赖项”部分中添加依赖项。它将允许开发人员直接使用内置函数。

XML
dependencies {     
      implementation 'rb.popview:popview:0.1.0'
}


image_view.xml


    


activity_main.xml


   
    
   
    


MainActivity.java
package org.geeksforgeeks.popView          
  
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import rb.popview.PopField;
  
public class MainActivity extends AppCompatActivity {
  
    ImageView popImage, changeImage;
  
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // Attach popField to the layout
        final PopField popField = PopField.attach2Window(this);
  
        popImage = findViewById(R.id.imageView);
        changeImage = findViewById(R.id.changeImage);
  
        popImage.setOnClickListener(
                 new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //It will pop the view.
                popField.popView(v);
            }
        });
  
        changeImage.setOnClickListener(
                    new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  
                // It is used to inflate the image_view
                // so that when the gfg_two image will pop
                // gfg_first image will take place of it.
                LayoutInflater layoutInflater = 
                        (LayoutInflater) getApplicationContext()
                        .getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                final View addView = layoutInflater
                                     .inflate(R.layout.image_view
                                     , null);
                ImageView newImageView = addView
                                         .findViewById(R.id.imageView2);
                newImageView.setImageDrawable(
                             getResources()
                            .getDrawable(R.drawable.gfg_first));
                popField.popView(v,addView);
            }
        });
    }
}


步骤2:image_view.xml文件中添加以下代码。在此文件中,将ImageView添加到布局中。该文件用于使视图膨胀。

image_view.xml



    
 

步骤3:activity_main.xml文件中添加以下代码。在此文件中,将ImageView添加到布局中。

activity_main.xml



   
    
   
    

步骤4:MainActivity中添加以下代码。 Java文件。在此文件中,将OnClickListner添加到ImageViews中,以便每当用户点击它时, setOnClickListener都会自动被调用。在此删除一个图像并替换另一个图像,以便对此image_view.XML进行膨胀以替换先前的图像。

主要活动。Java

package org.geeksforgeeks.popView          
  
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import rb.popview.PopField;
  
public class MainActivity extends AppCompatActivity {
  
    ImageView popImage, changeImage;
  
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // Attach popField to the layout
        final PopField popField = PopField.attach2Window(this);
  
        popImage = findViewById(R.id.imageView);
        changeImage = findViewById(R.id.changeImage);
  
        popImage.setOnClickListener(
                 new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //It will pop the view.
                popField.popView(v);
            }
        });
  
        changeImage.setOnClickListener(
                    new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  
                // It is used to inflate the image_view
                // so that when the gfg_two image will pop
                // gfg_first image will take place of it.
                LayoutInflater layoutInflater = 
                        (LayoutInflater) getApplicationContext()
                        .getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                final View addView = layoutInflater
                                     .inflate(R.layout.image_view
                                     , null);
                ImageView newImageView = addView
                                         .findViewById(R.id.imageView2);
                newImageView.setImageDrawable(
                             getResources()
                            .getDrawable(R.drawable.gfg_first));
                popField.popView(v,addView);
            }
        });
    }
}

输出:

参考:

  • https://developer.android.com/reference/android/view/LayoutInflater
  • https://github.com/krishnarb3/Popview-Android
想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!