【问题标题】:Android: Using CilpDrawable to horizontally crop a selected image does not work?Android:使用 CilpDrawable 水平裁剪所选图像不起作用?
【发布时间】:2021-06-20 22:28:16
【问题描述】:

在我的应用程序中,用户按下Button 来选择要显示在ImageView 上的图像。图像从右侧裁剪 50%。然而,当我运行我的代码时,用户选择的图像根本没有裁剪,如下面的屏幕截图所示:

当前应用(未裁剪)

Desired App (Cropped):白色阴影区域应该被裁剪:

这似乎很奇怪,考虑到:

  1. 我相信我已经正确地从ImageView 检索到ClipDrawableClipDrawable mClipDrawable = new ClipDrawable(singlePreviewBox.getDrawable(), 11, ClipDrawable.HORIZONTAL);

  2. 我已将ClipDrawable 明确设置为裁剪 50% (mClipDrawable.setLevel(5000))。

下面是我的代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/transition_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
    <Button
        android:id="@+id/photo_input_1"
        android:onClick="chooseFile"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:fontFamily="sans-serif"
        android:text="No Photo"
        android:textAllCaps="false"
        android:textSize="16sp"
        android:background="#f3f3f3"
        android:elevation="4dp"
        android:layout_margin="4dp"
        android:minHeight="40dp"/>

    <ImageView
            android:id="@+id/image_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxHeight="370dp"
            android:adjustViewBounds="true"/>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.changingimageviewwidth">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

MainActivity.java

package com.example.changingimageviewwidth;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.io.InputStream;

public class MainActivity extends Activity
{
    // Request code used when reading in a file
    private Integer READ_IN_FILE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void chooseFile(View v) {
        // Specify that only photos are allowed as inputs (.jpg, .png).
        Intent photoInputSpecs = new Intent(Intent.ACTION_GET_CONTENT);
        photoInputSpecs.setType("image/*");

        Intent photoInputHandler = Intent.createChooser(photoInputSpecs, "Choose a file");
        startActivityForResult(photoInputHandler, READ_IN_FILE);
    }

    // Processes the results of Activities.
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        try {
            // Processes the photos that the user selected
            if (requestCode == READ_IN_FILE) {
                // Retrieve the photo's location
                Uri photoUri = data.getData();

                InputStream photoInputStream = getContentResolver().openInputStream(photoUri);
                System.out.println("[photoInputStream.available()] = " + photoInputStream.available());
                Bitmap photoBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),
                        photoUri);
                // Display the selected image in the preview box
                displayPhoto(photoUri);
            }
        }
        catch (Exception ex) {
            System.out.println("onActivityResult error: " + ex.toString());
        }
    }

    // Displays a specified photo in the split and single screen preview boxes.
    private void displayPhoto(Uri photoUri) {
        ImageView singlePreviewBox = findViewById(R.id.image_container);
        singlePreviewBox.setImageURI(photoUri);

        ClipDrawable mClipDrawable = new ClipDrawable(singlePreviewBox.getDrawable(), 11,
                                                      ClipDrawable.HORIZONTAL);
        mClipDrawable.setLevel(5000);
    }
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    displayPhoto() 中的 mClipDrawable.setLevel(5000); 之后调用singlePreviewBox.setBackground(mClipDrawable); 将解决您的问题。

    private void displayPhoto(Uri photoUri) {
        ImageView singlePreviewBox = findViewById(R.id.image_container);
        singlePreviewBox.setImageURI(photoUri);
    
        ClipDrawable mClipDrawable = new 
        ClipDrawable(singlePreviewBox.getDrawable(), 11, ClipDrawable.HORIZONTAL);
        mClipDrawable.setLevel(5000);
        //Add this one to apply the changes.
        singlePreviewBox.setBackground(mClipDrawable);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-01
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多