• 团队项目-第二阶段冲刺-3


    一、说在前面

    1、今天任务:完善个人信息界面(3h)

    2、明天任务:写意见反馈界面(预计2h)

    3、遇到问题:就是不好看

    二、效果

      三、代码

    1、myFragment.java

    package com.me.fragment;
    
    
    import android.Manifest;
    import android.annotation.TargetApi;
    import android.app.AlertDialog;
    import android.content.ContentUris;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Build;
    import android.os.Bundle;
    
    import androidx.annotation.Nullable;
    import androidx.core.app.ActivityCompat;
    import androidx.core.content.ContextCompat;
    import androidx.core.content.FileProvider;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.fragment.app.FragmentTransaction;
    import androidx.lifecycle.ViewModelProvider;
    import androidx.lifecycle.ViewModelProviders;
    import androidx.navigation.NavController;
    import androidx.navigation.Navigation;
    
    import android.provider.ContactsContract;
    import android.provider.DocumentsContract;
    import android.provider.MediaStore;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.bumptech.glide.Glide;
    import com.bumptech.glide.load.resource.bitmap.CenterCrop;
    import com.me.ViewModel.NewsViewModel;
    import com.me.news_2.LoginActivity;
    import com.me.news_2.LoveActivity;
    import com.me.news_2.NewsActivity;
    import com.me.news_2.R;
    import com.me.news_2.SugActivity;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    
    import jp.wasabeef.glide.transformations.BlurTransformation;
    import jp.wasabeef.glide.transformations.CropCircleTransformation;
    
    import static android.app.Activity.RESULT_OK;
    
    /**
     * A simple {@link Fragment} subclass.
     */
    public class MyFragment extends Fragment implements View.OnClickListener {
        public static final int TAKE_CAMERA = 101;
        public static final int PICK_PHOTO = 102;
        private Uri imageUri;
        private NewsViewModel newsViewModel;
        private Boolean isLogin = false;
        TextView history;
        TextView save;
        TextView my;
        TextView setting;
        TextView zp;
        TextView sug;
        private ImageView imageViewHead = null,blurImageView;
    
        public MyFragment() {
            // Required empty public constructor
        }
    
        public void event(){
            history.setOnClickListener(this);
            save.setOnClickListener(this);
            my.setOnClickListener(this);
            zp.setOnClickListener(this);
            sug.setOnClickListener(this);
            imageViewHead.setOnClickListener(this);
            blurImageView.setOnClickListener(this);
            setting.setOnClickListener(this);
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View inflate = inflater.inflate(R.layout.fragment_my, container, false);
            history = inflate.findViewById(R.id.txt_history);
            save = inflate.findViewById(R.id.txt_my_save);
            my = inflate.findViewById(R.id.txt_my);
            setting = inflate.findViewById(R.id.txt_setting);
            zp = inflate.findViewById(R.id.txt_zp);
            sug = inflate.findViewById(R.id.txt_sug);
            imageViewHead = inflate.findViewById(R.id.h_head);
            blurImageView = inflate.findViewById(R.id.h_back);
            return inflate;
        }
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            event();
            newsViewModel = ViewModelProviders.of(this).get(NewsViewModel.class);
            if (newsViewModel.bitmap!=null){
                imageViewHead.setImageBitmap(newsViewModel.bitmap);
            }else {
                //设置顶部磨砂图像背景
                Glide.with(requireContext()).load(R.drawable.logo)
                        .bitmapTransform(new BlurTransformation(requireContext(), 25), new CenterCrop(requireContext()))
                        .into(blurImageView);
    
                Glide.with(requireContext()).load(R.drawable.logo)
                        .bitmapTransform(new CropCircleTransformation(requireContext()))
                        .into(imageViewHead);
            }
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.txt_my:
    
                    break;
                case R.id.txt_history:
                    NavController controller = Navigation.findNavController(v);
                    controller.navigate(R.id.action_my_to_historyFragment);
                    break;
                case R.id.txt_my_save:
                    break;
                case R.id.txt_setting:
                    Intent love = new Intent(requireContext(), LoveActivity.class);
                    startActivity(love);
                    break;
                case R.id.txt_zp:
                    break;
                case R.id.txt_sug:
                    Intent sug = new Intent(requireContext(), SugActivity.class);
                    startActivity(sug);
                    break;
                case R.id.h_head:
                    chooseDialog();
                    break;
                case R.id.bu_exit_login:
                case R.id.h_back:
                    Intent intent = new Intent(requireContext(), LoginActivity.class);
                    startActivity(intent);
                    break;
                default:
                     break;
            }
        }
        // 将弹出对话框封装成一个私有方法
        private void chooseDialog() {
            new AlertDialog.Builder(requireContext())//
                    .setTitle("选择头像")//
    
                    .setNegativeButton("相册", new DialogInterface.OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (ContextCompat.checkSelfPermission(requireContext(),
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);
                            } else {
                                //打开相册
                                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                                //Intent.ACTION_GET_CONTENT = "android.intent.action.GET_CONTENT"
                                intent.setType("image/*");
                                startActivityForResult(intent, PICK_PHOTO); // 打开相册
                            }
    
                        }
                    })
    
                    .setPositiveButton("拍照", new DialogInterface.OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
    
                            // 创建File对象,用于存储拍照后的图片
                            //存放在手机SD卡的应用关联缓存目录下
                            File outputImage = new File(getActivity().getExternalCacheDir(), "output_image.jpg");
                       /* 从Android 6.0系统开始,读写SD卡被列为了危险权限,如果将图片存放在SD卡的任何其他目录,
                          都要进行运行时权限处理才行,而使用应用关联 目录则可以跳过这一步
                        */
                            try {
                                if (outputImage.exists()) {
                                    outputImage.delete();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
    
                        /*
                           7.0系统开始,直接使用本地真实路径的Uri被认为是不安全的,会抛 出一个FileUriExposedException异常。
                           而FileProvider则是一种特殊的内容提供器,它使用了和内 容提供器类似的机制来对数据进行保护,
                           可以选择性地将封装过的Uri共享给外部,从而提高了 应用的安全性
                         */
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                //大于等于版本24(7.0)的场合
                                imageUri = FileProvider.getUriForFile(requireContext(), "com.feige.pickphoto.fileprovider", outputImage);
                            } else {
                                //小于android 版本7.0(24)的场合
                                imageUri = Uri.fromFile(outputImage);
                            }
    
                            //启动相机程序
                            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            //MediaStore.ACTION_IMAGE_CAPTURE = android.media.action.IMAGE_CAPTURE
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                            startActivityForResult(intent, TAKE_CAMERA);
                        }
                    }).show();
    
        }
    
        //通过相册实现圆角头像和磨砂背景
        private void setFrostedBackground(String imagePath){
            Glide.with(requireContext()).load(imagePath)
                    .bitmapTransform(new BlurTransformation(requireContext(), 25), new CenterCrop(requireContext()))
                    .into(blurImageView);
    
            Glide.with(requireContext()).load(imagePath)
                    .bitmapTransform(new CropCircleTransformation(requireContext()))
                    .into(imageViewHead);
        }
        //通过拍照实现圆角头像和磨砂背景
        private void setFrostedBackground(Uri imageUrl){
            Glide.with(requireContext()).load(imageUrl)
                    .bitmapTransform(new BlurTransformation(requireContext(), 25), new CenterCrop(requireContext()))
                    .into(blurImageView);
    
            Glide.with(requireContext()).load(imageUrl)
                    .bitmapTransform(new CropCircleTransformation(requireContext()))
                    .into(imageViewHead);
        }
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            switch (requestCode) {
                case TAKE_CAMERA:
                    if (resultCode == RESULT_OK) {
                        try {
                            // 将拍摄的照片显示出来
                            Bitmap bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(imageUri));
                            newsViewModel.setBitmap(bitmap);
                            imageViewHead.setImageBitmap(newsViewModel.bitmap);
                            setFrostedBackground(imageUri);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case PICK_PHOTO:
                    if (resultCode == RESULT_OK) { // 判断手机系统版本号
                        if (Build.VERSION.SDK_INT >= 19) {
                            // 4.4及以上系统使用这个方法处理图片
                            handleImageOnKitKat(data);
                        } else {
                            // 4.4以下系统使用这个方法处理图片
                            handleImageBeforeKitKat(data);
                        }
                    }
    
                    break;
                default:
                    break;
            }
        }
        @TargetApi(19)
        private void handleImageOnKitKat(Intent data) {
            String imagePath = null;
            Uri uri = data.getData();
            if (DocumentsContract.isDocumentUri(requireContext(), uri)) {
                // 如果是document类型的Uri,则通过document id处理
                String docId = DocumentsContract.getDocumentId(uri);
                if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
                    String id = docId.split(":")[1];
                    // 解析出数字格式的id
                    String selection = MediaStore.Images.Media._ID + "=" + id;
                    imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
                } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
                    Uri contentUri = ContentUris.withAppendedId(Uri.parse("content: //downloads/public_downloads"), Long.valueOf(docId));
                    imagePath = getImagePath(contentUri, null);
                }
            } else if ("content".equalsIgnoreCase(uri.getScheme())) {
                // 如果是content类型的Uri,则使用普通方式处理
                imagePath = getImagePath(uri, null);
            } else if ("file".equalsIgnoreCase(uri.getScheme())) {
                // 如果是file类型的Uri,直接获取图片路径即可
                imagePath = uri.getPath();
            }
            // 根据图片路径显示图片
            displayImage(imagePath);
        }
        /**
         * android 4.4以前的处理方式
         * @param data
         */
        private void handleImageBeforeKitKat(Intent data) {
            Uri uri = data.getData();
            String imagePath = getImagePath(uri, null);
            displayImage(imagePath);
        }
    
        private String getImagePath(Uri uri, String selection) {
            String path = null;
            // 通过Uri和selection来获取真实的图片路径
            Cursor cursor = getActivity().getContentResolver().query(uri, null, selection, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                }
                cursor.close();
            }
            return path;
        }
        private void displayImage(String imagePath) {
            if (imagePath != null) {
                Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                imageViewHead.setImageBitmap(bitmap);
                setFrostedBackground(imagePath);
            } else {
                Toast.makeText(requireContext(), "获取图片失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
    View Code

     2、fragment_my.xml

    <FrameLayout 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="com.me.fragment.MyFragment">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <!--磨砂头像-->
        <RelativeLayout
            android:id="@+id/relative"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/h_back"
                android:layout_width="match_parent"
                android:layout_height="200dp" />
            <ImageView
                android:id="@+id/h_head"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_centerInParent="true" />
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@id/h_back"
                android:layout_marginBottom="20dp"
                android:orientation="horizontal">
                <ImageView
                    android:id="@+id/user_line"
                    android:layout_width="1dp"
                    android:layout_height="25dp"
                    android:layout_centerHorizontal="true"
                    android:layout_marginLeft="15dp"
                    android:background="@android:color/white" />
                <TextView
                    android:id="@+id/user_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toLeftOf="@id/user_line"
                    android:text="张三"
                    android:textColor="@android:color/white"
                    android:textSize="17sp" />
                <TextView
                    android:id="@+id/user_val"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:layout_toRightOf="@id/user_line"
                    android:text="182****5882"
                    android:textColor="@android:color/white"
                    android:textSize="17sp" />
            </RelativeLayout>
    
        </RelativeLayout>
    
        <!--滚动列表-->
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:gravity="center">
                    <ImageView
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        android:src="@mipmap/list_save"
                        android:layout_marginLeft="10dp"/>
    
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:layout_marginLeft="10dp">
    
                        <TextView
                            android:id="@+id/txt_my_save"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:paddingTop="18dp"
                            android:text="我的收藏"
                            android:textColor="#000000"
                            android:textSize="24sp" />
    
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="#D8DDE1"
                            android:layout_below="@+id/txt_my_save"
                            android:layout_marginTop="10dp"
                            android:layout_marginRight="10dp"></View>
                    </RelativeLayout>
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:gravity="center">
                    <ImageView
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        android:src="@mipmap/list_my"
                        android:layout_marginLeft="10dp"/>
    
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp">
                        <TextView
                            android:id="@+id/txt_my"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="我的关注"
                            android:textSize="24sp"
                            android:textColor="#000000"
                            android:paddingTop="18dp"/>
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="#D8DDE1"
                            android:layout_below="@+id/txt_my"
                            android:layout_marginTop="10dp"
                            android:layout_marginRight="10dp"></View>
                    </RelativeLayout>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:gravity="center">
                    <ImageView
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        android:src="@mipmap/list_setting"
                        android:layout_marginLeft="10dp"/>
    
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp">
                        <TextView
                            android:id="@+id/txt_setting"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="爱好设置"
                            android:textSize="24sp"
                            android:textColor="#000000"
                            android:paddingTop="18dp"/>
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="#D8DDE1"
                            android:layout_below="@+id/txt_setting"
                            android:layout_marginTop="10dp"
                            android:layout_marginRight="10dp"></View>
    
                    </RelativeLayout>
                </LinearLayout>
    
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:gravity="center">
                    <ImageView
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        android:src="@mipmap/list_safe_center"
                        android:layout_marginLeft="10dp"/>
    
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp">
                        <TextView
                            android:id="@+id/txt_history"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="历史记录"
                            android:textSize="24sp"
                            android:textColor="#000000"
                            android:paddingTop="18dp"
                            />
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="#D8DDE1"
                            android:layout_below="@+id/txt_history"
                            android:layout_marginTop="10dp"
                            android:layout_marginRight="10dp"></View>
                    </RelativeLayout>
                </LinearLayout>
    
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:gravity="center">
                    <ImageView
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        android:src="@mipmap/list_service"
                        android:layout_marginLeft="10dp"/>
    
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp">
                        <TextView
                            android:id="@+id/txt_zp"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="作品管理"
                            android:textSize="24sp"
                            android:textColor="#000000"
                            android:paddingTop="18dp"/>
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="#D8DDE1"
                            android:layout_below="@+id/txt_zp"
                            android:layout_marginTop="10dp"
                            android:layout_marginRight="10dp"></View>
                    </RelativeLayout>
                </LinearLayout>
    
                <LinearLayout
                    android:id="@+id/layout_sug"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:gravity="center">
                    <ImageView
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        android:src="@mipmap/list_sug"
                        android:layout_marginLeft="10dp"/>
    
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp">
                        <TextView
                            android:id="@+id/txt_sug"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="意见反馈"
                            android:textSize="24sp"
                            android:textColor="#000000"
                            android:paddingTop="18dp"/>
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="#D8DDE1"
                            android:layout_below="@+id/txt_sug"
                            android:layout_marginTop="10dp"
                            android:layout_marginRight="10dp"></View>
                    </RelativeLayout>
                </LinearLayout>
    
    
                <Button
                    android:id="@+id/bu_exit_login"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    style="@style/Button_Login"
                    android:text="退出登入"
                    android:layout_margin="5dp"
                    android:textSize="16dp"/>
            </LinearLayout>
    
        </ScrollView>
    
    
    
    </LinearLayout>
    
    </FrameLayout>
    View Code
  • 相关阅读:
    复杂声明的正确解读(*、()、[])
    (多张图片打包为Zip返回前端下载) 记NetCore HttpClient.GetStreamAsync()返回只读流,Stream的Length属性不可用,报错的问题。
    ### Vue开发环境搭建
    计算机网络原理----CRC编码相关问题及解题思路
    CentOS7安装MongoDB4.4.4
    树莓派4B安装.NET Core 3.1 SDK
    基于数据库的代码自动生成工具,生成JavaBean、生成数据库文档、生成前后端代码等(TableGo v7.0.0版)
    解决Unity启动报错 Assertion failed on expression: 'SUCCEEDED(hr)'
    @RabbitListener注解导致spring bean注入属性为空 解决方案
    一文彻底讲透@Async注解的原理和使用方法
  • 原文地址:https://www.cnblogs.com/wyhqj/p/12886367.html
Copyright © 2020-2023  润新知