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

时间:2019-11-07 23:31:10

标签: android-studio bitmap floating-action-button

我有一个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, 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保持不变。

0 个答案:

没有答案