首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从imageView获取相同大小的位图

从imageView获取相同大小的位图
EN

Stack Overflow用户
提问于 2018-06-25 08:36:52
回答 1查看 575关注 0票数 0

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

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

代码语言:javascript
运行
复制
<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大小完全相同的位图。

代码语言:javascript
运行
复制
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 
}

遗憾的是,返回的图像与帧的大小不同,并且在某些图像上,位图大小会发生变化。当一个星形最初加载时是150x150,对于这个特定的图像是(988x988),但是imageview的尺寸应该延伸到超过这个尺寸,并且是矩形而不是正方形。

代码语言:javascript
运行
复制
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通道填充额外的空间。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51015033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档