加载网络图片Glide+圆角 fragment的动态添加 底部图标点击选中

一、加载网络图片Glide+圆角

1、添加网络请求。

    <!-- 添加权限 -->
    <uses-permission android:name="android.permission.INTERNET" />
  android:usesCleartextTraffic="true"

2、添加依赖包。

   //第三方框架依赖包 glide
    implementation 'com.github.bumptech.glide:glide:4.13.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0'

3、加载网络图片。

        //添加网络请求 有一些功能需要添加请求
        //加载网络图片  android本身不能加载网络 -->第三方框架 Glide
        Glide.with(this)
                .load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg9.51tietu.net%2Fpic%2F2019-091023%2Fhrhi1fi1vhbhrhi1fi1vhb.jpg&refer=http%3A%2F%2Fimg9.51tietu.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651385943&t=17ca45a3845f9ac162a05b133dd455e1")
                .into(view);

        //显示圆形图片
//        Glide.with(this)
//                .load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg9.51tietu.net%2Fpic%2F2019-091023%2Fhrhi1fi1vhbhrhi1fi1vhb.jpg&refer=http%3A%2F%2Fimg9.51tietu.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651385943&t=17ca45a3845f9ac162a05b133dd455e1")
//                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
//                .into(iv1);

//        //显示圆角图片
//        Glide.with(this)
//                .load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg9.51tietu.net%2Fpic%2F2019-091023%2Fhrhi1fi1vhbhrhi1fi1vhb.jpg&refer=http%3A%2F%2Fimg9.51tietu.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651385943&t=17ca45a3845f9ac162a05b133dd455e1")
//                .apply(RequestOptions.bitmapTransform(new RoundedCorners(60)))
//                .into(view);

二、fragment的动态添加。

1、创建待添加的碎片实例。

2、获取FragmentManager,在活动中可以直接通过调用getSupportFragmentManager()方法得到。

3、开启一个事务、通过调用beginTransaction()方法开启。

4、向容器中添加或替换碎片。

5、提交事务,调用commit()方法来完成。

        //加载fragment,第一个参数是activity给fragment的约束布局id,第二个是new的fragment的对象或者说fragment的引用
        //replace 每执行一次会创建一个新的fragment
        getSupportFragmentManager().beginTransaction().add(R.id.fragment,blankFragment1).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment,blankFragment2).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment,blankFragment3).commit();
        //设置显示
        getSupportFragmentManager().beginTransaction()
                .show(blankFragment1)
                .hide(blankFragment2)
                .hide(blankFragment3)
                .commit();

        ConstraintLayout btn1=findViewById(R.id.cl1);
     
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSupportFragmentManager().beginTransaction()
                     .show(blankFragment1)
                     .hide(blankFragment2)
                     .hide(blankFragment3)
                     .commit();
                
            }
        });

 三、底部图标点击选中。

最关键的是创建xml文件。设置选中或者未选中显示。要准备两份素材。

这是其中一个xml文件。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/contact_select" android:state_selected="true" />
    <item android:drawable="@drawable/contact_unselect" android:state_selected="false" />
</selector>

 activity的layout布局里调用这个xml文件。

 android:background="@drawable/contact"

活动里拿控件引用。设置显示。

  btn1.setSelected(true);
  btn2.setSelected(false);
  btn3.setSelected(false);

activity_main主布局代码。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/con1"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/ad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/con1"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        app:layout_constraintBottom_toBottomOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/cl1"
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:background="@color/purple_200"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/cl2"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <ImageView
                android:id="@+id/image1"
                android:layout_width="50dp"
                android:layout_height="50dp"

                android:background="@drawable/contact"

                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="联系人"
                android:textColor="@color/white"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.527"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/image1" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/cl2"
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:background="@color/purple_200"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/cl3"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/cl1"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0">
            <ImageView
                android:id="@+id/image2"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/message"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="消息"
                android:textColor="@color/white"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.527"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/image2" />


        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/cl3"
            android:layout_width="0dp"
            android:layout_height="80dp"
            android:background="@color/purple_200"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/cl2"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0">
            <ImageView
                android:id="@+id/image3"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/dongtai"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/text3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="动态"
                android:textColor="@color/white"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.527"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/image3" />


        </androidx.constraintlayout.widget.ConstraintLayout>


    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Maintivity代码。

package com.hnucm.fragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

public class MainActivity extends AppCompatActivity {
    BlankFragment blankFragment=new BlankFragment();
    BlankFragment1 blankFragment1=new BlankFragment1();
    BlankFragment2 blankFragment2=new BlankFragment2();
    BlankFragment3 blankFragment3=new BlankFragment3();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView view =findViewById(R.id.ad);

        //添加网络请求 有一些功能需要添加请求
        //加载网络图片  android本身不能加载网络 -->第三方框架 Glide
        Glide.with(this)
                .load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg9.51tietu.net%2Fpic%2F2019-091023%2Fhrhi1fi1vhbhrhi1fi1vhb.jpg&refer=http%3A%2F%2Fimg9.51tietu.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651385943&t=17ca45a3845f9ac162a05b133dd455e1")
                .into(view);

        //显示圆形图片
//        Glide.with(this)
//                .load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg9.51tietu.net%2Fpic%2F2019-091023%2Fhrhi1fi1vhbhrhi1fi1vhb.jpg&refer=http%3A%2F%2Fimg9.51tietu.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651385943&t=17ca45a3845f9ac162a05b133dd455e1")
//                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
//                .into(iv1);

//        //显示圆角图片
//        Glide.with(this)
//                .load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg9.51tietu.net%2Fpic%2F2019-091023%2Fhrhi1fi1vhbhrhi1fi1vhb.jpg&refer=http%3A%2F%2Fimg9.51tietu.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651385943&t=17ca45a3845f9ac162a05b133dd455e1")
//                .apply(RequestOptions.bitmapTransform(new RoundedCorners(60)))
//                .into(view);

        //加载fragment,第一个参数是activity给fragment的约束布局id,第二个是new的fragment的对象或者说fragment的引用
        //replace 每执行一次会创建一个新的fragment
        getSupportFragmentManager().beginTransaction().add(R.id.fragment,blankFragment1).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment,blankFragment2).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment,blankFragment3).commit();
        //设置显示
        getSupportFragmentManager().beginTransaction()
                .show(blankFragment1)
                .hide(blankFragment2)
                .hide(blankFragment3)
                .commit();

        ConstraintLayout btn1=findViewById(R.id.cl1);
        ConstraintLayout btn2=findViewById(R.id.cl2);
        ConstraintLayout btn3=findViewById(R.id.cl3);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSupportFragmentManager().beginTransaction()
                        .show(blankFragment1).hide(blankFragment2).hide(blankFragment3).commit();
                btn1.setSelected(true);
                btn2.setSelected(false);
                btn3.setSelected(false);
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSupportFragmentManager().beginTransaction()
                        .show(blankFragment2).hide(blankFragment1).hide(blankFragment3).commit();

                btn1.setSelected(false);
                btn2.setSelected(true);
                btn3.setSelected(false);
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSupportFragmentManager().beginTransaction()
                        .show(blankFragment3).hide(blankFragment2).hide(blankFragment1).commit();

                btn1.setSelected(false);
                btn2.setSelected(false);
                btn3.setSelected(true);
            }
        });

    }
}

猜你喜欢

转载自blog.csdn.net/qq_58451437/article/details/126675874