如何以编程方式用图像填充FAB并修复LinearLayout裁剪FAB阴影?

我有一个LinearLayout,里面装有FAB,FAB用作您单击的按钮,它使您可以选择图像作为组的图标(例如Telegram和WhatsApp),并且我有2个问题:

  1. 我想用用户选择的图像填充FAB(当然是通过编程方式)
  2. LinearLayout裁剪FAB阴影并使它看起来像素化。 我有一个问题,这甚至是正确的方法吗?使用FAB?我尝试使用CircleImageView,但是在设置默认图标时遇到问题... 非常感谢您的任何帮助:)

我尝试将其更改为CircleImageView,但是随后出现了设置选择照片的默认图标的问题,并且我知道圆形按钮应该是FAB。 而且我尝试将其更改为RelativeLayout,但是发生了同样的问题...

XML:

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/photo_title_bar_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="2dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="2dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.floatingactionbutton.FloatingactionButton
            android:id="@+id/add_a_icon_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start|center_vertical"
            android:backgroundTint="@color/add_image_background"
            android:src="@drawable/ic_add_a_photo_blue"
            app:borderWidth="0dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:tint="@null" />

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/group_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="10dp"
            android:hint="Title"/>
    </androidx.appcompat.widget.LinearLayoutCompat>

程序:

        pickPhotoFAB.setOnClicklistener(new View.OnClicklistener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(AddEditGroupactivity.this,Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                        //request the permission
                        activityCompat.requestPermissions(AddEditGroupactivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},MY_PERMISSIONS_READ_EXTERNAL_STORAGE);

                        // MY_PERMISSIONS_REQUEST_READ_CONTactS is an
                        // app-defined int constant. The callback method gets the
                        // result of the request.
                }
                if ((ContextCompat.checkSelfPermission(AddEditGroupactivity.this,Manifest.permission.READ_EXTERNAL_STORAGE)
                        == PackageManager.PERMISSION_GRANTED)) {
                    Intent photoPick = new Intent(
                            Intent.actION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startactivityForResult(photoPick,PICK_IMAGE_REQUEST);
                }
            }
        });

protected void onactivityResult(int requestCode,int resultCode,@Nullable Intent data) {
        super.onactivityResult(requestCode,resultCode,data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PICK_IMAGE_REQUEST:
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};

                    Cursor cursor = getcontentResolver().query(selectedImage,filePathColumn,null,null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getcolumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();

                    Bitmap pictureBM = BitmapFactory.decodeFile(picturePath);

                    pickPhotoFAB.setImageDrawable(new BitmapDrawable(getResources(),pictureBM));
                    break;
                default:
                    Toast.makeText(getapplicationContext(),"Couldn't get photo",Toast.LENGTH_SHORT).show();
            }
        }
    }

实际结果: https://imgur.com/a/x5e6NzQ
当我选择图像时,FAB保持不变。

gukoule 回答:如何以编程方式用图像填充FAB并修复LinearLayout裁剪FAB阴影?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3141088.html

大家都在问