直播平台搭建,显示图片&&隐藏图片

直播平台搭建,显示图片&&隐藏图片

一:界面布局

 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn_picture"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:text="获取图片" />
       </RelativeLayout>
    <ImageView
        android:id="@+id/img_photos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

二:Java代码

 

这里使用到了一个算法和判断语句,确保用户可以反复点击,达到相同的效果。

 


public class MainActivity extends AppCompatActivity {
    private Button btn_picture;
    private ImageView img_photos;
    public int TAG = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_picture=findViewById(R.id.btn_picture);
        img_photos=findViewById(R.id.img_photos);
        btn_picture.setOnClickListener(v -> {
            TAG++;
            img_photos.setImageResource(R.drawable.mybaby);
            if (TAG%2==0){
                img_photos.setVisibility(View.VISIBLE);
            }else {
                img_photos.setVisibility(View.INVISIBLE);
            }
        });
    }

 

以上就是直播平台搭建,显示图片&&隐藏图片, 更多内容欢迎关注之后的文章

 

posted @ 2023-06-20 14:12  云豹科技-苏凌霄  阅读(35)  评论(0编辑  收藏  举报