android - 在图像上的触摸点(坐标)处绘制矩形或形状

标签 android drawable

如何在触摸点(如坐标 28,87)绘制矩形(使用形状资源)。我创建了一个像这样的形状。

android:shape="rectangle" >


<solid 
    android:color="@color/transparent"/>
<stroke

    android:width="3dp"
    android:color="@color/green" />

我想在图像上的触摸点处绘制这个矩形。

最佳答案

您可以使用 View 的 onDraw() 方法在该 View 上绘制形状。没有可用于在 View Canvas 上绘制可绘制形状的方法。
而且您不必使用可绘制的形状来绘制矩形。您可以使用 canvas.drawRect() 方法绘制矩形。 这是一个代码:

public class MyView extends View{

float x,y;
Bitmap bmp;
Paint mPaint;
float width = 100.0f;
float height = 50.0f;

boolean touched = false;

public MyView (Context context)
{
    super(context);
    x = y = 0;
    mPaint = new Paint();
    mPaint.setColor(Color.BLUE);
    mPaint.setStyle(Style.STROKE);
}

@Override
protected void onDraw (Canvas canvas)
{
    canvas.drawColor(Color.WHITE);
    if(touched)
    {
        canvas.drawRect(x, y, x+width, y+height, mPaint);

    }   
}

@Override
public boolean onTouchEvent (MotionEvent event)
{
    touched = true;
    //getting the touched x and y position
    x = event.getX();
    y = event.getY();
    invalidate();
    return true;
}

}

关于android - 在图像上的触摸点(坐标)处绘制矩形或形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12246775/

相关文章:

android - Glide : load drawable but don't scale placeholder

android - Google-ads DFP 多个广告尺寸失败

android - 从java修改一个xml StateListDrawable

android - 在 Vector Drawable 中翻转 Drawable 图像

android + 访问远程可绘制对象 - 最佳实践

android - 如何在 LinearLayout 中加载动画作为背景?

java - 列表和适配器

android - 尝试在空对象引用上调用虚拟方法 'androidx.navigation.NavGraph androidx.navigation.NavDestination.getParent()'

android - 关于在an​​droid中上传图片的查询

android - 从intellij 12导入项目到android studio beta