首页 > 解决方案 > 从 imageView 获取相同大小的位图

问题描述

我正在制作一个应用程序,它可以从大图像中剪切一个圆形并将其保存为缩略图。

为此,我创建了一个 FrameLayout 来包含一个尺寸与父级匹配的 ImageView 和一个 CustomView。Glide 用于将图像加载到 ImageView 中。位于 imageview 顶部的自定义视图具有 onclick 方法,允许用户在图像周围拖动可缩放的圆圈。

<FrameLayout
    android:layout_width="357dp"
    android:layout_height="247dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
        <!--app:srcCompat="@android:drawable/btn_star_big_on" />-->

    <com.example.christopher.thumbnailtest.ThumbnailFrame
        android:id="@+id/ThumbnailFrame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

目前,我正在尝试创建一个与 ImageView 大小完全相同的位图。

public void cropImage(View view) {

    ImageView image = (ImageView) findViewById(R.id.imageView);

    BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
    Bitmap bitmap = drawable.getBitmap();

    Log.d("bitmap", bitmap.toString());

    //Create a bitmap with the same dimensions (thumbnail)
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

    Log.d("BITMAP_WIDTH", String.valueOf(bitmap.getWidth()));
    Log.d("BITMAP_Height", String.valueOf(bitmap.getHeight()));

    //Create new canvas with our bitmap to draw into
    Canvas canvas = new Canvas(output);
    //Set a paint object for a solid colored object
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(0XFF000000);

    //Get a handle on current image object
    ThumbnailFrame thumbnail = findViewById(R.id.ThumbnailFrame);

    //Draw a small circle on the new bitmap (thumbnail) with the same dimensions as that of our thumbnail frame.
    canvas.drawCircle(thumbnail.getyPosit(), thumbnail.getxPosit(), thumbnail.getradius(), paint);
    Log.d("XPOSIT", String.valueOf(thumbnail.getxPosit()));
    Log.d("YPOSIT", String.valueOf(thumbnail.getyPosit()));
    Log.d("RADIUS", String.valueOf(thumbnail.getradius()));

    //Use Porter.Duff method to make picture ONLY exist in a new bitmap
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    //Crop new bitmap with createBitmap
    canvas.drawBitmap(bitmap, 0, 0, paint);
    //Compress?  and save new bitmap as a thumbnail, and send thumbnail URI 
}

可悲的是,返回的图像与帧的大小不同,并且在某些图像上,位图大小会发生变化。当最初加载的星形是 150 x 150 时,对于这个特定的图像,它是 (988 x 988),但是图像视图的尺寸应该超过这个尺寸并且是矩形而不是正方形。

D/bitmap: android.graphics.Bitmap@491f236

D/BITMAP_WIDTH: 988

D/BITMAP_Height: 988

D/XPOSIT: 1378.0

D/YPOSIT: 938.0

D/RADIUS: 50.0

有没有一种方法可以获得完全相同大小的 imageView 的精确位图版本?

注意:它不需要缩放到图像视图的尺寸,但位图应该是完全相同的大小,也许用 alpha 通道填充额外的空间。

标签: javaandroidandroid-bitmapandroid-glidebitmapimage

解决方案


经过进一步研究,我了解到您永远不应该使用 image.buildDrawingCache();Bitmap bmap = image.getDrawingCache();

这些现在是不推荐使用的方法,对我来说结果是仅部分加载的扭曲图像。

如果您尝试做我所做的事情,请将图片中圆圈的位置转换为位图的测量值。这很容易做到,特别是如果您的图像视图特别方形

[( x coord. of circle) / (imageview width)] * [bitmap width]

使用...

    BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
    Bitmap bmap = drawable.getBitmap();

如果您尝试截取图像视图的屏幕截图,可以使用 PixelCopy(),我不知道如何使用它,但它记录在网站上。 https://developer.android.com/reference/android/view/PixelCopy


推荐阅读