修改位图图像

huangapple go评论77阅读模式
英文:

Modifying bitmap image

问题

我正在处理一个 Android Studio 项目。我正在尝试选择用户在图像上触摸的像素,如图1所示。

我想在用户触摸的像素上添加(X),我该如何通过修改 bitmap 来实现?

这是图像。

图片:链接 宽度:800。

英文:

I'm working with an android studio project. I'm trying to select user touched pixel on an image, as shown Fig 1.

I want to add (X) on a pixel touched by the user, how can I make it by modifying bitmap ?

This is image.

<img src="https://i.stack.imgur.com/8f9df.png" width="800"/>

答案1

得分: 0

如果我正确理解你的问题你希望在用户每次点击位图时都在上面绘制 X以下是你需要的代码

ImageView img;
int clickCount = 0;
Bitmap src;
int colorTarget = Color.BLACK;

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

    img = findViewById(R.id.img);

    img.setOnTouchListener((v, event) -> {
        int x = (int) event.getX();
        int y = (int) event.getY();
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            drawXByPosition(img, src, x, y);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            clickCount++;
            if (clickCount % 2 == 0) colorTarget = Color.BLACK;
            else colorTarget = Color.WHITE;
        }
        return true;
    });
}

private void drawXByPosition(ImageView iv, Bitmap bm, int x, int y) {
    if (x < 0 || y < 0 || x > iv.getWidth() || y > iv.getHeight())
        Toast.makeText(getApplicationContext(), "超出 ImageView 范围", Toast.LENGTH_SHORT).show();
    else {
        int projectedX = (int) ((double) x * ((double) bm.getWidth() / (double) iv.getWidth()));
        int projectedY = (int) ((double) y * ((double) bm.getHeight() / (double) iv.getHeight()));
        src = drawX(src, "X", 44, colorTarget, projectedX, projectedY);
        img.setImageBitmap(src);
    }
}

public Bitmap drawX(final Bitmap src,
                    final String content,
                    final float textSize,
                    @ColorInt final int color,
                    final float x,
                    final float y) {
    Bitmap ret = src.copy(src.getConfig(), true);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Canvas canvas = new Canvas(ret);
    paint.setColor(color);
    paint.setTextSize(textSize);
    Rect bounds = new Rect();
    paint.getTextBounds(content, 0, content.length(), bounds);
    canvas.drawText(content, x, y + textSize, paint);
    return ret;
}
英文:

If I understand your question correctly, you want to draw X in each time the user click on Bitmap, here's what you need :

修改位图图像

    ImageView img;
int clickCount = 0;
Bitmap src;
int colorTarget = Color.BLACK;
@SuppressLint(&quot;ClickableViewAccessibility&quot;)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.img);
img.setOnTouchListener((v, event) -&gt; {
int x = (int) event.getX();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
drawXByPosition(img, src, x, y);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
clickCount++;
if (clickCount % 2 == 0) colorTarget = Color.BLACK;
else colorTarget = Color.WHITE;
}
return true;
});
}
private void drawXByPosition(ImageView iv, Bitmap bm, int x, int y) {
if (x &lt; 0 || y &lt; 0 || x &gt; iv.getWidth() || y &gt; iv.getHeight())
Toast.makeText(getApplicationContext(), &quot;Outside of ImageView&quot;, Toast.LENGTH_SHORT).show();
else {
int projectedX = (int) ((double) x * ((double) bm.getWidth() / (double) iv.getWidth()));
int projectedY = (int) ((double) y * ((double) bm.getHeight() / (double) iv.getHeight()));
src = drawX(src, &quot;X&quot;, 44, colorTarget, projectedX, projectedY);
img.setImageBitmap(src);
}
}
public Bitmap drawX(final Bitmap src,
final String content,
final float textSize,
@ColorInt final int color,
final float x,
final float y) {
Bitmap ret = src.copy(src.getConfig(), true);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Canvas canvas = new Canvas(ret);
paint.setColor(color);
paint.setTextSize(textSize);
Rect bounds = new Rect();
paint.getTextBounds(content, 0, content.length(), bounds);
canvas.drawText(content, x, y + textSize, paint);
return ret;
}

huangapple
  • 本文由 发表于 2020年10月6日 03:18:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64214863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定