【问题标题】:Bitmap image issue android位图图像问题android
【发布时间】:2013-06-18 19:38:57
【问题描述】:

这是我在指定位置的图像上绘制矩形的代码。矩形已绘制,但我的问题是现在我的图像未显示。

Bitmap bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
imageView.setImageBitmap(bitmap);

Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(50);
float left = 20;
float topy = 20;
float right = 50;
float bottom = 50;
canvas.drawRect(left, topy, right, bottom, paint);

请告诉我的代码有什么问题吗?

【问题讨论】:

  • 我的错误改变了...
  • 知道这里有什么问题吗??
  • 我认为你需要在四个边上画 4 条线!
  • @user2291423,我找到了问题解决方案,查看答案。

标签: android android-imageview android-image


【解决方案1】:

您已将StrokeWidth 设置为50。这是非常巨大的。要么删除它,要么减少它。否则代码可以绘制矩形。

我尝试了以下代码。

Bitmap bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
button.setImageBitmap(bitmap);

Paint paint = new Paint();
paint.setColor(Color.BLACK); // canvas background fill 
canvas.drawPaint(paint); // just to check how big rectangle draw on canvas
    //you can remove 2 above lines. its my testing
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(5); //5 instead of 50
float left = 20;
float topy = 20;
float right = 50;
float bottom = 50;
canvas.drawRect(left, topy, right, bottom, paint);

编辑

你可以试试下面的布局

<LinearLayout
    android:id="@+id/linearParentHolder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:gravity="center" >

    <RelativeLayout
        android:id="@+id/relativeHolder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <ImageView
            android:id="@+id/imgMainImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:contentDescription="@string/app_name"
            android:scaleType="fitCenter" />

        <com.customview.CustomRectangleOverlayView
            android:id="@+id/photoSortrView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imgMainImage"
            android:layout_alignLeft="@+id/imgMainImage"
            android:layout_alignRight="@+id/imgMainImage"
            android:layout_alignTop="@+id/imgMainImage" />
    </RelativeLayout>
</LinearLayout>

其中,两个视图在RelativeLayout中。一个是图像视图,第二个是用于绘制矩形的自定义视图。您的 ImageView 将与第二个重叠。第二种观点具有透明度。所以,这看起来就像是在原始图像上绘制矩形。

【讨论】:

  • 为什么不使用另一个视图来绘制具有透明度的矩形?或将您的图像设置在背景中,而不是图像视图的来源。
  • @chinthan 其实是我想要的,比如在 Facebook 标记照片的某些部分
  • 好的。有你的问题。等待。解决你的问题。给 5 分钟。
【解决方案2】:

试试这个

    Bitmap bm = new BitmapFactory().decodeResource(getResources(), your_image_id, Bitmap.Config.ARGB_8888);
    Bitmap bitmap = Bitmap.createBitmap(bm);
    Canvas canvas = new Canvas(bitmap);


    Paint paint = new Paint();
    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setStrokeWidth(50);
    float left = 20;
    float topy = 20;
    float right = 50;
    float bottom = 50;
    canvas.drawRect(left, topy, right, bottom, paint);

imageView.setImageBitmap(bitmap);

【讨论】:

  • 您好尝试过这个,但在更改为我的图像时第一行显示错误..:(
  • 我认为您需要在图像上绘制一个矩形,所以您参考@Arun 的答案。
【解决方案3】:

你需要先使图像可变,然后在其上绘制矩形并使其不可变-

bitmap3=mutableimage3.copy(Bitmap.Config.ARGB_4444,true);// mutable image.

bitmap3=mutableimage3.copy(Bitmap.Config.ARGB_4444,false);//immutable

【讨论】:

    猜你喜欢
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多